Search code examples
djangocroncelerydjango-celeryperiodic-task

Schedule Django Celery periodic tasks to run from time to other time


How can I accomplish running Django Celery tasks to be running only from Mon to Friday and on those days only from 9am to 5pm EST?

celery.py

from celery.schedule import crontab


app.conf.beat_schedule = {
    'compute-every-5-seconds': {
         'task': 'sum',
         'schedule': crontab(),
     },
  }

What parameters should I add to crontab() that it would run those days and between only those hours?


Solution

  • celery.py

    from celery.schedule import crontab
    app.conf.beat_schedule = {
        'compute-every-minute-mon-through-friday-9-to-5': {
             'task': 'sum',
             'schedule': crontab(minute='*/1',
    hour='9-17', day_of_week='mon,tue,wed,thu,fri'),
         },
      }
    

    minute='*/1' - runs every minute

    hour='9-17' - runs 9am to 5pm

    day_of_week='mon,tue,wed,thu,fri' - Monday through Friday

    Most of these are available on the documentation page, check it out!