Set up a scheduled job in a Django project?

Learn, how to set up a scheduled job in a Django project?
Submitted by Apurva Mathur, on May 21, 2022

Job scheduling is the process where different tasks get executed at a fixed time without any human intervention. Scheduled jobs run automatically at regular internal decided by the human, this is a very important concept in today's world.

The job scheduling process can be used in the domain where something really needs to get updated after a fixed amount of time.

For example:

Imagine you own an e-commerce website and you give an exclusive discount of 20% on some product category (let's say on electronic items) at every 9:00 Pm. So what would you do, you'll tell your team to apply offers every day? That would be a tough task. Here job scheduling comes into the picture as you don't need any human interference once you fixed the task that the machine has to execute. In this case, the machine will automatically apply a markdown of 20% on every electronic category whenever the time comes.

There are many ways to perform the job scheduling process, but using Django Q is somewhat more trouble-free than other methods.

Django Q is a native Django task queue, scheduler, and worker application using python multiprocessing.

Let's see how to do this task by taking an example.

Visualize, in a webpage you have to delete the offers which were created last hour of the current time, and you have to do this process in a loop.

Step 1: Model Creation

Simply make a model in model.py file named as Offers_Applied and inside this model create a field that keeps the record of every offer date and time (let's name it Offer_time).

from django.db import models
from datetime import datetime

class Offers_Applied(models.Model):
    Offers_time=models.DateTimeField(default=datetime.now)

Step 2: Installing a package, since we are working on a new package so it's necessary to install it first. You can install it directly by typing pip install django-q or you can also install it from pycharm package installer settings.

Set up a scheduled job (step 1)

Step 3: Changing the settings, after importing this, it's important to change the settings else we'll the error. To change the settings go to setting.py file.

Set up a scheduled job (step 2)

Step 4: Migrate; (Migration is a way of applying changes that we have made to a model, into the database schema.) by typing python manage.py migrate.

Set up a scheduled job (step 3)

Step 5: See Changes, after apply migrations type python manage.py showmigrations this will the result of changes made in your database.

Set up a scheduled job (step 4)

Step 6: Now, create a python file to define your task, in this file you will simply write the task that you want the machine to perform. This will be the simple python code which we usually write on a daily basis.

def remove_last_hour_offers():
    previous_hour = datetime.now() - datetime.timedelta(hours=1)
    last_hour_offers = Offers_Applied.objects.filter(
        Offers_time__lte=previous_hour
    )
    remove_last_hour_offers()

Step 7: After this, just create a schedule at the admin side.

from django_q.models import Schedule

Schedule.objects.create(
    func= 'remove_last_hour_offers',
    hours=1,
    repeats=-1
)

Step 8: Last step is to just run the server by typing python manage.py runserver and django q scheduler by typing python manage.py qcluster.




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.