Creating a Celery Periodic Task on Gondor

I’ve recently been using Gondor as a hosting platform for a business I’m starting. I needed to set up a task to update my search indexes every 30 minutes, so I thought of Celery. Asking in #gondor on freenode, I discovered that the Gondor guys had recently gotten whatever backend was needed to make Celery periodic tasks available on Gondor. They haven’t yet gotten around to documenting it fully, but it turned out to be relatively simple.

Setting up Gondor for Celery:

The Gondor docs have a great page on this. Follow the directions and you should be good. I configured my instance using Redis, so I can’t speak to using Kombu.

One note on using the settings from their docs: you’ll probably want to configure your local settings as well. I set up an if statement to detect if I was running locally or on Gondor than specified which settings to load for Redis, etc. Using platform.node is the best way I’ve found to do this (needs some modification if you’re working with more than one dev):

import platform
if platform.node().startswith('your-hostname'):
    #load your settings
else:
    #load Gondor settings

Setting up Celery for Gondor:

For the purposes of this post, I’m going to assume that you already know how to use Celery. If not, it’s not too complicated, check out the docs here. If you have Celery up and executing normal tasks locally, you’re nearly there already. Running a task periodically just requires that you register that task to run periodically. This page here documents the steps. Locally I was able to tasks running both by specifying when they should run in the settings as well as using the database scheduler, but on Gondor I had to use the database scheduler to get it working (could be error my part, I’m not sure). Look here for directions on setting up the DB scheduler. Basically, all I had to do to get this working was add this to my settings.py file:

CELERYBEAT_SCHEDULER = ‘djcelery.schedulers.DatabaseScheduler’

From this point, go into the Django admin (don’t forget to deploy first). You’ll see this section:

Depending on what you want to do here, you may have different steps, but to get a basic task running, add an Interval (pretty self explanatory) then click on “Periodic tasks.” Then click to add a periodic task, and you should see this:

If you’ve set your task up to register properly, you’ll see it listed in the Task (registered) dropdown. If it’s not there, it’s probably something to do with relative paths (read this). Once you’ve selected the task you want, named it, and set an interval, things should just start working. Gondor handles running the Celery workers and Celerybeat on their end.

Good luck and stay safe out there!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s