用Django-芹菜- Django/芹菜重新尝试任务。

时间:2021-02-07 19:34:39

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",)

类型错误(“kwargs参数重试不能为空”。任务必须接受**kwargs,见http://bit.ly/cAx3Bg”。


Any ideas?

什么好主意吗?

2 个解决方案

#1


15  

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.

**kwargs需要添加到add函数的签名中,并在调用重试时通过kwargs=kwargs。

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

注意:这种风格是在芹菜2.2的发布中被弃用的。

#2


26  

You can set your retry parameters in the decorator:

您可以在decorator中设置重试参数:

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

#1


15  

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.

**kwargs需要添加到add函数的签名中,并在调用重试时通过kwargs=kwargs。

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

注意:这种风格是在芹菜2.2的发布中被弃用的。

#2


26  

You can set your retry parameters in the decorator:

您可以在decorator中设置重试参数:

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