From dde3ea8ef7b73ee8eb48c3e0fb204eb5c7fe3bb7 Mon Sep 17 00:00:00 2001 From: Vincent Driessen Date: Fri, 25 Nov 2011 00:45:03 +0100 Subject: [PATCH] Take down horse process when the worker is terminated. --- rq/worker.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/rq/worker.py b/rq/worker.py index c8ca4146..81ab1ad3 100644 --- a/rq/worker.py +++ b/rq/worker.py @@ -77,6 +77,7 @@ class Worker(object): self.rv_ttl = rv_ttl self._state = 'starting' self._is_horse = False + self._horse_pid = 0 self._stopped = False self.log = Logger('worker') @@ -122,6 +123,13 @@ class Worker(object): """The current process ID.""" 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 def is_horse(self): """Returns whether or not this is the worker or the work horse.""" @@ -182,6 +190,11 @@ class Worker(object): """Terminates the application (cold shutdown). """ 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() def request_stop(signum, frame): @@ -267,6 +280,7 @@ class Worker(object): sys.exit(1) sys.exit(0) else: + self._horse_pid = child_pid self.procline('Forked %d at %d' % (child_pid, time.time())) while True: try: