Skip to content

Commit

Permalink
Allow tornado to exit with two ctrl+c
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Friedman <25047695+Ryanf55@users.noreply.github.com>
  • Loading branch information
Ryanf55 committed Nov 8, 2024
1 parent 55b2748 commit 1abfba6
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions app/cesium_web_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 1abfba6

Please sign in to comment.