From 296777ab53b0d19be6a48597fa0a3d8fb1f8e91f Mon Sep 17 00:00:00 2001 From: Peter 'Pita' Martischka Date: Wed, 17 Aug 2011 15:58:42 +0100 Subject: [PATCH] added a graceful shutdown --- node/server.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/node/server.js b/node/server.js index bb2151ed..56f58d94 100644 --- a/node/server.js +++ b/node/server.js @@ -334,6 +334,40 @@ async.waterfall([ app.listen(settings.port, settings.ip); console.log("Server is listening at " + settings.ip + ":" + settings.port); + var onShutdown = false; + var gracefulShutdown = function(err) + { + if(err && err.stack) + { + console.error(err.stack); + } + else if(err) + { + console.error(err); + } + + //ensure there is only one graceful shutdown running + if(onShutdown) return; + onShutdown = true; + + console.log("graceful shutdown..."); + + //stop the http server + app.close(); + + //do the db shutdown + db.db.doShutdown(function() + { + console.log("db sucessfully closed."); + + process.exit(0); + }); + } + + //connect graceful shutdown with sigint and uncaughtexception + process.on('SIGINT', gracefulShutdown); + process.on('uncaughtException', gracefulShutdown); + //init socket.io and redirect all requests to the MessageHandler var io = socketio.listen(app);