From 1abfba6e559fce79fa8d1bedd5f8208bb64a9878 Mon Sep 17 00:00:00 2001 From: Ryan Friedman <25047695+Ryanf55@users.noreply.github.com> Date: Fri, 8 Nov 2024 01:02:58 -0700 Subject: [PATCH] Allow tornado to exit with two ctrl+c Signed-off-by: Ryan Friedman <25047695+Ryanf55@users.noreply.github.com> --- app/cesium_web_server.py | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/app/cesium_web_server.py b/app/cesium_web_server.py index b17f8cf..6faf76b 100644 --- a/app/cesium_web_server.py +++ b/app/cesium_web_server.py @@ -114,14 +114,35 @@ def close_all_websockets(): for ws in removable: live_web_sockets.remove(ws) lock.release() - + def stop_tornado(config): - close_all_websockets() - ioloop = tornado.ioloop.IOLoop.current() - ioloop.add_callback(ioloop.stop) + if config.APP_DEBUG: print("Asked Tornado to exit") + # Tornado doesn't stop well with the current architecture. + # Try this approach so we don't have to result to sigterm in CLI. + # https://github.com/tornadoweb/tornado/issues/1791#issuecomment-238214198 + + ioloop = tornado.ioloop.IOLoop.instance() + def register_signal(sig, frame): + global signal_received + print("%s received, stopping server" % sig) + close_all_websockets() + ioloop.add_callback(ioloop.stop) + signal_received = True + + def stop_on_signal(): + global signal_received + if signal_received and not ioloop._callbacks: + ioloop.stop() + print("IOLoop stopped") + + tornado.ioloop.PeriodicCallback(stop_on_signal, 1000).start() + signal.signal(signal.SIGTERM, register_signal) + logging.info("Starting server") + ioloop.start() + def websocket_send_message(message): removable = set() lock.acquire()