-
-
Notifications
You must be signed in to change notification settings - Fork 294
Description
Versions affected
I ran into the exception on version 3.2.1. Looking at the code, it seems like it also affects current master branch.
I am using Django 5.2.5 and rq 2.6.1
Bug
In this code
django-rq/django_rq/decorators.py
Lines 49 to 69 in 4f2dfaf
| if callable(func_or_queue): | |
| func = func_or_queue | |
| queue: Union[Queue, str] = 'default' | |
| else: | |
| func = None | |
| queue = func_or_queue | |
| queue_name = 'default' | |
| if isinstance(queue, str): | |
| queue_name = queue | |
| try: | |
| queue = get_queue(queue) | |
| if connection is None: | |
| connection = queue.connection | |
| except KeyError: | |
| pass | |
| else: | |
| if connection is None: | |
| connection = queue.connection | |
| kwargs['result_ttl'] = kwargs.get('result_ttl', get_result_ttl(queue_name)) |
When the func_or_queue argument to the job decorator is a rq.Queue (or non-callable, non-str type), then queue_name ends up being 'default' rather than using rq.Queue.name.
In my Django app, in which there is no 'default' named queue, this results in a KeyError when I provide a Queue object rather than the string name of my queue. This is the KeyError:
File "/.../.venv1/lib/python3.11/site-packages/django_rq/decorators.py", line 69, in job
kwargs['result_ttl'] = kwargs.get('result_ttl', get_result_ttl(queue_name))
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/.../.venv1/lib/python3.11/site-packages/django_rq/queues.py", line 202, in get_result_ttl
return QUEUES[name].get('DEFAULT_RESULT_TTL', RQ.get('DEFAULT_RESULT_TTL'))
~~~~~~^^^^^^
KeyError: 'default'
Fix
My first thought at a fix is to insert the line queue_name = queue.name between lines 65 and 66 above.
This seems like it should be fine since:
func_or_queuehas its type specified asUnion['Callable[P, R]', 'Queue', str]- when
func_or_queueis astror callable at runtime, thenqueueat line 57 above has typestr - when
func_or_queueis a'Queue', thenqueueat line 57 above has type'Queue'
- when
- so at line 66 above, when
queuehas non-str type,queueshould have type'Queue'- so
queueshould have a.nameattribute in thatelsebranch body assuming'Queue'isrq.Queue
- so