芹菜中重复的任务

Tasks being repeated in Celery

几天后,我的芹菜服务将无限期地重复执行一项任务。这有点难以重现,但每周定期发生一次或更频繁,具体取决于正在处理的任务量。

对于如何获取有关此问题的更多数据的任何提示,我将不胜感激,因为我不知道如何跟踪它。出现时,重启celery会暂时解决。

我有一个 celery 节点 运行 4 个工人(版本 3.1.23)。 Broker 和结果后端在 Redis 上。我只发布到一个队列,我不使用 celery beat。

Django 的 setting.py 中的配置是:

BROKER_URL = 'redis://localhost:6380'
CELERY_RESULT_BACKEND = 'redis://localhost:6380'

日志相关部分:

[2016-05-28 10:37:21,957: INFO/MainProcess] Received task: painel.tasks.indicar_cliente[defc87bc-5dd5-4857-9e45-d2a43aeb2647]
[2016-05-28 11:37:58,005: INFO/MainProcess] Received task: painel.tasks.indicar_cliente[defc87bc-5dd5-4857-9e45-d2a43aeb2647]
[2016-05-28 13:37:59,147: INFO/MainProcess] Received task: painel.tasks.indicar_cliente[defc87bc-5dd5-4857-9e45-d2a43aeb2647]
...
[2016-05-30 09:27:47,136: INFO/MainProcess] Task painel.tasks.indicar_cliente[defc87bc-5dd5-4857-9e45-d2a43aeb2647] succeeded in 53.33468166703824s: None
[2016-05-30 09:43:08,317: INFO/MainProcess] Task painel.tasks.indicar_cliente[defc87bc-5dd5-4857-9e45-d2a43aeb2647] succeeded in 466.0324719119817s: None
[2016-05-30 09:57:25,550: INFO/MainProcess] Task painel.tasks.indicar_cliente[defc87bc-5dd5-4857-9e45-d2a43aeb2647] succeeded in 642.7634702899959s: None

任务由用户请求发送:

tasks.indicar_cliente.delay(indicacao_db.id)

这是 source code of the task and the celery service configuration

为什么在服务 运行 一段时间后多次收到任务?我怎样才能获得一致的行为?

通过使用 rabbitmq 代理而不是 redis 解决了。

它可能有点过时了,但我遇到了同样的问题并用 Redis 修复了它。长话短说,Celery 会等待一段时间执行任务,如果时间到了,它会重新启动任务。这称为可见性超时。 来自文档的解释:

If a task isn’t acknowledged within the Visibility Timeout the task will be redelivered to another worker and executed. This causes problems with ETA/countdown/retry tasks where the time to execute exceeds the visibility timeout; in fact if that happens it will be executed again, and again in a loop. So you have to increase the visibility timeout to match the time of the longest ETA you’re planning to use. Note that Celery will redeliver messages at worker shutdown, so having a long visibility timeout will only delay the redelivery of ‘lost’ tasks in the event of a power failure or forcefully terminated workers.

选项示例: https://docs.celeryproject.org/en/stable/userguide/configuration.html#broker-transport-options

详情: https://docs.celeryproject.org/en/stable/getting-started/brokers/redis.html#visibility-timeout

我运行遇到了这样的问题。种芹菜 visibility timeout 没用。

事实证明,我也是 运行 Prometheus 导出器,它实例化了自己的 Celery 对象,该对象使用默认的可见性超时——因此取消了我在应用程序中设置的更高超时。

如果您有多个 Celery 客户端——无论它们是用于提交任务、处理任务,还是只是观察任务——请确保它们都具有完全相同的配置。