Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up different weekday/weekend schedules for celery beat in Django?

How do I go about scheduling my tasks differently for weekdays and weekends in celery beat?

The schedule is set as follows in my settings.py file

{
    "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"},
    },
}

However when I set it up, it ran the weekday schedule today (3/21/2021 Sunday) instead of picking up the weekend schedule.

I have the app timezone set to 'US/Pacific' and the CELERY_ENABLE_UTC is set to False.

After setting it up I see the following log entry but it runs the weekday tasks schedule.

[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)>

I have tried running the tasks every few minutes as well to test which schedule it picks up and picks up the weekend schedule:

{
    "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
like image 296
ChickenNugget Avatar asked Mar 22 '21 01:03

ChickenNugget


1 Answers

task_name is a unique index. In table periodictask, a task-name can be there only once. So your two job submissions mean the second one is overwriting the first.

You could make a task function my_regualar_task_weekend() which simply calls my_regular_task(), and then apply the weekend schedule to new task.

like image 113
Tim Richardson Avatar answered Nov 09 '22 09:11

Tim Richardson