Django Scheduled Task Using Django-Q

Im trying to run scheduled task using Django-q I followed the docs but its not running

heres my config

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
        'LOCATION': 'db_cache_table',
    }
}


Q_CLUSTER = {
    'name': 'DjangORM',
    'workers': 4,
    'timeout': 90,
    'retry': 120,
    'queue_limit': 50,
    'bulk': 10,
    'orm': 'default'

}

heres my scheduled task

Nothin is executing, please help

2 Answers

I also had problems with getting scheduled tasks processed in the first place, but finally found a workflow.
I run django-q on a windows machine, using the django ORM as a broker. Before talking about the execution routine i came up, lets quickly check out my modules first, starting with ..

settings.py:

Q_CLUSTER = {
    "name": "austrian_energy_monthly",
    "workers": 1,
    "timeout": 10,
    "retry": 20,
    "queue_limit": 50,
    "bulk": 10,
    "orm": "default",
    "ack_failures": True,
    "max_attempts": 1,
    "attempt_count": 0,
}

.. and my folder structure:

As you can see, the folder of my django project is inside the src folder. Further, there's a folder for the app i created for this project, which is simply called "app". Inside the app folder i do have another folder called "cron", which includes the following files and functions related to the scheduling:

tasks.py

I do not use the schedule() method provided by the django-q, instead i go for the creating tables directly (see: django-q official schedule docs)

from django.utils import timezone
from austrian_energy_monthly.app.cron.func import create_text_file

from django_q.models import Schedule

Schedule.objects.create(
    func="austrian_energy_monthly.app.cron.func.create_text_file",
    kwargs={"content": "Insert this into a text file"},
    hooks="austrian_energy_monthly.app.cron.hooks.print_result",
    name="Text file creation process",
    schedule_type=Schedule.ONCE,
    next_run=timezone.now(),
)

Make sure you assign the "right" path to the "func" keyword. Just using "func.create_text_file",didn't work out for me, even though these files are laying in the same folder. Same for the "hooks" keyword.
(NOTE: I've set up my project as a development package via setup.py, such that i can call it from everywhere inside my src folder)

func.py:

Contains the function called by the schedule table object.

def create_text_file(content: str) -> str:
    file = open(f"copy.txt", "w")
    file.write(content)
    file.close()
    return "Created a text file"

hooks.py:

Contains the function called after the scheduled process finished.

def print_result(task):
    print(task.result)

Let's now see how i managed to get the executions running for with the file examples described above:

  1. First i've scheduled the "Text file creation process". Therefore I used "python manage.py shell" and imported the tasks.py module (you probably could schedule everythin via the admin page as well, but i didnt tested this yet):

You could now see the scheduled task, with a question mark on the success column in the admin page (tab "Scheduled tasks", as within your picture):

  1. After that i opened a new terminal and started the cluster with "python manage.py qcluster", resulting in the following output in the terminal:

The successful execution can be inspected by looking at "13:22:17 [Q] INFO Processed [ten-virginia-potato-high]", alongside the hook print statement "Created a text file" in the terminal. Further you can check it at the admin page, under the tab "Successful Tasks", where you should see:

Hope that helped!

1

Django-q dont support windows. :)

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Elena Rostova

Elena Rostova

Lead Health, Wellness & Medical Journalist

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.

Share this article
Twitter Facebook Pinterest