几天后,我的 celery 服务将无限期地重复执行一项任务。这有点难以重现,但每周定期发生一次或更频繁,具体取决于正在处理的任务量。
对于如何获取有关此问题的更多数据的任何提示,我将不胜感激,因为我不知道如何追踪它。出现时,重启celery会暂时解决。
我有一个 celery 节点与 4 个 worker 一起运行(版本 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和 celery service configuration .
为什么服务运行一段时间后会多次收到任务?我怎样才能获得一致的行为?
最佳答案
它可能有点过时,但我遇到了同样的问题并用 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
关于python - celery 中重复的任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37531811/