Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent Celery Periodic Tasks from executing late?

I have some tasks in Celery that are scheduled with Celery Beat/Crontab like this:

CELERYBEAT_SCHEDULE = {
    'task-1': {
        'task': 'tasks.run_task1',
        'schedule': crontab(hour=5, minute=30, day_of_week='mon-fri'),
    },
    'task-2': {
        'task': 'tasks.run_task2',
        'schedule': crontab(hour=12, minute=0, day_of_week='sun-fri'),
    },
}

Sometimes I have the Celery process turned off and when I turn it back on at 4:00pm, it will run the tasks from earlier in the day. How can I make it so that these tasks will only run within ~5 minutes of when they are actually scheduled and NOT start to run later in the day?

like image 403
PaulMest Avatar asked Oct 20 '22 05:10

PaulMest


1 Answers

Use the expires option:

CELERYBEAT_SCHEDULE = {
    'task-1': {
        'task': 'tasks.run_task1',
        'schedule': crontab(hour=5, minute=30, day_of_week='mon-fri'),
        'options': {
                    'expires': 5*60,
                    },
    },
    'task-2': {
        'task': 'tasks.run_task2',
        'schedule': crontab(hour=12, minute=0, day_of_week='sun-fri'),
        'options': {
                    'expires': 5*60,
                    },
    },
}
like image 66
noamk Avatar answered Nov 01 '22 14:11

noamk