From 538bbb1b55b8148d5448b7e15960c5207cfa322d Mon Sep 17 00:00:00 2001 From: Scott Burns Date: Thu, 13 Sep 2012 11:07:52 -0500 Subject: [PATCH] Decorator accepts result_ttl argument --- rq/decorators.py | 8 +++++--- rq/worker.py | 3 ++- tests/test_decorator.py | 16 +++++++++++++++- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/rq/decorators.py b/rq/decorators.py index d5851c3..d1a7086 100644 --- a/rq/decorators.py +++ b/rq/decorators.py @@ -1,11 +1,12 @@ from functools import wraps from .queue import Queue from .connections import resolve_connection - +from .worker import DEF_TTL class job(object): - def __init__(self, queue, connection=None, timeout=None): + def __init__(self, queue, connection=None, timeout=None, + result_ttl=DEF_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 diff --git a/rq/worker.py b/rq/worker.py index c936262..4d2b21d 100644 --- a/rq/worker.py +++ b/rq/worker.py @@ -29,6 +29,7 @@ green = make_colorizer('darkgreen') yellow = make_colorizer('darkyellow') blue = make_colorizer('darkblue') +DEF_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=DEF_TTL, connection=None, exc_handler=None): # noqa if connection is None: connection = get_current_connection() diff --git a/tests/test_decorator.py b/tests/test_decorator.py index 4f95f9c..d669fbf 100644 --- a/tests/test_decorator.py +++ b/tests/test_decorator.py @@ -3,7 +3,7 @@ from tests.fixtures import decorated_job from rq.decorators import job from rq.job import Job - +from rq.worker import DEF_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, DEF_TTL) + + @job('default', result_ttl=10) + def hello(): + return 'Why hello' + result = hello.delay() + self.assertEqual(result.result_ttl, 10)