Celery Scheduled Tasks

Published: March 16, 2017, 8:48 p.m. Back

Author: rachell

 

Working on an existing project that uses celery for sending emails to clients, taking screenshots, and a number of other tasks. My task was to email some site status to the site admin for all indicated sites on the system once a month at 9am on the first of the month. 

 

So the emailing was all set up and sending on activation, but how can we automate it so they will be sent on a regular schedule? If you guessed Celery, you're right, good job! 

 

Since we already had celery working(v3.1 check Official docs for setup), I needed to 

  1. Add 'djcelery' to installed apps in settings.py:

  2. Add to settings.py: 
    import djcelery
    djcelery.setup_loader()

  3. Made a task for testing in link_checker/tasks.py

from django.conf import settings
from projectname.celery import app
from celery.schedules import crontab
from celery.task import periodic_task
...
@periodic_task(run_every=contab(hour=1, minute=30, day_of_month=1))
def some_awesome_function():
...
do some magic here
...
@periodic_task(run_every(crontab(*)) # I used this for testing
def say_hi():
print('hi')

 

  1. I also added this in Celery.py : 

CELERY_TASK_RESULT_EXPIRES = 7*86400 
CELERY_SEND_EVENTS = True
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_BACKEND = 'djcelery.backends.database:DatabaseBackend'
CELERYBEAT_SCHEDULER = 'djcelery.schedulers.DatabaseScheduler' 

 Lastly, to test it locally, I had 3 terminals going:

python manage.py runserver

celery -A projectname beat -S djcelery.schedulers.DatabaseScheduler

celery -A projectname worker -l info 

 A little note: this was on version 3.1.25 of Celery. their latest version does things a bit differently, so make sure to user the documentation for whatever version your project is using. Also the @periodic decorator isn't the proper way per documentation but it works for now. To see how it is handled check celery's documentation.

If you see any mistakes or have any suggestions, feel free to leave a comment! 

Thanks

New Comment