如果它的子任务给出异常,如何使链失败

时间:2012-09-30 11:44:00

标签: python django rabbitmq celery

芹菜面临着一个非常奇怪的问题:

有一系列任务,其中一个给出异常并进行多次重试

chain = (err.si(1) | err.si(2))
result = chain.apply_async()
result.state
result.get()

这是任务的代码:

@celery.task(base=MyTask)
def err(x):
try:
    if x < 3:
        raise Exception
    else:
        return x+1

except Exception as exp:
    print "retrying"
    raise err.retry(args=[x],exc=exp,countdown=5,max_retries=3)

问题是虽然链中的任务给出了异常,但result.state仍然是'PENDING'而.get()只是冻结。

我试图在任务达到最大重试次数值时失败:

class MyTask(celery.Task):
abstract = True
def after_return(self, status, retval, task_id, args, kwargs, einfo):
    if self.max_retries == self.request.retries:
        self.state = states.FAILURE

但是虽然单独执行的任务被标记为FAILED,但在链中执行会产生相同的结果 - PENDING&amp;冻结得到。

我预计,一旦任何任务的任务失败,链就会失败。结果的结果应该产生从任务抛出的异常。

_ 更新 _ APPL_async给出的堆栈跟踪,ALWAYS_EAGER = True

result = chain.apply_async()

Exception                                 
Traceback (most recent call last)
<ipython-input-4-81202b369b5f> in <module>()
----> 1 result = chain.apply_async()

lib/python2.7/site-packages/celery/canvas.pyc in apply_async(self, args, kwargs,     **options)
    147         # For callbacks: extra args are prepended to the stored args.
    148         args, kwargs, options = self._merge(args, kwargs, options)
--> 149         return self.type.apply_async(args, kwargs, **options)
    150 
    151     def append_to_list_option(self, key, value):

/lib/python2.7/site-packages/celery/app/builtins.pyc in apply_async(self, args, kwargs, group_id, chord, task_id, **options)
    232                 task_id=None, **options):
    233             if self.app.conf.CELERY_ALWAYS_EAGER:
--> 234                 return self.apply(args, kwargs, **options)
    235             options.pop('publisher', None)
    236             tasks, results = self.prepare_steps(args, kwargs['tasks'])

lib/python2.7/site-packages/celery/app/builtins.pyc in apply(self, args, kwargs, subtask, **options)
    249             last, fargs = None, args  # fargs passed to first task only
    250             for task in kwargs['tasks']:
--> 251                 res = subtask(task).clone(fargs).apply(last and (last.get(), ))
    252                 res.parent, last, fargs = last, res, None
    253             return last

lib/python2.7/site-packages/celery/result.pyc in get(self, timeout, propagate, **kwargs)
    677         elif self.state in states.PROPAGATE_STATES:
    678             if propagate:
--> 679                 raise self.result
    680             return self.result
    681     wait = get

Exception: 

2 个答案:

答案 0 :(得分:13)

当你有一个链:

>>> c = a.s() | b.s() | c.s()
>>> res = c()
>>> res.get()

调用链将为链中的所有任务生成唯一的id,发送消息并返回链中的最后结果

所以当你res.get()时,你很容易找到链中最后一个任务的结果。

它还将使用parent属性修饰结果,您可以遍历这些属性来获取链的进度:

>>> res                # result of c.s()
>>> res.parent         # result of b.s()
>>> res.parent.parent  # result of a.s()

如果您想沿着以下方式检查错误:

def nodes(node):
    while node.parent:
        yield node
        node = node.parent
    yield node


values = [node.get(timeout=1) for node in reversed(list(nodes(res)))]
value = values[-1]

答案 1 :(得分:0)

实际上我认为你不应该在这里使用raise

您正在抛出异常,当documentation says you shouldn't时,您可能只想使用err.retry而不是raise err.retry