Retrying tasks with Django-Celery - Django/Celery

15,484

Solution 1

The task needs to accept keyword arguments, they are used to pass information amongst other about the retry count. I think the code should look like this:

from celery.decorators import task

@task()
def add(x, y, **kwargs):
    if not x or not y:
        try:
            raise Exception("test error")
        except Exception, e:
            add.retry(args=[x, y], exc=e, countdown=30, kwargs=kwargs)
    return x+y

**kwargs need to be added to the signature of the add function, and passed as kwargs=kwargs when calling retry.

Note: this style was deprecated with the release of celery 2.2.

Solution 2

You can set your retry parameters in the decorator:

@task(default_retry_delay=5 * 60, max_retries=12)
def foo(bar):
  try:
      ...
  except Exception, exc:
      raise foo.retry(exc=exc)
Share:
15,484

Related videos on Youtube

RadiantHex
Author by

RadiantHex

hello! :)

Updated on October 17, 2020

Comments

  • RadiantHex
    RadiantHex over 3 years

    I'm having problems retrying tasks, here is what a test task looks like

    from celery.decorators import task
    
    @task()
    def add(x, y):
        if not x or not y:
            raise Exception("test error")
        return x+y
    

    I cannot find any documentation what-so-ever on how to retry decorated tasks, all I found was this:

    self.retry(x,y, exc=exception, countdown=30)
    

    which doesn't seem to work with my case as there is not self variable being passed from the method.

    Edit:

    I'm trying the following now to no avail:

    from celery.decorators import task
    
    @task()
    def add(x, y):
        if not x or not y:
            try:
                raise Exception("test error")
            except Exception, e:
                add.retry([x, y], exc=e, countdown=30)
        return x+y
    

    I get the following error:

    TypeError("kwargs argument to retries can't be empty. Task must accept **kwargs, see http://bit.ly/cAx3Bg",)

    • asksol
      asksol over 10 years
      There is no self because your task is not bound. This is a new concept in Celery 3.1: @task(bind=True) def add(self, x, y):. If you use an earlier version you must reference the name of the task: add.retry(...).
    • asksol
      asksol over 10 years
      Also, you get the last error because you didn't specify keyword arguments for the task: add.retry([x, y], {}, exc=e, countdown=30) ought to work, but you don't have to specify x` and y here (unless the function changed their values), because retry` will automatically use the arguments used to invoke the task: add.retry(exc=e, countdown=30).
  • hendrixski
    hendrixski over 12 years
    Yup, in the latest version it seems that it's good enough to just do add.retry(exc=e, countdown=30) ... where add is replaced with whatever the name of the decorated task/method is.