Skip to content

Commit

Permalink
get_worker: replace special kwargs with separate parameters (#659)
Browse files Browse the repository at this point in the history
This communicates more information to IDEs and type checkers, making the
function easier to use.

Having regular parameters after `*args` has been possible since Python 3.0.
  • Loading branch information
SpecLad authored May 1, 2024
1 parent 5ce9bf2 commit fa23214
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions django_rq/workers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from typing import Optional, Type, Union

from rq import Worker
from rq.job import Job
from rq.utils import import_attribute

from django.conf import settings

from .jobs import get_job_class
from .queues import get_queues
from .queues import DjangoRQ, get_queues


def get_exception_handlers():
Expand Down Expand Up @@ -37,16 +40,21 @@ def get_worker_class(worker_class=None):
return worker_class


def get_worker(*queue_names: str, **kwargs) -> Worker:
def get_worker(
*queue_names: str,
job_class: Optional[Union[str, Type[Job]]] = None,
queue_class: Optional[Union[str, Type[DjangoRQ]]] = None,
worker_class: Optional[Union[str, Type[Worker]]] = None,
**kwargs,
) -> Worker:
"""
Returns a RQ worker for all queues or specified ones.
"""
job_class = get_job_class(kwargs.pop('job_class', None))
queue_class = kwargs.pop('queue_class', None)
queues = get_queues(*queue_names, **{'job_class': job_class, 'queue_class': queue_class})
job_class = get_job_class(job_class)
queues = get_queues(*queue_names, job_class=job_class, queue_class=queue_class)
# normalize queue_class to what get_queues returns
queue_class = queues[0].__class__
worker_class = get_worker_class(kwargs.pop('worker_class', None))
worker_class = get_worker_class(worker_class)
return worker_class(
queues,
connection=queues[0].connection,
Expand Down

0 comments on commit fa23214

Please sign in to comment.