Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increase celery retry time each retry cycle

I do retries with celery like in the Docs-Example:

@task()
def add(x, y):
    try:
        ...
    except Exception, exc:
        add.retry(exc=exc, countdown=60)  # override the default and
                                          # retry in 1 minute

How can I increase the retry-countdown everytime the retry occurs for this job - e.g. 60 seconds, 2 minutes, 4 minutes and so on until the MaxRetriesExceeded is raised?

like image 942
Gregor Avatar asked Feb 27 '12 18:02

Gregor


People also ask

Does celery retry failed tasks?

Celery will stop retrying after 7 failed attempts and raise an exception.

What is Shared_task in celery?

The "shared_task" decorator allows creation of Celery tasks for reusable apps as it doesn't need the instance of the Celery app. It is also easier way to define a task as you don't need to import the Celery app instance.

What is bind true celery?

The bind argument means that the function will be a “bound method” so that you can access attributes and methods on the task type instance. See the docs. Follow this answer to receive notifications.


1 Answers

Here is a simple way to create bigger delay each time the task is evaluated. This value is updated by celery itself so you don't need to manage anything yourself.

@task()
def add(x, y):
    try:
        ...
    except Exception as exc:
        raise add.retry(exc=exc, countdown=60 * add.request.retries) 

Note: First task is repeated with countdown of 0. Because number of retries is 0 for the first run.

like image 196
Lamp town guy Avatar answered Sep 24 '22 18:09

Lamp town guy