[Django]-Executing tasks with celery at periodic schedule

9👍

As per latest celery 4.3 version , to execute the task at 12:30 pm below code will be useful
celery.py

from celery.schedules import crontab

app.conf.beat_schedule = {
    # Executes every day at  12:30 pm.
    'run-every-afternoon': {
        'task': 'tasks.elast',
        'schedule': crontab(hour=12, minute=30),
        'args': (),
    },
}

tasks.py

import celery
@celery.task
def elast():
    do something

to start celery beat scheduler
celery -A proj worker -B

for older version around celery 2.0

from celery.task.schedules import crontab
from celery.decorators import periodic_task

@periodic_task(run_every=crontab(hour=12, minute=30))
def elast():
    print("code execution started.")

please check timezone setting.

New userguide

Old userguide

2👍

Check out the documentation, especially the parts specific for Django users. Also note that using @periodic_task decorator is deprecated and should be replaced with beat_schedule configuration (see the code).

Leave a comment