Take down horse process when the worker is terminated.

This commit is contained in:
Vincent Driessen 2011-11-25 00:45:03 +01:00
parent 4ac243b3e8
commit dde3ea8ef7
1 changed files with 14 additions and 0 deletions

View File

@ -77,6 +77,7 @@ class Worker(object):
self.rv_ttl = rv_ttl self.rv_ttl = rv_ttl
self._state = 'starting' self._state = 'starting'
self._is_horse = False self._is_horse = False
self._horse_pid = 0
self._stopped = False self._stopped = False
self.log = Logger('worker') self.log = Logger('worker')
@ -122,6 +123,13 @@ class Worker(object):
"""The current process ID.""" """The current process ID."""
return os.getpid() return os.getpid()
@property
def horse_pid(self):
"""The horse's process ID. Only available in the worker. Will return
0 in the horse part of the fork.
"""
return self._horse_pid
@property @property
def is_horse(self): def is_horse(self):
"""Returns whether or not this is the worker or the work horse.""" """Returns whether or not this is the worker or the work horse."""
@ -182,6 +190,11 @@ class Worker(object):
"""Terminates the application (cold shutdown). """Terminates the application (cold shutdown).
""" """
self.log.warning('Cold shut down.') self.log.warning('Cold shut down.')
# Take down the horse with the worker
if self.horse_pid:
self.log.debug('Taking down horse %d with me.' % self.horse_pid)
os.kill(self.horse_pid, signal.SIGKILL)
raise SystemExit() raise SystemExit()
def request_stop(signum, frame): def request_stop(signum, frame):
@ -267,6 +280,7 @@ class Worker(object):
sys.exit(1) sys.exit(1)
sys.exit(0) sys.exit(0)
else: else:
self._horse_pid = child_pid
self.procline('Forked %d at %d' % (child_pid, time.time())) self.procline('Forked %d at %d' % (child_pid, time.time()))
while True: while True:
try: try: