Django Celery Beat 管理员更新 Cron Schedule 定期任务未生效 [英] Django Celery Beat admin updating Cron Schedule Periodic task not taking effect

查看:70
本文介绍了Django Celery Beat 管理员更新 Cron Schedule 定期任务未生效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 CentOS 7 上使用 Django 10、RabbitMQ 和 Celery 4 运行一个站点.

I'm running a site using Django 10, RabbitMQ, and Celery 4 on CentOS 7.

我的 Celery Beat 和 Celery Worker 实例由主管控制,我正在使用 django celery 数据库调度程序.

My Celery Beat and Celery Worker instances are controlled by supervisor and I'm using the django celery database scheduler.

我已经使用 Django-admin 中的 cronsheduler 安排了一个 cron 样式的任务.

I've scheduled a cron style task using the cronsheduler in Django-admin.

当我启动 celery beat 和 worker 实例时,作业会按预期触发.

When I start celery beat and worker instances the job fires as expected.

但是,如果在 Django-admin 中更改了计划时间,那么除非我重新启动 celery-beat 实例,否则这些更改不会生效.

But if a change the schedule time in Django-admin then the changes are not picked up unless I restart the celery-beat instance.

我有什么遗漏或者我需要编写自己的调度程序吗?

Is there something I am missing or do I need to write my own scheduler?

Celery Beat,使用 'django_celery_beat.schedulers.DatabaseScheduler' 从数据库加载时间表.根据以下文档 https://media.readthedocs.org/pdf/django-celery-beat/latest/django-celery-beat.pdf 这应该会强制 Celery Beat 重新加载:

Celery Beat, with the 'django_celery_beat.schedulers.DatabaseScheduler' loads the schedule from the database. According to the following doc https://media.readthedocs.org/pdf/django-celery-beat/latest/django-celery-beat.pdf this should force Celery Beat to reload:

以特定间隔(例如每 5 秒)运行的计划.•django_celery_beat.models.CrontabSchedule一个日程和字段喜欢条目在cron:分钟 小时 星期几 day_of_month month_of_year.

A schedule that runs at a specific interval (e.g. every 5 seconds). • django_celery_beat.models.CrontabSchedule A schedule with fields like entries in cron: minute hour day-of-week day_of_month month_of_year.

django_celery_beat.models.PeriodicTasks此模型仅用作跟踪计划何时更改的索引.每当您更新 PeriodicTask 时,此表中的计数器也会增加,这告诉 celery beat服务以从数据库重新加载计划.如果您批量更新定期任务,则需要手动更新计数器:

django_celery_beat.models.PeriodicTasks This model is only used as an index to keep track of when the schedule has changed. Whenever you update a PeriodicTask a counter in this table is also incremented, which tells the celery beat service to reload the schedule from the database. If you update periodic tasks in bulk, you will need to update the counter manually:

from django_celery_beat.models import PeriodicTasks
PeriodicTasks.changed()

从上面我希望 Celery Beat 进程定期检查表是否有任何变化.

From the above I would expect the Celery Beat process to check the table regularly for any changes.

推荐答案

我有一个解决方案:

  1. 创建一个使用 RabbitMQ 队列的单独工作进程.
  2. 当 Django 更新数据库时,它会向队列发布一条消息,其中包含 Celery Beat 进程的名称(名称由主管配置定义).
  3. 工作进程然后重新启动命名的 Celery Beat 进程.

有点啰嗦,但确实可以.还可以更轻松地管理同一服务器上需要相同功能的多个 Django 应用程序.

A bit long winded but does the job. Also makes it easier to manage multiple Django apps on the same server that require the same functionality.

这篇关于Django Celery Beat 管理员更新 Cron Schedule 定期任务未生效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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