From 9a851d6ccebcab6ab6a7d6cccc4134f93a07f427 Mon Sep 17 00:00:00 2001 From: Henry Oswald Date: Tue, 4 Dec 2018 13:47:04 +0000 Subject: [PATCH] mvp for safe shutdown --- services/real-time/app.coffee | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/services/real-time/app.coffee b/services/real-time/app.coffee index 0857fcc835..034dc2d915 100644 --- a/services/real-time/app.coffee +++ b/services/real-time/app.coffee @@ -78,3 +78,20 @@ server.listen port, host, (error) -> # Stop huge stack traces in logs from all the socket.io parsing steps. Error.stackTraceLimit = 10 + + +shutdownCleanly = (signal) -> + return () -> + logger.log signal: signal, "received interrupt, cleaning up" + connectedClients = io.sockets.clients()?.length + logger.log {connectedClients, signal}, "looking to shut down process" + if connectedClients == 0 + logger.log("no clients connected, exiting") + process.exit() + else + setTimeout () -> + shutdownCleanly(signal) + , 10000 + +for signal in ['SIGINT', 'SIGHUP', 'SIGQUIT', 'SIGUSR1', 'SIGUSR2', 'SIGTERM', 'SIGABRT'] + process.on signal, shutdownCleanly(signal)