app.celery.ContextTask.retry

ContextTask.retry(args=None, kwargs=None, exc=None, throw=True, eta=None, countdown=None, max_retries=None, **options)

Retry the task, adding it to the back of the queue.

Example

>>> from imaginary_twitter_lib import Twitter
>>> from proj.celery import app
>>> @app.task(bind=True)
... def tweet(self, auth, message):
...     twitter = Twitter(oauth=auth)
...     try:
...         twitter.post_status_update(message)
...     except twitter.FailWhale as exc:
...         # Retry in 5 minutes.
...         self.retry(countdown=60 * 5, exc=exc)

Note

Although the task will never return above as retry raises an exception to notify the worker, we use raise in front of the retry to convey that the rest of the block won’t be executed.

Parameters
  • args (Tuple) – Positional arguments to retry with.

  • kwargs (Dict) – Keyword arguments to retry with.

  • exc (Exception) –

    Custom exception to report when the max retry limit has been exceeded (default: @MaxRetriesExceededError).

    If this argument is set and retry is called while an exception was raised (sys.exc_info() is set) it will attempt to re-raise the current exception.

    If no exception was raised it will raise the exc argument provided.

  • countdown (float) – Time in seconds to delay the retry for.

  • eta (datetime) – Explicit time and date to run the retry at.

  • max_retries (int) – If set, overrides the default retry limit for this execution. Changes to this parameter don’t propagate to subsequent task retry attempts. A value of None, means “use the default”, so if you want infinite retries you’d have to set the max_retries attribute of the task to None first.

  • time_limit (int) – If set, overrides the default time limit.

  • soft_time_limit (int) – If set, overrides the default soft time limit.

  • throw (bool) – If this is False, don’t raise the @Retry exception, that tells the worker to mark the task as being retried. Note that this means the task will be marked as failed if the task raises an exception, or successful if it returns after the retry call.

  • **options (Any) – Extra options to pass on to apply_async().

Raises

celery.exceptions.Retry – To tell the worker that the task has been re-sent for retry. This always happens, unless the throw keyword argument has been explicitly set to False, and is considered normal operation.