mirror of https://github.com/peter4431/rq.git
Added a job decorator.
parent
b7d1fab2db
commit
bdc1af28d1
@ -0,0 +1,35 @@
|
||||
from .connections import use_connection, get_current_connection
|
||||
from .queue import Queue
|
||||
|
||||
|
||||
class job(object):
|
||||
|
||||
def __init__(self, queue=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)
|
||||
def simple_add(x, y):
|
||||
return x + y
|
||||
|
||||
simple_add.delay(1, 2) # Puts simple_add function into queue
|
||||
|
||||
"""
|
||||
self.queue = queue
|
||||
|
||||
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)
|
||||
f.delay = delay
|
||||
return f
|
||||
|
||||
|
@ -0,0 +1,29 @@
|
||||
from tests import RQTestCase
|
||||
from tests.fixtures import decorated_job
|
||||
|
||||
from rq.decorators import job
|
||||
from rq.job import Job
|
||||
|
||||
|
||||
class TestDecorator(RQTestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(TestDecorator, self).setUp()
|
||||
|
||||
def test_decorator_preserves_functionality(self):
|
||||
"""
|
||||
Ensure that a decorated function's functionality is still preserved
|
||||
"""
|
||||
self.assertEqual(decorated_job(1, 2), 3)
|
||||
|
||||
def test_decorator_adds_delay_attr(self):
|
||||
"""
|
||||
Ensure that decorator adds a delay attribute to function that returns
|
||||
a Job instance when called.
|
||||
"""
|
||||
self.assertTrue(hasattr(decorated_job, 'delay'))
|
||||
result = decorated_job.delay(1, 2)
|
||||
self.assertTrue(isinstance(result, Job))
|
||||
# Ensure that job returns the right result when performed
|
||||
self.assertEqual(result.perform(), 3)
|
||||
|
Loading…
Reference in New Issue