Merge branch 'sburns-decorator-ttl'

This commit is contained in:
Vincent Driessen 2012-09-14 09:59:31 +02:00
commit 9ff5b18ebb
3 changed files with 22 additions and 5 deletions

View File

@ -1,11 +1,12 @@
from functools import wraps
from .queue import Queue
from .connections import resolve_connection
from .worker import DEFAULT_RESULT_TTL
class job(object):
def __init__(self, queue, connection=None, timeout=None):
def __init__(self, queue, connection=None, timeout=None,
result_ttl=DEFAULT_RESULT_TTL):
"""A decorator that adds a ``delay`` method to the decorated function,
which in turn creates a RQ job when called. Accepts a required
``queue`` argument that can be either a ``Queue`` instance or a string
@ -20,6 +21,7 @@ class job(object):
self.queue = queue
self.connection = resolve_connection(connection)
self.timeout = timeout
self.result_ttl = result_ttl
def __call__(self, f):
@wraps(f)
@ -29,6 +31,6 @@ class job(object):
else:
queue = self.queue
return queue.enqueue_call(f, args=args, kwargs=kwargs,
timeout=self.timeout)
timeout=self.timeout, result_ttl=self.result_ttl)
f.delay = delay
return f

View File

@ -29,6 +29,7 @@ green = make_colorizer('darkgreen')
yellow = make_colorizer('darkyellow')
blue = make_colorizer('darkblue')
DEFAULT_RESULT_TTL = 500
class StopRequested(Exception):
pass
@ -96,7 +97,7 @@ class Worker(object):
return worker
def __init__(self, queues, name=None, default_result_ttl=500,
def __init__(self, queues, name=None, default_result_ttl=DEFAULT_RESULT_TTL,
connection=None, exc_handler=None): # noqa
if connection is None:
connection = get_current_connection()

View File

@ -3,7 +3,7 @@ from tests.fixtures import decorated_job
from rq.decorators import job
from rq.job import Job
from rq.worker import DEFAULT_RESULT_TTL
class TestDecorator(RQTestCase):
@ -34,3 +34,17 @@ class TestDecorator(RQTestCase):
return 'Hi'
result = hello.delay()
self.assertEqual(result.origin, 'queue_name')
def test_decorator_accepts_result_ttl_as_argument(self):
"""Ensure that passing in result_ttl to the decorator sets the
result_ttl on the job
"""
#Ensure default
result = decorated_job.delay(1, 2)
self.assertEqual(result.result_ttl, DEFAULT_RESULT_TTL)
@job('default', result_ttl=10)
def hello():
return 'Why hello'
result = hello.delay()
self.assertEqual(result.result_ttl, 10)