[英]How to set celery task status when uncaught exception raised
I have celery task 我有芹菜任务
@app.task
def add(x, y):
try:
return call_another_function(x, y)
except ValueError as ex:
handle_value_error(ex)
I am handling ValueError
, but what if call_another_function
raise other exception like IOError
or IndexError
? 我正在处理ValueError
,但是如果call_another_function
引发其他异常(如IOError
或IndexError
怎么办?
Celery has any mechanism, where we can set like app.call_for_any_exception
and that method call if we not handle the exception in our code? Celery有任何机制,如果我们不处理代码中的异常,可以在其中设置app.call_for_any_exception
方法并调用该方法?
I just want to make sure, if there are any exception, my task should be in fail
state. 我只是想确保,如果有任何异常,我的任务应该处于fail
状态。
I am using celery v4.0.2
. 我正在使用celery v4.0.2
。
Add another except clause without any exceptions to catch: 添加另一个except子句来捕获:
try:
return call_another_function(x, y)
except ValueError as ex:
handle_value_error(ex)
except:
update_state(state=FAILED)
raise
If you want a solution for many task, you can use this way proposed in the documentation : 如果您想为许多任务提供解决方案,则可以使用文档中建议的这种方式:
class MyTask(celery.Task):
def on_failure(self, exc, task_id, args, kwargs, einfo):
self.update_state(state=FAILED)
@task(base=MyTask)
def add(x, y):
try:
return call_another_function(x, y)
except ValueError as ex:
handle_value_error(ex)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.