From e750134e8aa8ee6735666df292767e8a6940e04f Mon Sep 17 00:00:00 2001 From: Samuel Colvin Date: Wed, 14 Oct 2015 19:03:59 +0100 Subject: [PATCH] move request_(force_)stop out of _install_signal_handlers --- rq/worker.py | 82 ++++++++++++++++++++++++++-------------------------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/rq/worker.py b/rq/worker.py index 64efdba..ab3a39a 100644 --- a/rq/worker.py +++ b/rq/worker.py @@ -323,47 +323,47 @@ class Worker(object): gracefully. """ - def request_force_stop(signum, frame): - """Terminates the application (cold shutdown). - """ - self.log.warning('Cold shut down') - - # Take down the horse with the worker - if self.horse_pid: - msg = 'Taking down horse {0} with me'.format(self.horse_pid) - self.log.debug(msg) - try: - 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() - - def request_stop(signum, frame): - """Stops the current worker loop but waits for child processes to - end gracefully (warm shutdown). - """ - self.log.debug('Got signal {0}'.format(signal_name(signum))) - - signal.signal(signal.SIGINT, request_force_stop) - signal.signal(signal.SIGTERM, request_force_stop) - - msg = 'Warm shut down requested' - self.log.warning(msg) - - # If shutdown is requested in the middle of a job, wait until - # finish before shutting down - if self.get_state() == 'busy': - self._stop_requested = True - self.log.debug('Stopping after current horse is finished. ' - 'Press Ctrl+C again for a cold shutdown.') - else: - raise StopRequested() - - signal.signal(signal.SIGINT, request_stop) - signal.signal(signal.SIGTERM, request_stop) + signal.signal(signal.SIGINT, self.request_stop) + signal.signal(signal.SIGTERM, self.request_stop) + + def request_force_stop(self, signum, frame): + """Terminates the application (cold shutdown). + """ + self.log.warning('Cold shut down') + + # Take down the horse with the worker + if self.horse_pid: + msg = 'Taking down horse {0} with me'.format(self.horse_pid) + self.log.debug(msg) + try: + 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() + + def request_stop(self, signum, frame): + """Stops the current worker loop but waits for child processes to + end gracefully (warm shutdown). + """ + self.log.debug('Got signal {0}'.format(signal_name(signum))) + + signal.signal(signal.SIGINT, self.request_force_stop) + signal.signal(signal.SIGTERM, self.request_force_stop) + + msg = 'Warm shut down requested' + self.log.warning(msg) + + # If shutdown is requested in the middle of a job, wait until + # finish before shutting down + if self.get_state() == 'busy': + self._stop_requested = True + self.log.debug('Stopping after current horse is finished. ' + 'Press Ctrl+C again for a cold shutdown.') + else: + raise StopRequested() def check_for_suspension(self, burst): """Check to see if workers have been suspended by `rq suspend`"""