summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornerok <nerok@users.noreply.github.com>2020-08-14 02:17:03 +0200
committerGitHub <noreply@github.com>2020-08-14 07:17:03 +0700
commit7bf100ebe794179847b774f458857fb3bc6234c2 (patch)
tree60bc5ca43f3e343b5e7dfc4c9171e69e2a787b70
parentc2931b45b624215d6b4ab6f57e8d95b335472814 (diff)
downloadrq-7bf100ebe794179847b774f458857fb3bc6234c2.tar.gz
Allow retries to be set through decorator (#1319)
Co-authored-by: Didrik Koren <didrik.koren@uninett.no>
-rw-r--r--rq/decorators.py6
-rw-r--r--tests/test_decorator.py18
2 files changed, 21 insertions, 3 deletions
diff --git a/rq/decorators.py b/rq/decorators.py
index e8c1f37..c86a0c5 100644
--- a/rq/decorators.py
+++ b/rq/decorators.py
@@ -17,7 +17,7 @@ class job(object): # noqa
def __init__(self, queue, connection=None, timeout=None,
result_ttl=DEFAULT_RESULT_TTL, ttl=None,
queue_class=None, depends_on=None, at_front=None, meta=None,
- description=None, failure_ttl=None):
+ description=None, failure_ttl=None, retry=None):
"""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
@@ -40,6 +40,7 @@ class job(object): # noqa
self.at_front = at_front
self.description = description
self.failure_ttl = failure_ttl
+ self.retry = retry
def __call__(self, f):
@wraps(f)
@@ -63,6 +64,7 @@ class job(object): # noqa
return queue.enqueue_call(f, args=args, kwargs=kwargs,
timeout=self.timeout, result_ttl=self.result_ttl,
ttl=self.ttl, depends_on=depends_on, job_id=job_id, at_front=at_front,
- meta=self.meta, description=self.description, failure_ttl=self.failure_ttl)
+ meta=self.meta, description=self.description, failure_ttl=self.failure_ttl,
+ retry=self.retry)
f.delay = delay
return f
diff --git a/tests/test_decorator.py b/tests/test_decorator.py
index 47c4226..5e846db 100644
--- a/tests/test_decorator.py
+++ b/tests/test_decorator.py
@@ -6,7 +6,7 @@ import mock
from redis import Redis
from rq.decorators import job
-from rq.job import Job
+from rq.job import Job, Retry
from rq.queue import Queue
from rq.worker import DEFAULT_RESULT_TTL
from tests import RQTestCase
@@ -221,3 +221,19 @@ class TestDecorator(RQTestCase):
return 'Why hello'
result = hello.delay()
self.assertEqual(result.failure_ttl, 10)
+
+ def test_decorator_custom_retry(self):
+ """ Ensure that passing in retry to the decorator sets the
+ retry on the job
+ """
+ # Ensure default
+ result = decorated_job.delay(1, 2)
+ self.assertEqual(result.retries_left, None)
+ self.assertEqual(result.retry_intervals, None)
+
+ @job('default', retry=Retry(3, [2]))
+ def hello():
+ return 'Why hello'
+ result = hello.delay()
+ self.assertEqual(result.retries_left, 3)
+ self.assertEqual(result.retry_intervals, [2])