Skip to content

Commit

Permalink
Rearranged how explicit kwargs are passed into queue.enqueue(). Fixes r…
Browse files Browse the repository at this point in the history
  • Loading branch information
selwin committed Apr 11, 2014
1 parent 76e2470 commit 4f91804
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
17 changes: 7 additions & 10 deletions rq/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,17 +190,14 @@ def enqueue(self, f, *args, **kwargs):

# Detect explicit invocations, i.e. of the form:
# q.enqueue(foo, args=(1, 2), kwargs={'a': 1}, timeout=30)
timeout = None
description = None
result_ttl = None
depends_on = None
if 'args' in kwargs or 'kwargs' in kwargs or 'depends_on' in kwargs:
timeout = kwargs.pop('timeout', None)
description = kwargs.pop('description', None)
result_ttl = kwargs.pop('result_ttl', None)
depends_on = kwargs.pop('depends_on', None)

if 'args' in kwargs or 'kwargs' in kwargs:
assert args == (), 'Extra positional arguments cannot be used when using explicit args and kwargs.' # noqa
timeout = kwargs.pop('timeout', None)
description = kwargs.pop('description', None)
args = kwargs.pop('args', None)
result_ttl = kwargs.pop('result_ttl', None)
depends_on = kwargs.pop('depends_on', None)
args = kwargs.pop('args', None)
kwargs = kwargs.pop('kwargs', None)

return self.enqueue_call(func=f, args=args, kwargs=kwargs,
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ def access_self():
return job.id


def echo(*args, **kwargs):
return (args, kwargs)


class Number(object):
def __init__(self, value):
self.value = value
Expand Down
29 changes: 28 additions & 1 deletion tests/test_queue.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from tests import RQTestCase
from tests.fixtures import Number, div_by_zero, say_hello, some_calculation
from tests.fixtures import Number, div_by_zero, echo, say_hello, some_calculation
from rq import Queue, get_failed_queue
from rq.job import Job, Status
from rq.worker import Worker
Expand Down Expand Up @@ -260,6 +260,33 @@ def test_enqueue_sets_status(self):
job = q.enqueue(say_hello)
self.assertEqual(job.get_status(), Status.QUEUED)

def test_enqueue_explicit_args(self):
"""enqueue() works for both implicit/explicit args."""
q = Queue()

# Implicit args/kwargs mode
job = q.enqueue(echo, 1, timeout=1, result_ttl=1, bar='baz')
self.assertEqual(job.timeout, 1)
self.assertEqual(job.result_ttl, 1)
self.assertEqual(
job.perform(),
((1,), {'bar': 'baz'})
)

# Explicit kwargs mode
kwargs = {
'timeout': 1,
'result_ttl': 1,
}
job = q.enqueue(echo, timeout=2, result_ttl=2, args=[1], kwargs=kwargs)
self.assertEqual(job.timeout, 2)
self.assertEqual(job.result_ttl, 2)
self.assertEqual(
job.perform(),
((1,), {'timeout': 1, 'result_ttl': 1})
)


def test_all_queues(self):
"""All queues"""
q1 = Queue('first-queue')
Expand Down

0 comments on commit 4f91804

Please sign in to comment.