allow for already dead horse on heroku worker termination

main
Samuel Colvin 8 years ago
parent 4fc032bfbf
commit b4b99f30b0

@ -352,6 +352,19 @@ class Worker(object):
signal.signal(signal.SIGINT, self.request_stop) signal.signal(signal.SIGINT, self.request_stop)
signal.signal(signal.SIGTERM, self.request_stop) signal.signal(signal.SIGTERM, self.request_stop)
def kill_horse(self):
"""
Kill the horse but catch "No such process" error has the horse could already be dead.
"""
try:
os.kill(self.horse_pid, signal.SIGKILL)
except OSError as e:
if e.errno == errno.ESRCH:
# "No such process" is fine with us
self.log.debug('Horse already dead')
else:
raise
def request_force_stop(self, signum, frame): def request_force_stop(self, signum, frame):
"""Terminates the application (cold shutdown). """Terminates the application (cold shutdown).
""" """
@ -361,13 +374,7 @@ class Worker(object):
if self.horse_pid: if self.horse_pid:
msg = 'Taking down horse {0} with me'.format(self.horse_pid) msg = 'Taking down horse {0} with me'.format(self.horse_pid)
self.log.debug(msg) self.log.debug(msg)
try: self.kill_horse()
os.kill(self.horse_pid, signal.SIGKILL)
except OSError as e:
# ESRCH ("No such process") is fine with us
if e.errno != errno.ESRCH:
self.log.debug('Horse already down')
raise
raise SystemExit() raise SystemExit()
def request_stop(self, signum, frame): def request_stop(self, signum, frame):
@ -834,7 +841,7 @@ class HerokuWorker(Worker):
"""If horse is alive send it SIGRTMIN""" """If horse is alive send it SIGRTMIN"""
if self.horse_pid != 0: if self.horse_pid != 0:
self.log.warning('Warm shut down requested, sending horse SIGRTMIN signal') self.log.warning('Warm shut down requested, sending horse SIGRTMIN signal')
os.kill(self.horse_pid, signal.SIGRTMIN) self.kill_horse()
else: else:
self.log.warning('Warm shut down requested, no horse found') self.log.warning('Warm shut down requested, no horse found')

@ -833,7 +833,7 @@ class HerokuWorkerShutdownTestCase(TimeoutTestCase, RQTestCase):
w._horse_pid = p.pid w._horse_pid = p.pid
w.handle_warm_shutdown_request() w.handle_warm_shutdown_request()
p.join(2) p.join(2)
self.assertEqual(p.exitcode, -34) self.assertEqual(p.exitcode, -9)
self.assertFalse(os.path.exists(path)) self.assertFalse(os.path.exists(path))
def test_handle_shutdown_request_no_horse(self): def test_handle_shutdown_request_no_horse(self):
@ -842,5 +842,4 @@ class HerokuWorkerShutdownTestCase(TimeoutTestCase, RQTestCase):
w = HerokuWorker('foo') w = HerokuWorker('foo')
w._horse_pid = 19999 w._horse_pid = 19999
with self.assertRaises(OSError):
w.handle_warm_shutdown_request() w.handle_warm_shutdown_request()

Loading…
Cancel
Save