同一任务多次执行 [英] Same task executed multiple times

查看:55
本文介绍了同一任务多次执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些ETA任务,这些任务已发送给Celery的Redis经纪人.这是一个芹菜和Redis实例,都在同一台机器上.

I have ETA tasks that get sent to a Redis broker for Celery. It is a single celery and redis instance, both int he same machine.

问题是,任务被多次执行.我已经看到任务执行了4到11次.

The problem is, tasks are getting executed multiple times. I've seen tasks executed 4 to 11 times.

我将可见性超时设置为12小时,因为我的ETA在4-11小时之间(在运行时确定):

I set up the visibility timeout to be 12 hours, given that my ETA's are between 4-11 hours (determined at runtime):

BROKER_TRANSPORT_OPTIONS = {'visibility_timeout': 12 * 60 * 60}

即使如此,任务仍然会多次执行.

Even with that, tasks still get executed multiple times.

最初,有问题的任务不是幂等的,所以我尝试添加数据库检查以使其成为幂等的.

Initially, the task in question was not idempotent, so I tried adding in a DB check to make them idempotent.

它看起来像这样:

@app.task
def foo(side_effect_action):
    if side_effect_action.executed:
        return ALREADY_EXECUTED
    else:
        do_side_effect()
        side_effect_action.executed = True
        side_effect_action.save() #hits the db
        return JUST_EXECUTED

结果表明,芹菜工作者在foo能够调用 side_effect_action.save()并保存状态之前就开始执行任务,因此在所有情况下,当它正在寻找 side_effect_action.exected时,它仍然是False,因此被执行了多次.

Turns out that the celery worker gets to the task before foo is able to call side_effect_action.save() and save the state, so in all cases when it's looking for side_effect_action.executed it is still False, and thus gets executed multiple times.

任何想法如何解决此问题?

Any ideas how can I solve this issue?

推荐答案

为了避免这个问题,我将芹菜经纪人切换到RabbitMQ.不幸的是,因为现在我的Web应用程序中还有一个组件(我仍然需要redis来完成其他事情),但是它确实解决了ETA任务多次执行错误.

I switched my celery broker to RabbitMQ to avoid this issue. It is unfortunate since I now have one more component in my webapp (I still need redis for something else), but it did solve the multiple execution for ETA tasks bug.

这篇关于同一任务多次执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆