【问题标题】:How to set up different weekday/weekend schedules for celery beat in Django?如何在 Django 中为 celery beat 设置不同的工作日/周末时间表?
【发布时间】:2021-06-18 16:37:31
【问题描述】:

如何在 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 cron celery django-celery celerybeat


    【解决方案1】:

    task_name 是唯一索引。在表periodictask中,一个task-name只能出现一次。 因此,您提交的两个作业意味着第二个将覆盖第一个。

    您可以创建一个任务函数my_regualar_task_weekend(),它只调用my_regular_task(),然后将周末计划应用于新任务。

    【讨论】:

      最近更新 更多