Skip to content

Can't register tasks in django settings file #117

@chris48s

Description

@chris48s

Hello.

The documentation in https://github.com/westerveltco/django-q-registry?tab=readme-ov-file#registering-periodic-tasks shows 3 examples. Example 3 suggests that it should be possible to register tasks in a django settings file.

I tried out this example code, but I hit two problems.

Issue 1: Can't import from django_q.models import Schedule in settings file

The documentation suggest putting from django_q.models import Schedule in your settings file so I tried that.

I found that if I put from django_q.models import Schedule at the top of my settings file, then trying to run ./manage.py qcluster fails with the error Unknown command: 'qcluster' and trying to run ./manage.py setup_periodic_tasks fails with the error Unknown command: 'setup_periodic_tasks'.

If I move the line from django_q.models import Schedule down to somewhere after the INSTALLED_APPS declaration, that still doesn't work, but I get the error django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet instead.

As far as I can tell, it doesn't seem possible to import from django_q.models import Schedule in a django settings file.

However, the only reason I'm trying to import this is so that I can use the value of Schedule.CRON.

So I decided to just press on and see if I could work around this for now by hard-coding

"schedule_type": "C",

(which is the value of Schedule.CRON)
https://github.com/django-q2/django-q2/blob/09e65da5b34b9de152905dec42336d27c8b2116d/django_q/models.py#L191

Doing that resulted in..

Issue 2: KeyError: 'func'

Instead of trying to import Schedule.CRON, I wrote

Q_REGISTRY = {
    "TASKS": [
        {
            "name": "Do The Things",
            "func": "myapp.tasks.do_the_things",
            "kwargs": {},
            "schedule_type": "C",
            "cron": "*/5 * * * *",
        },
    ],
}

Now if I try to start ./manage.py qcluster or ./manage.py setup_periodic_tasks I get KeyError: 'func' in

def _register_settings(self):
for task_dict in app_settings.TASKS:
self._register_task(
func=task_dict.pop("func"),
**task_dict,
)


In the end, I abandoned trying to declare tasks in a settings file and got my application working using the decorator syntax (which does work as expected).

I have confirmed that this behaviour reproduces in a completely clean django app with these packages:

django==5.2.6
django-q2==1.8.0
django-q-registry==0.5.0
blessed==1.20.0
croniter==6.0.0

If you need any more info to reproduce, let me know.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions