Skip to content

Instantly share code, notes, and snippets.

@lukemarsden
Created June 30, 2011 21:45
Show Gist options
  • Save lukemarsden/1057343 to your computer and use it in GitHub Desktop.
Save lukemarsden/1057343 to your computer and use it in GitHub Desktop.
Serialize operations
def serialize(func):
"""
Decorator to make operations which modify state in the cluster happen one
at a time
"""
def wrap(self, *a, **kw):
return self._do_operation(func, self, *a, **kw)
return wrap
class HybridClusterAPI(jsonrpc.JSONRPC):
"""
The web cluster's API.
"""
def __init__(self):
self._operations = []
self._current_operation = None
def _update_operations(self, _result=None):
"""
Called when the current operation completes. Runs the next operation
if there is one, chaining the completion of that operation onto a
recursive call back to this function to check the next operation, etc.
"""
if self._operations:
d, f, a, kw = self._operations.pop(0)
self._current_operation = defer.maybeDeferred(f, *a, **kw)
(self._current_operation
.addBoth(self._update_operations)
.chainDeferred(d))
else:
self._current_operation = None
return _result
def _do_operation(self, func, *a, **kw):
"""
Add an operation to the queue and run the queue if it's not running
already.
"""
d = defer.Deferred()
self._operations.append((d, func, a, kw))
if self._current_operation is None:
self._update_operations()
return d
@serialize
def jsonrpc_deleteSite(self, site):
# ...
pass
@serialize
@defer.inlineCallbacks
def jsonrpc_installWordpress(self, domain, database):
# ...
yield someDeferred
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment