Browse Source

Documentation now uses 'raise task.retry()'

Ask Solem 13 years ago
parent
commit
7e43c5583d
4 changed files with 16 additions and 19 deletions
  1. 8 7
      celery/app/task.py
  2. 4 8
      celery/tests/tasks/test_tasks.py
  3. 1 1
      docs/userguide/groups.rst
  4. 3 3
      docs/userguide/tasks.rst

+ 8 - 7
celery/app/task.py

@@ -613,7 +613,7 @@ class BaseTask(object):
             ...         twitter.post_status_update(message)
             ...     except twitter.FailWhale, exc:
             ...         # Retry in 5 minutes.
-            ...         return tweet.retry(countdown=60 * 5, exc=exc)
+            ...         raise tweet.retry(countdown=60 * 5, exc=exc)
 
         Although the task will never return above as `retry` raises an
         exception to notify the worker, we use `return` in front of the retry
@@ -654,13 +654,14 @@ class BaseTask(object):
         # If task was executed eagerly using apply(),
         # then the retry must also be executed eagerly.
         if request.is_eager:
-            return self.apply(args=args, kwargs=kwargs, **options).get()
-
-        self.apply_async(args=args, kwargs=kwargs, **options)
+            self.apply(args=args, kwargs=kwargs, **options).get()
+        else:
+            self.apply_async(args=args, kwargs=kwargs, **options)
+        ret = RetryTaskError(eta and "Retry at %s" % eta
+                                  or "Retry in %s secs." % countdown, exc)
         if throw:
-            raise RetryTaskError(
-                eta and "Retry at %s" % (eta, )
-                     or "Retry in %s secs." % (countdown, ), exc)
+            raise ret
+        return ret
 
     def apply(self, args=None, kwargs=None, **options):
         """Execute this task locally, by blocking until the task returns.

+ 4 - 8
celery/tests/tasks/test_tasks.py

@@ -64,7 +64,7 @@ def retry_task(arg1, arg2, kwarg=1, max_retries=None, care=True):
     if care and retries >= rmax:
         return arg1
     else:
-        return current.retry(countdown=0, max_retries=rmax)
+        raise current.retry(countdown=0, max_retries=rmax)
 
 
 @task.task(max_retries=3, iterations=0)
@@ -75,7 +75,7 @@ def retry_task_noargs(**kwargs):
     if retries >= 3:
         return 42
     else:
-        return current.retry(countdown=0)
+        raise current.retry(countdown=0)
 
 
 @task.task(max_retries=3, iterations=0, base=MockApplyTask)
@@ -87,7 +87,7 @@ def retry_task_mockapply(arg1, arg2, kwarg=1, **kwargs):
         return arg1
     else:
         kwargs.update(kwarg=kwarg)
-    return current.retry(countdown=0)
+    raise current.retry(countdown=0)
 
 
 class MyCustomException(Exception):
@@ -106,7 +106,7 @@ def retry_task_customexc(arg1, arg2, kwarg=1, **kwargs):
             raise MyCustomException("Elaine Marie Benes")
         except MyCustomException, exc:
             kwargs.update(kwarg=kwarg)
-            return current.retry(countdown=0, exc=exc)
+            raise current.retry(countdown=0, exc=exc)
 
 
 class test_task_retries(Case):
@@ -115,20 +115,17 @@ class test_task_retries(Case):
         retry_task.__class__.max_retries = 3
         retry_task.iterations = 0
         result = retry_task.apply([0xFF, 0xFFFF])
-        self.assertEqual(result.get(), 0xFF)
         self.assertEqual(retry_task.iterations, 4)
 
         retry_task.__class__.max_retries = 3
         retry_task.iterations = 0
         result = retry_task.apply([0xFF, 0xFFFF], {"max_retries": 10})
-        self.assertEqual(result.get(), 0xFF)
         self.assertEqual(retry_task.iterations, 11)
 
     def test_retry_no_args(self):
         retry_task_noargs.__class__.max_retries = 3
         retry_task_noargs.iterations = 0
         result = retry_task_noargs.apply()
-        self.assertEqual(result.get(), 42)
         self.assertEqual(retry_task_noargs.iterations, 4)
 
     def test_retry_kwargs_can_be_empty(self):
@@ -158,7 +155,6 @@ class test_task_retries(Case):
         retry_task_customexc.__class__.max_retries = 3
         retry_task_customexc.iterations = 0
         result = retry_task_customexc.apply([0xFF, 0xFFFF], {"kwarg": 0xF})
-        self.assertEqual(result.get(), 0xFF + 0xF)
         self.assertEqual(retry_task_customexc.iterations, 4)
 
     def test_retry_with_custom_exception(self):

+ 1 - 1
docs/userguide/groups.rst

@@ -247,7 +247,7 @@ Example implementation:
     def unlock_chord(taskset, callback, interval=1, max_retries=None):
         if taskset.ready():
             return subtask(callback).delay(taskset.join())
-        unlock_chord.retry(countdown=interval, max_retries=max_retries)
+        raise unlock_chord.retry(countdown=interval, max_retries=max_retries)
 
 
 This is used by all result backends except Redis and Memcached, which increment a

+ 3 - 3
docs/userguide/tasks.rst

@@ -133,7 +133,7 @@ of temporary failure.
             twitter = Twitter(oauth)
             twitter.update_status(tweet)
         except (Twitter.FailWhaleError, Twitter.LoginError), exc:
-            send_twitter_status.retry(exc=exc)
+            raise send_twitter_status.retry(exc=exc)
 
 Here we used the `exc` argument to pass the current exception to
 :meth:`@-Task.retry`. At each step of the retry this exception
@@ -173,8 +173,8 @@ override this default.
         try:
             ...
         except Exception, exc:
-            add.retry(exc=exc, countdown=60)  # override the default and
-                                              # retry in 1 minute
+            raise add.retry(exc=exc, countdown=60)  # override the default and
+                                                    # retry in 1 minute
 
 .. _task-options: