From e6c32edad9da044b66deb9d9a612f4acb02e2090 Mon Sep 17 00:00:00 2001 From: Mark LaPerriere Date: Fri, 19 Dec 2014 14:17:17 -0500 Subject: [PATCH 1/5] add birth_date and death_date properties to Worker --- rq/worker.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/rq/worker.py b/rq/worker.py index 7283d56..73347c7 100644 --- a/rq/worker.py +++ b/rq/worker.py @@ -21,7 +21,7 @@ from .job import Job, Status from .logutils import setup_loghandlers from .queue import get_failed_queue, Queue from .timeouts import UnixSignalDeathPenalty -from .utils import import_attribute, make_colorizer, utcformat, utcnow +from .utils import import_attribute, make_colorizer, utcformat, utcnow, utcparse from .version import VERSION from .registry import FinishedJobRegistry, StartedJobRegistry @@ -235,6 +235,21 @@ class Worker(object): p.expire(self.key, 60) p.execute() + @property + def birth_date(self): + """Fetches birth date from Redis.""" + birth_timestamp = self.connection.hget(self.key, 'birth') + if birth_timestamp: + return utcparse(birth_timestamp) + + @property + def death_date(self): + """Fetches death date from Redis.""" + death_timestamp = self.connection.hget(self.key, 'death') + if death_timestamp: + return utcparse(death_timestamp) + + def set_state(self, state, pipeline=None): self._state = state connection = pipeline if pipeline is not None else self.connection From af5a8624a69fc3f528a0489984a272ff79a4ef0b Mon Sep 17 00:00:00 2001 From: Mark LaPerriere Date: Thu, 5 Feb 2015 23:05:10 -0500 Subject: [PATCH 2/5] add tests for birth and death worker methods --- tests/test_worker.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/test_worker.py b/tests/test_worker.py index c47a62e..e89f39b 100644 --- a/tests/test_worker.py +++ b/tests/test_worker.py @@ -379,3 +379,23 @@ class TestWorker(RQTestCase): w3 = Worker([q], name="worker1") worker_set = set([w1, w2, w3]) self.assertEquals(len(worker_set), 2) + + def test_worker_sets_birth(self): + """Ensure worker correctly sets worker birth date.""" + q = Queue() + w = Worker([q]) + + birth_date = w.birth_date + self.assertIsNotNone(birth_date) + self.assertEquals(type(birth_date).__name__, 'datetime') + + def test_worker_sets_death(self): + """Ensure worker correctly sets worker birth date.""" + q = Queue() + w = Worker([q]) + + w.register_death() + + death_date = w.death_date + self.assertIsNotNone(death_date) + self.assertEquals(type(death_date).__name__, 'datetime') From 450c5969aa4afd0550c130e12c55213b111a1108 Mon Sep 17 00:00:00 2001 From: Mark LaPerriere Date: Thu, 5 Feb 2015 23:12:33 -0500 Subject: [PATCH 3/5] call register birth in the register birth test --- tests/test_worker.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_worker.py b/tests/test_worker.py index e89f39b..65edead 100644 --- a/tests/test_worker.py +++ b/tests/test_worker.py @@ -385,6 +385,8 @@ class TestWorker(RQTestCase): q = Queue() w = Worker([q]) + w.register_birth() + birth_date = w.birth_date self.assertIsNotNone(birth_date) self.assertEquals(type(birth_date).__name__, 'datetime') From a4ca4704ace1c1e027543ecaf1f154ad561016c6 Mon Sep 17 00:00:00 2001 From: Mark LaPerriere Date: Thu, 5 Feb 2015 23:17:41 -0500 Subject: [PATCH 4/5] convert redis returned bytes into text/string --- rq/worker.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rq/worker.py b/rq/worker.py index c863b56..6c351c8 100644 --- a/rq/worker.py +++ b/rq/worker.py @@ -250,14 +250,14 @@ class Worker(object): """Fetches birth date from Redis.""" birth_timestamp = self.connection.hget(self.key, 'birth') if birth_timestamp: - return utcparse(birth_timestamp) + return utcparse(as_text(birth_timestamp)) @property def death_date(self): """Fetches death date from Redis.""" death_timestamp = self.connection.hget(self.key, 'death') if death_timestamp: - return utcparse(death_timestamp) + return utcparse(as_text(death_timestamp)) def set_state(self, state, pipeline=None): From 636a537fa7bb4fe31e3c5e30d79e2dfe4a460e45 Mon Sep 17 00:00:00 2001 From: Mark LaPerriere Date: Thu, 26 Feb 2015 09:41:20 -0500 Subject: [PATCH 5/5] updates addressing @selwin comments for PR #465 --- rq/worker.py | 4 ++-- tests/test_worker.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/rq/worker.py b/rq/worker.py index 6c351c8..a32c6d9 100644 --- a/rq/worker.py +++ b/rq/worker.py @@ -249,14 +249,14 @@ class Worker(object): def birth_date(self): """Fetches birth date from Redis.""" birth_timestamp = self.connection.hget(self.key, 'birth') - if birth_timestamp: + if birth_timestamp is not None: return utcparse(as_text(birth_timestamp)) @property def death_date(self): """Fetches death date from Redis.""" death_timestamp = self.connection.hget(self.key, 'death') - if death_timestamp: + if death_timestamp is not None: return utcparse(as_text(death_timestamp)) diff --git a/tests/test_worker.py b/tests/test_worker.py index 65edead..6f89f4a 100644 --- a/tests/test_worker.py +++ b/tests/test_worker.py @@ -392,7 +392,7 @@ class TestWorker(RQTestCase): self.assertEquals(type(birth_date).__name__, 'datetime') def test_worker_sets_death(self): - """Ensure worker correctly sets worker birth date.""" + """Ensure worker correctly sets worker death date.""" q = Queue() w = Worker([q])