Search code examples
pythondjangocelerydjango-celerycelerybeat

How to schedule weekday-aware jobs in celery


Is it possible to configure a sophisticated schedule with celery beat? For example, something like this:

On Monday-Friday, do job A with parameters (x1, y1), then do job B On Saturday, Sunday, do job A with parameters (x2, y2), don't do job B

I know I can implement a high frequency "tick" task that will check for this schedule, but I don't want to reinvent the wheel if something for this already exists.


Solution

  • Sure, use a crontab schedule for your tasks.

    from celery.schedules import crontab
    
    CELERYBEAT_SCHEDULE = {
        # Executes every weekday morning at 7:30 A.M
        'weekdays': {
            'task': 'tasks.A',
            'schedule': crontab(hour=7, minute=30, day_of_week='1-5'),
            'args': (x1, y1),
        },
        # Executes saturday at 4:00 A.M
        'saturday': {
            'task': 'tasks.B',
            'schedule': crontab(hour=4, minute=0, day_of_week='sat'),
            'args': (x1, y1),
        },
        # Executes sunday morning at 2:15 A.M
        'sunday': {
            'task': 'tasks.A',
            'schedule': crontab(hour=2, minute=15, day_of_week='sun'),
            'args': (x2, y2),
        },
    }