task-retries.rst 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. ================
  2. Retrying Tasks
  3. ================
  4. Retrying a task if something fails
  5. ==================================
  6. Simply use :meth:`celery.task.base.Task.retry` to re-sent the task, it will
  7. do the right thing, and respect the :attr:`celery.task.base.Task.max_retries`
  8. attribute:
  9. .. code-block:: python
  10. class SendTwitterStatusTask(Task):
  11. def run(self, oauth, tweet, **kwargs):
  12. try:
  13. twitter = Twitter(oauth)
  14. twitter.update_status(tweet)
  15. except (Twitter.FailWhaleError, Twitter.LoginError), exc:
  16. self.retry(args=[oauth, tweet], exc=exc, **kwargs)
  17. Here we used the ``exc`` argument to pass the current exception to
  18. :meth:`celery.task.base.Task.retry`. At each step of the retry this exception
  19. is available as the tombstone (result) of the task, when
  20. :attr:`celery.task.base.Task.max_retries` has been exceeded this is the exception
  21. raised. However, if an ``exc`` argument is not provided the
  22. :exc:`celery.task.base.RetryTaskError` exception is raised instead.
  23. Setting a custom delay for retries.
  24. ===================================
  25. The default countdown is in the tasks
  26. :attr:`celery.task.base.Task.default_retry_delay` attribute, which by
  27. default is set to 3 minutes.
  28. You can also provide the ``countdown`` argument to
  29. :meth:`celery.task.base.Task.retry` to override this default.
  30. .. code-block:: python
  31. class MyTask(Task):
  32. default_retry_delay = 30 * 60 # retry in 30 minutes
  33. def run(self, x, y, **kwargs):
  34. try:
  35. ...
  36. except Exception, exc:
  37. self.retry([x, y], exc=exc,
  38. countdown=60 # override the default and
  39. # retry in 1 minute
  40. **kwargs)