Skip to content

Commit

Permalink
Fixed an issue where worker.refresh() may fail if last_heartbeat is n…
Browse files Browse the repository at this point in the history
…ot present in Redis.
  • Loading branch information
selwin committed Nov 3, 2017
1 parent 25ced94 commit 7b9c3b6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 4 additions & 1 deletion rq/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,10 @@ def refresh(self):
queues = as_text(queues)
self._state = as_text(state or '?')
self._job_id = job_id or None
self.last_heartbeat = utcparse(as_text(last_heartbeat))
if last_heartbeat:
self.last_heartbeat = utcparse(as_text(last_heartbeat))
else:
self.last_heartbeat = None
self.birth_date = utcparse(as_text(birth))
if failed_job_count:
self.failed_job_count = int(as_text(failed_job_count))
Expand Down
8 changes: 6 additions & 2 deletions tests/test_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,16 @@ def test_heartbeat(self):
q = Queue()
w = Worker([q])
w.register_birth()
w.heartbeat()
last_heartbeat = self.testconn.hget(w.key, 'last_heartbeat')

self.assertTrue(last_heartbeat is not None)
w = Worker.find_by_key(w.key)
self.assertIsInstance(w.last_heartbeat, datetime)
self.assertIsInstance(w.last_heartbeat, datetime)

# worker.refresh() shouldn't fail if last_heartbeat is None
# for compatibility reasons
self.testconn.hdel(w.key, 'last_heartbeat')
w.refresh()

def test_work_fails(self):
"""Failing jobs are put on the failed queue."""
Expand Down

0 comments on commit 7b9c3b6

Please sign in to comment.