From 00d7ea51fd1633a141b2e97291baa425be1680c8 Mon Sep 17 00:00:00 2001 From: Tobias Henkel Date: Wed, 27 Dec 2017 10:31:40 +0100 Subject: Register term_handler for all zuul apps Almost all zuul apps use the method term_handler for SIGINT and SIGTERM. Defining this centrally in ZuulDaemonApp makes this much simpler and without repitition. Change-Id: I68f8d1bf52b0e16340818d2bcc44cd9fc5868ca7 --- zuul/cmd/__init__.py | 10 ++++++++++ zuul/cmd/executor.py | 14 +------------- zuul/cmd/fingergw.py | 18 ++++-------------- zuul/cmd/merger.py | 14 +------------- zuul/cmd/scheduler.py | 13 +------------ zuul/cmd/web.py | 6 ------ 6 files changed, 17 insertions(+), 58 deletions(-) diff --git a/zuul/cmd/__init__.py b/zuul/cmd/__init__.py index 2aad4eb39..4ee7e64ab 100755 --- a/zuul/cmd/__init__.py +++ b/zuul/cmd/__init__.py @@ -170,6 +170,14 @@ class ZuulDaemonApp(ZuulApp, metaclass=abc.ABCMeta): expand_user=True) return pid_fn + @abc.abstractmethod + def exit_handler(self, signum, frame): + """ + This is a signal handler which is called on SIGINT and SIGTERM and must + take care of stopping the application. + """ + pass + @abc.abstractmethod def run(self): """ @@ -189,6 +197,8 @@ class ZuulDaemonApp(ZuulApp, metaclass=abc.ABCMeta): signal.signal(signal.SIGUSR2, stack_dump_handler) if self.args.nodaemon: + signal.signal(signal.SIGTERM, self.exit_handler) + signal.signal(signal.SIGINT, self.exit_handler) self.run() else: # Exercise the pidfile before we do anything else (including diff --git a/zuul/cmd/executor.py b/zuul/cmd/executor.py index c600dc96d..bf85650cd 100755 --- a/zuul/cmd/executor.py +++ b/zuul/cmd/executor.py @@ -19,7 +19,6 @@ import logging import os import pwd import sys -import signal import tempfile import zuul.cmd @@ -53,8 +52,6 @@ class Executor(zuul.cmd.ZuulDaemonApp): def exit_handler(self, signum, frame): self.executor.stop() - self.executor.join() - sys.exit(0) def start_log_streamer(self): pipe_read, pipe_write = os.pipe() @@ -130,16 +127,7 @@ class Executor(zuul.cmd.ZuulDaemonApp): log_streaming_port=self.finger_port) self.executor.start() - if self.args.nodaemon: - signal.signal(signal.SIGTERM, self.exit_handler) - while True: - try: - signal.pause() - except KeyboardInterrupt: - print("Ctrl + C: asking executor to exit nicely...\n") - self.exit_handler(signal.SIGINT, None) - else: - self.executor.join() + self.executor.join() def main(): diff --git a/zuul/cmd/fingergw.py b/zuul/cmd/fingergw.py index 920eed8f2..0d47f0848 100644 --- a/zuul/cmd/fingergw.py +++ b/zuul/cmd/fingergw.py @@ -14,7 +14,6 @@ # under the License. import logging -import signal import sys import zuul.cmd @@ -47,6 +46,9 @@ class FingerGatewayApp(zuul.cmd.ZuulDaemonApp): if self.args.command: self.args.nodaemon = True + def exit_handler(self, signum, frame): + self.stop() + def run(self): ''' Main entry point for the FingerGatewayApp. @@ -84,19 +86,7 @@ class FingerGatewayApp(zuul.cmd.ZuulDaemonApp): self.log.info('Starting Zuul finger gateway app') self.gateway.start() - if self.args.nodaemon: - # NOTE(Shrews): When running in non-daemon mode, although sending - # the 'stop' command via the command socket will shutdown the - # gateway, it's still necessary to Ctrl+C to stop the app. - while True: - try: - signal.pause() - except KeyboardInterrupt: - print("Ctrl + C: asking gateway to exit nicely...\n") - self.stop() - break - else: - self.gateway.wait() + self.gateway.wait() self.log.info('Stopped Zuul finger gateway app') diff --git a/zuul/cmd/merger.py b/zuul/cmd/merger.py index 8c4798923..390191f4a 100755 --- a/zuul/cmd/merger.py +++ b/zuul/cmd/merger.py @@ -14,7 +14,6 @@ # License for the specific language governing permissions and limitations # under the License. -import signal import sys import zuul.cmd @@ -44,8 +43,6 @@ class Merger(zuul.cmd.ZuulDaemonApp): def exit_handler(self, signum, frame): self.merger.stop() - self.merger.join() - sys.exit(0) def run(self): # See comment at top of file about zuul imports @@ -62,16 +59,7 @@ class Merger(zuul.cmd.ZuulDaemonApp): self.connections) self.merger.start() - if self.args.nodaemon: - signal.signal(signal.SIGTERM, self.exit_handler) - while True: - try: - signal.pause() - except KeyboardInterrupt: - print("Ctrl + C: asking merger to exit nicely...\n") - self.exit_handler(signal.SIGINT, None) - else: - self.merger.join() + self.merger.join() def main(): diff --git a/zuul/cmd/scheduler.py b/zuul/cmd/scheduler.py index 7748a80a7..3cffa1010 100755 --- a/zuul/cmd/scheduler.py +++ b/zuul/cmd/scheduler.py @@ -63,9 +63,7 @@ class Scheduler(zuul.cmd.ZuulDaemonApp): def exit_handler(self, signum, frame): self.sched.exit() - self.sched.join() self.stop_gear_server() - sys.exit(0) def start_gear_server(self): pipe_read, pipe_write = os.pipe() @@ -175,16 +173,7 @@ class Scheduler(zuul.cmd.ZuulDaemonApp): signal.signal(signal.SIGHUP, self.reconfigure_handler) - if self.args.nodaemon: - signal.signal(signal.SIGTERM, self.exit_handler) - while True: - try: - signal.pause() - except KeyboardInterrupt: - print("Ctrl + C: asking scheduler to exit nicely...\n") - self.exit_handler(signal.SIGINT, None) - else: - self.sched.join() + self.sched.join() def main(): diff --git a/zuul/cmd/web.py b/zuul/cmd/web.py index ad3062ff0..78392db51 100755 --- a/zuul/cmd/web.py +++ b/zuul/cmd/web.py @@ -89,12 +89,6 @@ class WebServer(zuul.cmd.ZuulDaemonApp): name='web') self.thread.start() - try: - signal.pause() - except KeyboardInterrupt: - print("Ctrl + C: asking web server to exit nicely...\n") - self.exit_handler(signal.SIGINT, None) - self.thread.join() loop.stop() loop.close() -- cgit v1.2.1