设置芹菜任务失败时的结果

2024-04-16 17:22:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在芹菜任务失败时设置一个结果。我希望能够根据处理任务时发生的错误类型显示定制的消息。正如我所检查的,将任务定义为失败的唯一方法是在任务期间引发异常。如果任务到达最后的代码行,即使我尝试手动更改,状态始终为“成功”。我的结果是使用Redis 我曾想过在错误回调上查询任务id,但这样做似乎是错误的。以下是我如何称呼该任务:

run_long_process.apply_async(args=(instance.id, ),
                                                     link=commit_instances.s(),
                                                     link_error=rollback_instances.s(),
                                                     )

任务包括:

@shared_task(bind=True)
def run_long_process(self, some_parm):
    .
    .
    .
    if some_condition:
        // I tried here to do something like self.update_state(state='FAILURE') without
        // raising the exception
        raise CustomException("Error")

    return {some_response}

@shared_task
def rollback_instances(request, exc, traceback):
   //I want to set a message or something and when I query the task id I get the result with 
   //state FAILURE  
   doSomethingElse()


@shared_task
def commit_instances(*args, **kwargs):
    doSomething()

如果有的话,有没有一种类似芹菜的方法来实现这一点


Tags: theinstances方法runidtaskdef错误