即使主要任务失败也可以运行和弦回调

编程入门 行业动态 更新时间:2024-10-19 20:40:21
本文介绍了即使主要任务失败也可以运行和弦回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

即使主要任务失败,是否也可以运行 chord 回调?

Is it possible to run a chord callback even if the main tasks failed?

我创建了一个和弦,添加了很多任务并为其注册了一个回调.我的问题是,如果其中一项任务失败,则不会触发回调,但是我希望以任何一种方式触发回调.

I've created a chord which I added a bunch of tasks and registered a callback to it. My problem, is that if one of the tasks fail the callback is not triggered, but I would like the callback to be triggered either way.

我尝试用si()注册回调(不变性)

I've tried to register the callback with si() (immutability)

callback = tasks.run_delete_rule.si([timestamp]) header = [tasks.run_update_rule.s(i, timestamp) for i in item_ids] result = chord(header)(callback)

我还尝试将参数ignore_result=True添加到两个任务装饰器中,但没有成功.

I also tried to add the param ignore_result=True to both tasks decorator, but no success.

推荐答案

从github问题#1881 回调函数设置了link_error选项,该选项带有任务名称列表,然后当和弦任务失败时,将执行link_error任务.

From the github issue #1881 if the callback has the link_error option set, which takes a list of tasks names, then when a task of the chord fails the link_error tasks are going to be executed.

@task(name='super_task.good') def good(): return True @task(name='super_task.raise_exception') def raise_exception(): raise ValueError('error') @task(name='super_task.callback') def callback(*args, **kwargs): logger.info('callback') logger.info(args) logger.info(kwargs) return 'finished' @task(name='super_task.error_callback') def error_callback(*args, **kwargs): logger.info('error_callback') logger.info(args) logger.info(kwargs) return 'error' >>> c = chord( [raise_exception.s(), good.s(), raise_exception.s()], callback.s().set(link_error=['super_task.error_callback']) ) >>> result = c()

这将执行和弦,并在您的celery日志中,您将看到raise_exception任务失败,而执行error_callback的操作将在args中接收callback的task_id.

This will execute the chord and in your celery log, you'll see the raise_exception task fail, and the execution of error_callback which will receive in it's args the task_id of callback.

此时,result的值将包含callback的AsyncResult实例,并且由于和弦中的错误传播到回调中,因此执行result.get()会引发任务和result.traceback的异常给您回溯.

At this point the value of result will contain the AsyncResultinstance of callback, and because in a chord the errors propagate to the callback doing result.get() will raise the exception of the tasks and result.traceback gives you the traceback.

如果要有一个回调,只需将和弦回调的名称传递给link_error

If you want to have a single callback, just pass the name of the chord callback to link_error

callback.s().set(link_error='super_task.callback')

注意

另一个选项是设置CELERY_CHORD_PROPAGATES = False,它将恢复为芹菜3.1之前的行为并始终执行回调.

Another options it's to set CELERY_CHORD_PROPAGATES = False which will revert to the pre celery 3.1 behavior and always execute the callback.

但这不是推荐的方法,因为您可以在github问题#1349

But this is not a recommended approach because as you can find in the github issue #1349

Celery 3.1定义如何处理和弦错误,以前的行为从未记录 还有更多的事故,因为它从来没有打算那样做.

Celery 3.1 defines how chord errors are handled, the previous behavior was never documented and more of an accident since it was never the intention to work that way.

我们无法更改错误修正版本中的行为,因此必须改用设置, 但是从来没有打算有人故意禁用新行为.

We couldn't change the behavior in a bugfix release so a setting had to be used instead, but it was never the intention that someone would deliberately disable the new behavior.

这里有新的行为可以防止发生此类问题,并且可以删除向后兼容的设置.我建议您在这里找到其他方法来处理错误(如果您可以为此发明一个不错的api,我也不会介意一个提案)

The new behavior is there to protect against this sort of issue happening, and the backward compatible setting may be removed. I suggest you find some other way to handle errors here (and I wouldn't mind a proposal if you can invent a nice api for it)

更多推荐

即使主要任务失败也可以运行和弦回调

本文发布于:2023-11-13 00:20:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582892.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:回调   主要任务   和弦

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!