From 6559b0ffd75eee1dbd2bab3b75b4074479866fd8 Mon Sep 17 00:00:00 2001 From: Selwin Ong Date: Thu, 22 Nov 2018 19:09:25 +0700 Subject: [PATCH] Replace "timeout" argument in queue.enqueue() with "job_timeout" (#1010) --- rq/queue.py | 7 ++++++- tests/test_queue.py | 13 +++++++++++-- tests/test_worker.py | 6 +++--- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/rq/queue.py b/rq/queue.py index 3aea012..b8b5b47 100644 --- a/rq/queue.py +++ b/rq/queue.py @@ -291,7 +291,12 @@ class Queue(object): # Detect explicit invocations, i.e. of the form: # q.enqueue(foo, args=(1, 2), kwargs={'a': 1}, timeout=30) - timeout = kwargs.pop('timeout', None) + timeout = kwargs.pop('job_timeout', None) + if timeout is None: + timeout = kwargs.pop('timeout', None) + if timeout: + warnings.warn('The `timeout` keyword is deprecated. Use `job_timeout` instead', DeprecationWarning) + description = kwargs.pop('description', None) result_ttl = kwargs.pop('result_ttl', None) ttl = kwargs.pop('ttl', None) diff --git a/tests/test_queue.py b/tests/test_queue.py index 2407c28..20a051c 100644 --- a/tests/test_queue.py +++ b/tests/test_queue.py @@ -319,12 +319,21 @@ class TestQueue(RQTestCase): self.assertEqual(job.meta['foo'], 'bar') self.assertEqual(job.meta['baz'], 42) + def test_job_timeout(self): + """Timeout can be passed via job_timeout argument""" + queue = Queue() + job = queue.enqueue(echo, 1, timeout=15) + self.assertEqual(job.timeout, 15) + + job = queue.enqueue(echo, 1, job_timeout=15) + self.assertEqual(job.timeout, 15) + def test_enqueue_explicit_args(self): """enqueue() works for both implicit/explicit args.""" q = Queue() # Implicit args/kwargs mode - job = q.enqueue(echo, 1, timeout=1, result_ttl=1, bar='baz') + job = q.enqueue(echo, 1, job_timeout=1, result_ttl=1, bar='baz') self.assertEqual(job.timeout, 1) self.assertEqual(job.result_ttl, 1) self.assertEqual( @@ -337,7 +346,7 @@ class TestQueue(RQTestCase): 'timeout': 1, 'result_ttl': 1, } - job = q.enqueue(echo, timeout=2, result_ttl=2, args=[1], kwargs=kwargs) + job = q.enqueue(echo, job_timeout=2, result_ttl=2, args=[1], kwargs=kwargs) self.assertEqual(job.timeout, 2) self.assertEqual(job.result_ttl, 2) self.assertEqual( diff --git a/tests/test_worker.py b/tests/test_worker.py index 2f0a9df..5d2370e 100644 --- a/tests/test_worker.py +++ b/tests/test_worker.py @@ -257,7 +257,7 @@ class TestWorker(RQTestCase): for timeout, expected_heartbeats in [(2, 0), (7, 1), (12, 2)]: job = q.enqueue(long_running_job, args=(timeout,), - timeout=30, + job_timeout=30, result_ttl=-1) with mock.patch.object(w, 'heartbeat', wraps=w.heartbeat) as mocked: w.execute_job(job, q) @@ -392,7 +392,7 @@ class TestWorker(RQTestCase): # Put it on the queue with a timeout value res = q.enqueue(create_file_after_timeout, args=(sentinel_file, 4), - timeout=1) + job_timeout=1) try: os.unlink(sentinel_file) @@ -543,7 +543,7 @@ class TestWorker(RQTestCase): worker = SimpleWorker([queue]) job_timeout = 300 - job = queue.enqueue(save_key_ttl, worker.key, timeout=job_timeout) + job = queue.enqueue(save_key_ttl, worker.key, job_timeout=job_timeout) worker.work(burst=True) job.refresh() self.assertGreater(job.meta['ttl'], job_timeout)