18

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?

Chillar Anand
  • 27,936
  • 9
  • 119
  • 136
Gregor
  • 4,306
  • 1
  • 22
  • 37

3 Answers3

35

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.

Lamp town guy
  • 1,489
  • 1
  • 16
  • 23
10

Since version 4.2 you can use options autoretry_for and retry_backoff for this purposes, for example:

@task(max_retries=10, autoretry_for=(Exception,), retry_backoff=60)
def add(x, y):
    pass
Anton Shurashov
  • 1,820
  • 1
  • 26
  • 39
7

Keep a variable with your last retry time in it, and multiply it by 2 each time until it exceeds whatever level you want (or, keep a count if you prefer a certain number of times...)

Perry
  • 4,363
  • 1
  • 17
  • 20
  • So there is no elegant/magic way to do this instead of passing a retry-counter to the method on every retry? – Gregor Feb 27 '12 at 18:34
  • I'm insufficiently familiar with celery, but if you've read the manual and haven't found it, this seems like an easy way to accomplish what you want with only two or three lines of code, which seems easy enough. – Perry Feb 27 '12 at 18:36
  • @Gregor, I think this is elegant, but maybe we could add an retry_step as well. You could create an issue at http://github.com/ask/issue to request such a feature. – asksol Feb 27 '12 at 20:25
  • 17
    Oh, btw, you don't have to pass a parameter as the number of retries is available: `interval, step = 60, 60; add.retry(countdown=interval + (step * add.request.retries)` – asksol Feb 27 '12 at 20:27
  • Thanks asksol, I knew that this counter must be living somewhere, how else would celery raise the MaxRetriesExceeded exception after #retries. – Gregor Feb 28 '12 at 13:15