Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #465 from marklap/workerbirthdeath

add birth_date and death_date properties to Worker
commit 30b21ef06ef692788e6905631fc110b117c94d7e 2 parents 09b08e4 + 636a537
Selwin Ong selwin authored
Showing with 38 additions and 1 deletion.
  1. +16 −1 rq/worker.py
  2. +22 −0 tests/test_worker.py
17 rq/worker.py
View
@@ -23,7 +23,7 @@
from .registry import FinishedJobRegistry, StartedJobRegistry
from .suspension import is_suspended
from .timeouts import UnixSignalDeathPenalty
-from .utils import enum, import_attribute, make_colorizer, utcformat, utcnow
+from .utils import enum, import_attribute, make_colorizer, utcformat, utcnow, utcparse
from .version import VERSION
try:
@@ -245,6 +245,21 @@ def register_death(self):
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 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 is not None:
+ return utcparse(as_text(death_timestamp))
+
+
def set_state(self, state, pipeline=None):
self._state = state
connection = pipeline if pipeline is not None else self.connection
22 tests/test_worker.py
View
@@ -379,3 +379,25 @@ def test_worker_hash_(self):
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])
+
+ w.register_birth()
+
+ 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 death 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')
Please sign in to comment.
Something went wrong with that request. Please try again.