RQ WorkerPool is not loading models at all
FlavienJP opened this issue · comments
Hi,
This issue is close to #650, but we are not in the same context.
I need my jobs to read/write models on database, so I've made a simple task to check that it's working.
@job("data-tasks", timeout=3600)
def Test(instance):
print(instance)
When using a single worker, no issue, I can pass the id, or the object, without any issue.
python manage.py rqworker data-tasks notifications
16:49:47 data-tasks: data.methods.Test(<Instance: [MyObject] LVX Vlxx>) (9ea7c2a5-420e-4849-873a-fe378708f760)
[MyObject] LVX Vlxx
16:49:50 data-tasks: Job OK (9ea7c2a5-420e-4849-873a-fe378708f760)
But when I'm using a pool of 1 worker, then it seems that something is going wrong, the content of Django App is not loaded.
python manage.py rqworker-pool data-tasks notifications --num-workers 1
16:51:36 data-tasks: data.methods.Test(<Instance: [MyObject] LVX Vlxx>) (76b6fb38-8f10-4e78-9231-9fafc7c29042)
16:51:37 [Job 76b6fb38-8f10-4e78-9231-9fafc7c29042]: exception raised while executing (<DeserializationError>)
Traceback (most recent call last):
File "/Users/mymac/PycharmProjects/MyApp/.venv/lib/python3.10/site-packages/rq/job.py", line 486, in _deserialize_data
self._func_name, self._instance, self._args, self._kwargs = self.serializer.loads(self.data)
File "/Users/mymac/PycharmProjects/MyApp/.venv/lib/python3.10/site-packages/django/db/models/base.py", line 2612, in model_unpickle
model = apps.get_model(*model_id)
File "/Users/mymac/PycharmProjects/MyApp/.venv/lib/python3.10/site-packages/django/apps/registry.py", line 201, in get_model
self.check_models_ready()
File "/Users/mymac/PycharmProjects/MyApp/.venv/lib/python3.10/site-packages/django/apps/registry.py", line 143, in check_models_ready
raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/mymac/PycharmProjects/MyApp/.venv/lib/python3.10/site-packages/rq/worker.py", line 1422, in perform_job
self.prepare_job_execution(job, remove_from_intermediate_queue)
File "/Users/mymac/PycharmProjects/MyApp/.venv/lib/python3.10/site-packages/rq/worker.py", line 1351, in prepare_job_execution
self.procline(msg.format(job.func_name, job.origin, time.time()))
File "/Users/mymac/PycharmProjects/MyApp/.venv/lib/python3.10/site-packages/rq/job.py", line 520, in func_name
self._deserialize_data()
File "/Users/mymac/PycharmProjects/MyApp/.venv/lib/python3.10/site-packages/rq/job.py", line 488, in _deserialize_data
raise DeserializationError() from e
rq.exceptions.DeserializationError
I've also tried to pass only the ID and get the object, then I've an error that is telling that AbstractUser
cannot be loaded.
File "/Users/mymac/PycharmProjects/MyApp/data/models.py", line 4, in <module>
from django.contrib.auth.models import User
File "/Users/mymac/PycharmProjects/MyApp/.venv/lib/python3.10/site-packages/django/contrib/auth/models.py", line 3, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/Users/mymac/PycharmProjects/MyApp/.venv/lib/python3.10/site-packages/django/contrib/auth/base_user.py", line 58, in <module>
class AbstractBaseUser(models.Model):
File "/Users/mymac/PycharmProjects/MyApp/.venv/lib/python3.10/site-packages/django/db/models/base.py", line 129, in __new__
app_config = apps.get_containing_app_config(module)
File "/Users/mymac/PycharmProjects/MyApp/.venv/lib/python3.10/site-packages/django/apps/registry.py", line 260, in get_containing_app_config
self.check_apps_ready()
File "/Users/mymac/PycharmProjects/MyApp/.venv/lib/python3.10/site-packages/django/apps/registry.py", line 138, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
So it seems to me that worker pool is missing something.
Thanks for your help,