-
Notifications
You must be signed in to change notification settings - Fork 0
Description
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
django-q-registry/src/django_q_registry/registry.py
Lines 96 to 101 in 35e5074
| 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.