Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing tasks with celery at periodic schedule

I am trying to execute a task with celery in Django.I want to execute the task at 12:30 pm everyday for which I have written this in my tasks.py

@periodic_task(run_every=crontab(minute=30, hour=12), name="elast")
def elast():
        do something


This is not working but if I want to schedule it at every 30 seconds I write this code

@periodic_task(run_every=(timedelta(seconds=30)), name="elast")
def elast():
        do something

This works.I wanted to know that what is wrong with the first piece of code?Any help would be appreciated.

like image 582
Miller Avatar asked Oct 25 '25 15:10

Miller


2 Answers

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

like image 200
Roshan Bagdiya Avatar answered Oct 27 '25 05:10

Roshan Bagdiya


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

like image 20
Tomáš Linhart Avatar answered Oct 27 '25 07:10

Tomáš Linhart