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
|
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_queue has its type specified as Union['Callable[P, R]', 'Queue', str]
- when
func_or_queue is a str or callable at runtime, then queue at line 57 above has type str
- when
func_or_queue is a 'Queue', then queue at line 57 above has type 'Queue'
- so at line 66 above, when
queue has non-str type, queue should have type 'Queue'
- so
queue should have a .name attribute in that else branch body assuming 'Queue' is rq.Queue
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
When the
func_or_queueargument to the job decorator is arq.Queue(or non-callable, non-str type), thenqueue_nameends up being'default'rather than usingrq.Queue.name.In my Django app, in which there is no
'default'named queue, this results in aKeyErrorwhen I provide aQueueobject rather than the string name of my queue. This is theKeyError:Fix
My first thought at a fix is to insert the line
queue_name = queue.namebetween 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]func_or_queueis astror callable at runtime, thenqueueat line 57 above has typestrfunc_or_queueis a'Queue', thenqueueat line 57 above has type'Queue'queuehas non-str type,queueshould have type'Queue'queueshould have a.nameattribute in thatelsebranch body assuming'Queue'isrq.Queue