diff --git a/rq/decorators.py b/rq/decorators.py index 53eb4cb..b577667 100644 --- a/rq/decorators.py +++ b/rq/decorators.py @@ -1,20 +1,15 @@ -from .connections import use_connection, get_current_connection from .queue import Queue class job(object): - def __init__(self, queue=None): + def __init__(self, queue, connection=None, timeout=None): """ A decorator that adds a ``delay`` method to the decorated function, which in turn creates a RQ job when called. Accepts a ``queue`` instance as an optional argument. For example: - from rq import Queue, use_connection - use_connection() - q = Queue() - - @job(queue=q) + @job(queue='default') def simple_add(x, y): return x + y @@ -22,14 +17,15 @@ class job(object): """ self.queue = queue + self.connection = connection + self.timeout = timeout def __call__(self, f): def delay(*args, **kwargs): - if self.queue is None: - use_connection(get_current_connection()) - self.queue = Queue() - return self.queue.enqueue(f, *args, **kwargs) + if isinstance(self.queue, basestring): + queue = Queue(name=self.queue, connection=self.connection) + else: + queue = self.queue + return queue.enqueue_call(f, args=args, kwargs=kwargs, timeout=self.timeout) f.delay = delay return f - - diff --git a/tests/fixtures.py b/tests/fixtures.py index ada2bf5..1edebc4 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -51,6 +51,6 @@ class Calculator(object): def calculate(x, y): return x * y / self.denominator -@job() +@job(queue='default') def decorated_job(x, y): return x + y diff --git a/tests/test_decorator.py b/tests/test_decorator.py index 4a6fcb3..cf6d4e6 100644 --- a/tests/test_decorator.py +++ b/tests/test_decorator.py @@ -27,3 +27,13 @@ class TestDecorator(RQTestCase): # Ensure that job returns the right result when performed self.assertEqual(result.perform(), 3) + def test_decorator_accepts_queue_name_as_argument(self): + """ + Ensure that passing in queue name to the decorator puts the job in the + right queue. + """ + @job(queue='queue_name') + def hello(): + return 'Hi' + result = hello.delay() + self.assertEqual(result.origin, 'queue_name')