2👍
Running Periodic Tasks
I’m not sure what you’ve tried so far, or what your settings.py
file looks like but here are some ways to get djcelery to run scheduled tasks.
1) In your tasks.py
, create a task and use the celery.decorators.periodic_task
decorator. e.g.
from celery.task.schedules import crontab
from celery.decorators import periodic_task
@periodic_task(run_every=crontab(hour=”*”, minute=”*”, day_of_week=”*”))
def a_periodic_task():
print “Hello World!”
or
@celery.decorators.periodic_task(run_every=datetime.timedelta(minutes=5))
def a_periodic_task():
print “Hello World!”
2) In your celery.conf
file use the CELERYBEAT_SCHEDULE
setting like so:
from datetime import timedelta
CELERYBEAT_SCHEDULE = {
"runs-every-30-seconds": {
"task": "tasks.add",
"schedule": timedelta(seconds=30),
"args": (16, 16)
},
}
You can read more about this in the periodic task documentation.
Though please remeber that you must start celery in beat mode by:
python manage.py celeryd -B
And also check you’ve done all the installation steps including adding djcelery
to your INSTALLED_APPS
and running python manage.py syncdb
(or python manage.py migrate djcelery
if you’re using south)
Deployment
The celery documentation has a great section on daemonising your celery processes for deployment including an example django configuration for beat mode.
From the Docs:
# Where the Django project is. CELERYBEAT_CHDIR="/opt/Project/" # Name of the projects settings module. export DJANGO_SETTINGS_MODULE="settings" # Path to celerybeat CELERYBEAT="/opt/Project/manage.py celerybeat" # Extra arguments to celerybeat CELERYBEAT_OPTS="--schedule=/var/run/celerybeat-schedule"