简体   繁体   中英

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

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!

Django-q dont support windows. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM