简体   繁体   English

Celery重新启动损失计划任务

[英]Celery restart loss scheduled tasks

I use Celery to schedule the sending of emails in the future. 我使用Celery安排将来发送电子邮件的时间。 I put the task in celery with apply_async() and ETA setted sometimes in the future. 我将任务与apply_async()一起放入芹菜中,并且将来可能会设置ETA。

When I look in flower I see that all tasks scheduled for the future has status RECEIVED. 当我看着花时,我看到所有计划在将来执行的任务的状态为已接收。

If I restart celery all tasks are gone. 如果我重新启动celery,所有任务都消失了。 Why they are gone? 他们为什么走了?

I use redis as a broker. 我使用redis作为经纪人。

EDIT1 编辑1

In documentation I found: 在文档中,我发现:

If a task is not 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; 这会导致ETA /倒数/重试任务出现问题,其中执行时间超过了可见性超时; 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 are planning to use. 因此,您必须增加可见性超时,以匹配计划使用的最长ETA的时间。

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. 请注意,Celery会在工作人员关闭时重新传递消息,因此可见性超时时间长只会在电源故障或强制终止工作人员的情况下延迟重新发送“丢失”的任务。

Periodic tasks will not be affected by the visibility timeout, as this is a concept separate from ETA/countdown. 定期任务不会受到可见性超时的影响,因为这是与ETA /倒计时不同的概念。

You can increase this timeout by configuring a transport option with the same name: 您可以通过配置具有相同名称的传输选项来增加此超时:

BROKER_TRANSPORT_OPTIONS = {'visibility_timeout': 43200} BROKER_TRANSPORT_OPTIONS = {'visibility_timeout':43200}

The value must be an int describing the number of seconds. 该值必须是描述秒数的整数。

But the ETA of my tasks can be measured in months or years. 但是我的任务的预计到达时间可以用几个月或几年来衡量。

EDIT 2 编辑2

This is what I get when I type: 这是我输入时得到的:

$ celery -A app inspect scheduled $ celery-已安排应用检查

{u'priority': 6, u'eta': u'2015-11-22T11:53:00-08:00', u'request': {u'args': u'(16426,)', u'time_start': None, u'name': u'core.tasks.action_du e', u'delivery_info': {u'priority': 0, u'redelivered': None, u'routing_key': u'celery', u'exchange': u'celery'}, u'hostname': u'celery@app.myplanmap.com', u'ack nowledged': False, u'kwargs': u'{}', u'id': u'8ac59984-f8d0-47ae-ac9e-c4e3ea9c4a c6', u'worker_pid': None}} {u'priority':6,u'eta':u'2015-11-22T11:53:00-08:00',u'request':{u'args':u'(16426,)',u 'time_start':无,u'name':u'core.tasks.action_du e',u'delivery_info':{u'priority':0,u'redelivered':无,u'routing_key':u'celery' ,u'exchange':u'celery'},u'hostname':u'celery@app.myplanmap.com',u'ack nowledged':False,u'kwargs':u'{}',u'id ':u'8ac59984-f8d0-47ae-ac9e-c4e3ea9c4a c6',u'worker_pid':无}}

If you look closely, task wasn't acknowledged yet, so it should stay in redis after celery restart, right? 如果仔细观察,任务尚未被确认,因此在重新启动芹菜后应保留在redis中,对吗?

you have to use RabbitMq instead redis. 您必须使用RabbitMq代替redis。

RabbitMQ is feature-complete, stable, durable and easy to install. RabbitMQ功能齐全,稳定,耐用且易于安装。 It's an excellent choice for a production environment. 这是生产环境的绝佳选择。

Redis is also feature-complete, but is more susceptible to data loss in the event of abrupt termination or power failures. Redis的功能也很完善,但是在突然终止或断电的情况下更容易丢失数据。

Using rabbit mq your problem of lossing message on restart have to gone. 使用rabbit mq,您在重启时丢失消息的问题就不必解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM