Merge pull request #633 from olingerc/warmshutdown

Save date on which a busy worker receives a warm shutdown request
main
Selwin Ong 9 years ago
commit 640b2e31a0

@ -257,6 +257,10 @@ class Worker(object):
p.expire(self.key, 60) p.expire(self.key, 60)
p.execute() p.execute()
def set_shutdown_requested_date(self):
"""Sets the date on which the worker received a (warm) shutdown request"""
self.connection.hset(self.key, 'shutdown_requested_date', utcformat(utcnow()))
@property @property
def birth_date(self): def birth_date(self):
"""Fetches birth date from Redis.""" """Fetches birth date from Redis."""
@ -264,6 +268,13 @@ class Worker(object):
if birth_timestamp is not None: if birth_timestamp is not None:
return utcparse(as_text(birth_timestamp)) return utcparse(as_text(birth_timestamp))
@property
def shutdown_requested_date(self):
"""Fetches shutdown_requested_date from Redis."""
shutdown_requested_timestamp = self.connection.hget(self.key, 'shutdown_requested_date')
if shutdown_requested_timestamp is not None:
return utcparse(as_text(shutdown_requested_timestamp))
@property @property
def death_date(self): def death_date(self):
"""Fetches death date from Redis.""" """Fetches death date from Redis."""
@ -357,9 +368,10 @@ class Worker(object):
self.log.warning(msg) self.log.warning(msg)
# If shutdown is requested in the middle of a job, wait until # If shutdown is requested in the middle of a job, wait until
# finish before shutting down # finish before shutting down and save the request in redis
if self.get_state() == 'busy': if self.get_state() == 'busy':
self._stop_requested = True self._stop_requested = True
self.set_shutdown_requested_date()
self.log.debug('Stopping after current horse is finished. ' self.log.debug('Stopping after current horse is finished. '
'Press Ctrl+C again for a cold shutdown.') 'Press Ctrl+C again for a cold shutdown.')
else: else:

@ -561,6 +561,10 @@ class TestWorkerShutdown(RQTestCase):
self.assertTrue(w._stop_requested) self.assertTrue(w._stop_requested)
self.assertTrue(os.path.exists(sentinel_file)) self.assertTrue(os.path.exists(sentinel_file))
shutdown_requested_date = w.shutdown_requested_date
self.assertIsNotNone(shutdown_requested_date)
self.assertEqual(type(shutdown_requested_date).__name__, 'datetime')
@slow @slow
def test_working_worker_cold_shutdown(self): def test_working_worker_cold_shutdown(self):
"""worker with an ongoing job receiving double SIGTERM signal and shutting down immediately""" """worker with an ongoing job receiving double SIGTERM signal and shutting down immediately"""
@ -578,3 +582,6 @@ class TestWorkerShutdown(RQTestCase):
self.assertTrue(w._stop_requested) self.assertTrue(w._stop_requested)
self.assertFalse(os.path.exists(sentinel_file)) self.assertFalse(os.path.exists(sentinel_file))
shutdown_requested_date = w.shutdown_requested_date
self.assertIsNotNone(shutdown_requested_date)
self.assertEqual(type(shutdown_requested_date).__name__, 'datetime')

Loading…
Cancel
Save