Skip to content

Job decorator uses 'default' queue TTL when queue is rq.Queue type rather than specified queue TTL #760

@ashfordium

Description

@ashfordium

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions