Task Scheduler using Django and Celery

Ashun Kothari / June 20, 2021

3 min read

Task Scheduler

At time we need to run a bunch of scheduled jobs to do some work. Crontab is a a great utility for running such scheduled jobs but it has its limitations. Lets see what all things could make a better scheduler

Requirement checklist 📝#

Tech stack based on the requirement#

Based on the requirement our candidate to manage these scheduled jobs or tasks is celery.

Here is the tech stack which we will be using

  • Django - To provide a simple interface to manage the scheduler
  • Celery - To run and manage tasks
  • Redis - Our message broker
  • Postgres - To store task relevant data(status, scheduler, etc). "The World's Most Advanced Open Source Relational Database"

High level Dataflow diagram#

Task Scheduler Architecture

Setting up 🎉#

Github Project#

  • You can clone/fork this project to get started. I have configured all the below mentioned plugins and you can just run the project using docker-compose. Project Link

Resources#

  • You can use this guide to setup django + celery
  • We will use django-celery-beat to store the schedule of our tasks in database.
  • django-celery-results is another plugin which help us in storing the task results in database and make it accessible by the django admin page directly (You can use flower for checking up the results)
settings.py
# Storing the celery tasks results
CELERY_RESULT_BACKEND = "django-db"
  • We can define the redis server details like this
settings.py
# Settings for celery and redis configurations
CELERY_BROKER_URL = 'redis://redis:6379'  

Screenshots of the project 📸#

Django Admin Home#

admin_home

List of Tasks#

tasks

Add a Task#

new_task

Check Tasks results#

task_result

Revising the requirements#