|
@@ -15,24 +15,27 @@ try:
|
|
|
except ImportError:
|
|
|
import pickle
|
|
|
|
|
|
-TT_HOST = getattr(settings, "TT_HOST", None)
|
|
|
-TT_PORT = getattr(settings, "TT_PORT", None)
|
|
|
-
|
|
|
-if not TT_HOST or not TT_PORT:
|
|
|
- raise ImproperlyConfigured(
|
|
|
- "To use the Tokyo Tyrant backend, you have to "
|
|
|
- "set the TT_HOST and TT_PORT settings in your settings.py")
|
|
|
-else:
|
|
|
- def get_server():
|
|
|
- return pytyrant.PyTyrant.open(TT_HOST, TT_PORT)
|
|
|
|
|
|
|
|
|
class Backend(BaseBackend):
|
|
|
"""Tokyo Cabinet based task backend store."""
|
|
|
+ tyrant_host = None
|
|
|
+ tyrant_port = None
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
+ self.tyrant_host = kwargs.get("tyrant_host",
|
|
|
+ getattr(settings, "TT_HOST", self.tyrant_host))
|
|
|
+ self.tyrant_port = kwargs.get("tyrant_port",
|
|
|
+ getattr(settings, "TT_PORT", self.tyrant_port))
|
|
|
+ if not self.tyrant_host or not self.tyrant_port:
|
|
|
+ raise ImproperlyConfigured(
|
|
|
+ "To use the Tokyo Tyrant backend, you have to "
|
|
|
+ "set the TT_HOST and TT_PORT settings in your settings.py")
|
|
|
super(Backend, self).__init__(*args, **kwargs)
|
|
|
self._cache = {}
|
|
|
+
|
|
|
+ def get_server(self):
|
|
|
+ return pytyrant.PyTyrant.open(self.tyrant_host, self.tyrant_port)
|
|
|
|
|
|
def _cache_key(self, task_id):
|
|
|
return "celery-task-meta-%s" % task_id
|
|
@@ -41,7 +44,7 @@ class Backend(BaseBackend):
|
|
|
"""Store task result and status."""
|
|
|
result = self.prepare_result(result)
|
|
|
meta = {"status": status, "result": pickle.dumps(result)}
|
|
|
- get_server()[self._cache_key(task_id)] = serialize(meta)
|
|
|
+ self.get_server()[self._cache_key(task_id)] = serialize(meta)
|
|
|
|
|
|
def get_status(self, task_id):
|
|
|
"""Get the status for a task."""
|
|
@@ -58,7 +61,7 @@ class Backend(BaseBackend):
|
|
|
def _get_task_meta_for(self, task_id):
|
|
|
if task_id in self._cache:
|
|
|
return self._cache[task_id]
|
|
|
- meta = get_server().get(self._cache_key(task_id))
|
|
|
+ meta = self.get_server().get(self._cache_key(task_id))
|
|
|
if not meta:
|
|
|
return {"status": "PENDING", "result": None}
|
|
|
meta = deserialize(meta)
|