From 5b5ab4860f56c430280ba6e39e02361c06222622 Mon Sep 17 00:00:00 2001 From: glaslos Date: Thu, 29 Jan 2015 14:18:59 +0100 Subject: [PATCH 1/5] passing on the ttl --- rq/queue.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rq/queue.py b/rq/queue.py index 7352459..bdcf1f3 100644 --- a/rq/queue.py +++ b/rq/queue.py @@ -184,7 +184,7 @@ class Queue(object): # TODO: job with dependency shouldn't have "queued" as status job = self.job_class.create(func, args, kwargs, connection=self.connection, - result_ttl=result_ttl, status=JobStatus.QUEUED, + result_ttl=result_ttl, ttl=ttl, status=JobStatus.QUEUED, description=description, depends_on=depends_on, timeout=timeout, id=job_id) From 3af0e95ce5a0314fa8d95b60785e6cf2979e9d6b Mon Sep 17 00:00:00 2001 From: glaslos Date: Thu, 29 Jan 2015 14:19:50 +0100 Subject: [PATCH 2/5] adding two tests to ensure ttl behaviour --- tests/fixtures.py | 5 +++-- tests/test_job.py | 18 +++++++++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/tests/fixtures.py b/tests/fixtures.py index a0e8eba..4271373 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -85,5 +85,6 @@ with Connection(): return x + y -def long_running_job(): - time.sleep(10) +def long_running_job(timeout=10): + time.sleep(timeout) + return 'Done sleeping...' diff --git a/tests/test_job.py b/tests/test_job.py index 34859a7..1684315 100644 --- a/tests/test_job.py +++ b/tests/test_job.py @@ -12,7 +12,7 @@ from rq.utils import utcformat from tests import RQTestCase from tests.fixtures import (access_self, CallableObject, Number, say_hello, - some_calculation) + some_calculation, long_running_job) from tests.helpers import strip_microseconds try: @@ -313,6 +313,22 @@ class TestJob(RQTestCase): job.save() self.assertEqual(job.get_ttl(), None) + def test_ttl_via_enqueue(self): + ttl = 1 + queue = Queue(connection=self.testconn) + job = queue.enqueue(say_hello, ttl=ttl) + self.assertEqual(job.get_ttl(), ttl) + + def test_expire_during_execution(self): + """Test what happens when job expires during execution""" + ttl = 2 + queue = Queue(connection=self.testconn) + job = queue.enqueue(long_running_job, args=(4,), ttl=ttl) + self.assertEqual(job.get_ttl(), ttl) + job.perform() + self.assertFalse(job.exists(job.id)) + self.assertEqual(job.result, 'Done sleeping...') + def test_cleanup(self): """Test that jobs and results are expired properly.""" job = Job.create(func=say_hello) From 703ab0e355b2d427da252693da1f865db8dd2de9 Mon Sep 17 00:00:00 2001 From: glaslos Date: Thu, 29 Jan 2015 14:26:32 +0100 Subject: [PATCH 3/5] removed merged comment --- rq/queue.py | 1 - 1 file changed, 1 deletion(-) diff --git a/rq/queue.py b/rq/queue.py index 6b0c3bd..2c175e5 100644 --- a/rq/queue.py +++ b/rq/queue.py @@ -182,7 +182,6 @@ class Queue(object): """ timeout = timeout or self._default_timeout - # TODO: job with dependency shouldn't have "queued" as status job = self.job_class.create( func, args, kwargs, connection=self.connection, result_ttl=result_ttl, ttl=ttl, status=JobStatus.QUEUED, From 3d4d6a86d5f73ccdedbb4d2ee596f957337a5fd3 Mon Sep 17 00:00:00 2001 From: glaslos Date: Fri, 27 Feb 2015 16:07:44 +0100 Subject: [PATCH 4/5] persist the job right before execution --- rq/job.py | 2 ++ tests/test_job.py | 10 ++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/rq/job.py b/rq/job.py index 5dbbcf5..7195ddb 100644 --- a/rq/job.py +++ b/rq/job.py @@ -485,6 +485,8 @@ class Job(object): # Job execution def perform(self): # noqa """Invokes the job function with the job arguments.""" + self.connection.persist(self.key) + self.ttl = self.connection.ttl(self.key) _job_stack.push(self.id) try: self._result = self.func(*self.args, **self.kwargs) diff --git a/tests/test_job.py b/tests/test_job.py index d715298..2e91751 100644 --- a/tests/test_job.py +++ b/tests/test_job.py @@ -345,14 +345,16 @@ class TestJob(RQTestCase): job = queue.enqueue(say_hello, ttl=ttl) self.assertEqual(job.get_ttl(), ttl) - def test_expire_during_execution(self): + def test_never_expire_during_execution(self): """Test what happens when job expires during execution""" - ttl = 2 + ttl = 1 queue = Queue(connection=self.testconn) - job = queue.enqueue(long_running_job, args=(4,), ttl=ttl) + job = queue.enqueue(long_running_job, args=(2,), ttl=ttl) self.assertEqual(job.get_ttl(), ttl) + job.save() job.perform() - self.assertFalse(job.exists(job.id)) + self.assertEqual(job.get_ttl(), -1) + self.assertTrue(job.exists(job.id)) self.assertEqual(job.result, 'Done sleeping...') def test_cleanup(self): From 071c47dacba06f55de9d9a1569ac91592dc18eb2 Mon Sep 17 00:00:00 2001 From: glaslos Date: Thu, 19 Mar 2015 15:32:01 +0100 Subject: [PATCH 5/5] hard-coded TTL to -1 on job.perform() Removed merge artifacts --- rq/job.py | 2 +- tests/test_job.py | 5 ----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/rq/job.py b/rq/job.py index 7195ddb..ffa7983 100644 --- a/rq/job.py +++ b/rq/job.py @@ -486,7 +486,7 @@ class Job(object): def perform(self): # noqa """Invokes the job function with the job arguments.""" self.connection.persist(self.key) - self.ttl = self.connection.ttl(self.key) + self.ttl = -1 _job_stack.push(self.id) try: self._result = self.func(*self.args, **self.kwargs) diff --git a/tests/test_job.py b/tests/test_job.py index 2e91751..600cefd 100644 --- a/tests/test_job.py +++ b/tests/test_job.py @@ -4,11 +4,6 @@ from __future__ import (absolute_import, division, print_function, from datetime import datetime -from tests import RQTestCase -from tests.fixtures import (access_self, CallableObject, Number, say_hello, - some_calculation) -from tests.helpers import strip_microseconds - from rq.compat import as_text, PY2 from rq.exceptions import NoSuchJobError, UnpickleError from rq.job import get_current_job, Job