From b14c4e288d5f1b74f16c42f381a6c8993ee9c122 Mon Sep 17 00:00:00 2001 From: Selwin Ong Date: Sun, 16 Jun 2019 18:39:16 +0700 Subject: [PATCH] Added checks for 0 ttl (#1110) --- rq/queue.py | 11 ++++++++++- tests/test_queue.py | 13 +++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/rq/queue.py b/rq/queue.py index d58c27b..64280de 100644 --- a/rq/queue.py +++ b/rq/queue.py @@ -231,10 +231,19 @@ class Queue(object): and kwargs as explicit arguments. Any kwargs passed to this function contain options for RQ itself. """ - timeout = parse_timeout(timeout) or self._default_timeout + timeout = parse_timeout(timeout) + + if timeout is None: + timeout = self._default_timeout + elif timeout == 0: + raise ValueError('0 timeout is not allowed. Use -1 for infinite timeout') + result_ttl = parse_timeout(result_ttl) failure_ttl = parse_timeout(failure_ttl) + ttl = parse_timeout(ttl) + if ttl is not None and ttl <= 0: + raise ValueError('Job ttl must be greater than 0') job = self.job_class.create( func, args=args, kwargs=kwargs, connection=self.connection, diff --git a/tests/test_queue.py b/tests/test_queue.py index 463396e..750edde 100644 --- a/tests/test_queue.py +++ b/tests/test_queue.py @@ -237,6 +237,12 @@ class TestQueue(RQTestCase): None ) self.assertEqual(q.count, 0) + + def test_enqueue_with_ttl(self): + """Negative TTL value is not allowed""" + queue = Queue() + self.assertRaises(ValueError, queue.enqueue, echo, 1, ttl=0) + self.assertRaises(ValueError, queue.enqueue, echo, 1, ttl=-1) def test_enqueue_sets_status(self): """Enqueueing a job sets its status to "queued".""" @@ -264,6 +270,13 @@ class TestQueue(RQTestCase): job = queue.enqueue(echo, 1, job_timeout=15) self.assertEqual(job.timeout, 15) + # Not passing job_timeout will use queue._default_timeout + job = queue.enqueue(echo, 1) + self.assertEqual(job.timeout, queue._default_timeout) + + # job_timeout = 0 is not allowed + self.assertRaises(ValueError, queue.enqueue, echo, 1, job_timeout=0) + def test_default_timeout(self): """Timeout can be passed via job_timeout argument""" queue = Queue()