|
@@ -1,32 +1,39 @@
|
|
from inspect import getargspec
|
|
from inspect import getargspec
|
|
|
|
|
|
|
|
+from billiard.utils.functional import wraps
|
|
|
|
+
|
|
from celery.task.base import Task, PeriodicTask
|
|
from celery.task.base import Task, PeriodicTask
|
|
|
|
|
|
|
|
|
|
def task(**options):
|
|
def task(**options):
|
|
- """Make a task out of any callable.
|
|
|
|
|
|
+ """Decorator to create a task class out of any callable.
|
|
|
|
+
|
|
|
|
+ Examples:
|
|
|
|
|
|
- Examples:
|
|
|
|
|
|
+ .. code-block:: python
|
|
|
|
|
|
- >>> @task()
|
|
|
|
- ... def refresh_feed(url):
|
|
|
|
- ... return Feed.objects.get(url=url).refresh()
|
|
|
|
|
|
+ @task()
|
|
|
|
+ def refresh_feed(url):
|
|
|
|
+ return Feed.objects.get(url=url).refresh()
|
|
|
|
|
|
|
|
|
|
- >>> refresh_feed("http://example.com/rss") # Regular
|
|
|
|
- <Feed: http://example.com/rss>
|
|
|
|
- >>> refresh_feed.delay("http://example.com/rss") # Async
|
|
|
|
- <AsyncResult: 8998d0f4-da0b-4669-ba03-d5ab5ac6ad5d>
|
|
|
|
|
|
+ With setting extra options and using retry.
|
|
|
|
+
|
|
|
|
+ .. code-block:: python
|
|
|
|
|
|
- # With setting extra options and using retry.
|
|
|
|
|
|
+ @task(exchange="feeds")
|
|
|
|
+ def refresh_feed(url, **kwargs):
|
|
|
|
+ try:
|
|
|
|
+ return Feed.objects.get(url=url).refresh()
|
|
|
|
+ except socket.error, exc:
|
|
|
|
+ refresh_feed.retry(args=[url], kwargs=kwargs, exc=exc)
|
|
|
|
|
|
- >>> @task(exchange="feeds")
|
|
|
|
- ... def refresh_feed(url, **kwargs):
|
|
|
|
- ... try:
|
|
|
|
- ... return Feed.objects.get(url=url).refresh()
|
|
|
|
- ... except socket.error, exc:
|
|
|
|
- ... refresh_feed.retry(args=[url], kwargs=kwargs,
|
|
|
|
- ... exc=exc)
|
|
|
|
|
|
+ Calling the resulting task.
|
|
|
|
+
|
|
|
|
+ >>> refresh_feed("http://example.com/rss") # Regular
|
|
|
|
+ <Feed: http://example.com/rss>
|
|
|
|
+ >>> refresh_feed.delay("http://example.com/rss") # Async
|
|
|
|
+ <AsyncResult: 8998d0f4-da0b-4669-ba03-d5ab5ac6ad5d>
|
|
|
|
|
|
|
|
|
|
"""
|
|
"""
|
|
@@ -34,20 +41,13 @@ def task(**options):
|
|
def _create_task_cls(fun):
|
|
def _create_task_cls(fun):
|
|
base = options.pop("base", Task)
|
|
base = options.pop("base", Task)
|
|
|
|
|
|
- cls_name = fun.__name__
|
|
|
|
-
|
|
|
|
|
|
+ @wraps(fun)
|
|
def run(self, *args, **kwargs):
|
|
def run(self, *args, **kwargs):
|
|
return fun(*args, **kwargs)
|
|
return fun(*args, **kwargs)
|
|
- run.__name__ = fun.__name__
|
|
|
|
run.argspec = getargspec(fun)
|
|
run.argspec = getargspec(fun)
|
|
|
|
|
|
- cls_dict = dict(options)
|
|
|
|
- cls_dict["run"] = run
|
|
|
|
- cls_dict["__module__"] = fun.__module__
|
|
|
|
-
|
|
|
|
- task = type(cls_name, (base, ), cls_dict)()
|
|
|
|
-
|
|
|
|
- return task
|
|
|
|
|
|
+ cls_dict = dict(options, run=run, __module__=fun.__module__)
|
|
|
|
+ return type(fun.__name__, (base, ), cls_dict)()
|
|
|
|
|
|
return _create_task_cls
|
|
return _create_task_cls
|
|
|
|
|
|
@@ -69,5 +69,5 @@ def periodic_task(**options):
|
|
logger.warn("Task running...")
|
|
logger.warn("Task running...")
|
|
|
|
|
|
"""
|
|
"""
|
|
- options["base"] = PeriodicTask
|
|
|
|
|
|
+ options.setdefault("base", PeriodicTask)
|
|
return task(**options)
|
|
return task(**options)
|