如何在Django中为celerybeat设置不同的工作日/周末时间表?

我如何在 celery beat 中以不同的方式在工作日和周末安排我的任务?

时间表在我的settings.py文件中设置如下

{
    "task_weekday": {
        "task": "tasks.my_regular_task",
        "schedule": crontab(minute="0-30", hour="4,5", day_of_week="mon-fri"),
        "options": {"queue": "queue_name"},
    },
    "task_weekend": {
        "task": "tasks.my_regular_task",
        "schedule": crontab(minute="0-5", hour="10,12", day_of_week="sat,sun"),
        "options": {"queue": "queue_name"},
    },
}

但是,当我设置它时,它在今天(2021 年 3 月 21 日星期日)运行工作日计划,而不是选择周末计划。

我将应用程序时区设置为'US/Pacific'并且CELERY_ENABLE_UTC设置为False

设置后,我看到以下日志条目,但它运行工作日任务计划。

[2021-03-21 17:57:50,082: DEBUG/MainProcess] Current schedule:

<ScheduleEntry: task_weekday tasks.my_regular_task() <crontab: 0-30 4,5 mon-fri * * (m/h/d/dM/MY)>

<ScheduleEntry: task_weekend tasks.my_regular_task() <crontab: 0-5 10,12 sat,sun * * (m/h/d/dM/MY)>

<ScheduleEntry: celery.backend_cleanup celery.backend_cleanup() <crontab: 0 4 * * * (m/h/d/dM/MY)>

我也尝试过每隔几分钟运行一次任务,以测试它选择哪个时间表并选择周末时间表:

{
    "task_weekday": {
        "task": "tasks.my_regular_task",
        "schedule": crontab(minute="*/2", hour="*", day_of_week="mon-fri"),
        "options": {"queue": "queue_name"},
    },
    "task_weekend": {
        "task": "tasks.my_regular_task",
        "schedule": crontab(minute="*/3", hour="*", day_of_week="sat,sun"),
        "options": {"queue": "queue_name"},
    },
}
[2021-03-21 18:03:27,075: DEBUG/MainProcess] Current schedule:

<ScheduleEntry: task_weekend tasks.my_regular_task() <crontab: */3 * sat,sun * * (m/h/d/dM/MY)>

<ScheduleEntry: task_weekday tasks.my_regular_task() <crontab: */2 * mon-fri * * (m/h/d/dM/MY)>

<ScheduleEntry: celery.backend_cleanup celery.backend_cleanup() <crontab: 0 4 * * * (m/h/d/dM/MY)>

[2021-03-21 18:03:27,076: DEBUG/MainProcess] beat: Ticking with max interval->5.00 minutes

[2021-03-21 18:03:27,080: DEBUG/MainProcess] beat: Waking up in 32.91 seconds.

[2021-03-21 18:04:00,024: DEBUG/MainProcess] beat: Synchronizing schedule...

2021-03-21 18:04:00,041 | INFO | Self | 736a6a98bd5d456f8a253b5790f5a8e0 | 1 | celery.beat | beat:apply_entry | 271 | Scheduler: Sending due task task_weekday (tasks.my_regular_task)

[2021-03-21 18:04:00,041: INFO/MainProcess] Scheduler: Sending due task task_weekday (tasks.my_regular_task)

[2021-03-21 18:04:00,060: DEBUG/MainProcess] tasks.my_regular_task sent. id->bdffda9e-ab8a-41dd-a3b1-7ce62a1ab669

以上是如何在Django中为celerybeat设置不同的工作日/周末时间表?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>