rq / django-rq

A simple app that provides django integration for RQ (Redis Queue)
MIT License
1.81k stars 286 forks source link

USE_REDIS_CACHE with Django 4.0's native Redis backend? #571

Open rstmsn opened 1 year ago

rstmsn commented 1 year ago

Django has native support for Redis since version 4.0 via the django.core.cache.backends.redis.RedisCache backend.

If I create a Django cache, as follows:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.redis.RedisCache',
        'LOCATION': 'redis://127.0.0.1:6379',
    }
}

Is it possible to configure django-rq to use this redis connection, using the USE_REDIS_CACHE option?

obi-081 commented 1 year ago

I'm interested too. I'll try next week locally and then on a dev server. If it works, I'll put a message here.

obi-081 commented 1 year ago

Ok, it fails indeed. I got this stacktrace:

Traceback (most recent call last):
   File "/app/manage.py", line 25, in <module>
     main()
   File "/app/manage.py", line 21, in main
     execute_from_command_line(sys.argv)
   File "/usr/local/lib/python3.11/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
     utility.execute()
   File "/usr/local/lib/python3.11/site-packages/django/core/management/__init__.py", line 440, in execute
     self.fetch_command(subcommand).run_from_argv(self.argv)
   File "/usr/local/lib/python3.11/site-packages/django/core/management/base.py", line 402, in run_from_argv
     self.execute(*args, **cmd_options)
   File "/usr/local/lib/python3.11/site-packages/django/core/management/base.py", line 448, in execute
     output = self.handle(*args, **options)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/usr/local/lib/python3.11/site-packages/django_rq/management/commands/rqworker.py", line 120, in handle
     w = get_worker(*args, **worker_kwargs)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/usr/local/lib/python3.11/site-packages/django_rq/workers.py", line 46, in get_worker
     queues = get_queues(*queue_names, **{'job_class': job_class,
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/usr/local/lib/python3.11/site-packages/django_rq/queues.py", line 208, in get_queues
     return [get_queue(*queue_names, **kwargs)]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/usr/local/lib/python3.11/site-packages/django_rq/queues.py", line 165, in get_queue
     connection = get_connection(name)
                  ^^^^^^^^^^^^^^^^^^^^
   File "/usr/local/lib/python3.11/site-packages/django_rq/queues.py", line 142, in get_connection
     return get_redis_connection(QUEUES[name], use_strict_redis)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/usr/local/lib/python3.11/site-packages/django_rq/queues.py", line 97, in get_redis_connection
     return get_redis(config['USE_REDIS_CACHE'])
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/usr/local/lib/python3.11/site-packages/django_redis/__init__.py", line 15, in get_redis_connection
     raise NotImplementedError("This backend does not support this feature")
 NotImplementedError: This backend does not support this feature

So, I changed the django-rq settings like this:

RQ_QUEUES = {
     BACKGROUND_QUEUE: {
-        "USE_REDIS_CACHE": "default",
+        "HOST": REDIS_HOST,
+        "PORT": REDIS_PORT,
+        "USERNAME": REDIS_USERNAME,
+        "PASSWORD": REDIS_PASSWORD,
+        "DEFAULT_TIMEOUT": BACKGROUND_TIMEOUT,
     },
 }

And everything works fine. So the workaround is clean and simple for now.

selwin commented 1 year ago

@rstmsn @obi-jerome I'd love to have a PR to implement this.

mohamed99elsokary commented 9 months ago

@selwin @rstmsn @obi-jerome can you guys please give me a hint "What is that changes?"

obi-081 commented 9 months ago

@mohamed99elsokary Before Django 4.0 we used django-redis to get a redis cache. And django-rq could be simply configured then by setting "USE_REDIS_CACHE": "default". Now we configure our cache with the new native Django Redis cache backend django.core.cache.backends.redis.RedisCache But it is not declared as supported by django-rq, so we configured Redis manually for django-rq.

The native cache backend allows us to have one less dependency and to rely on the work of the Django Project that will probably give us a cache backend that is more reliable, integrated and performant, I guess.

It would be cool if django-rq was compatible with the Native Redis Backend configuration.