factored out shutdown setup

This commit is contained in:
Montana Scott Rowe 2012-01-19 10:14:14 -06:00
parent 98f11bea6d
commit e1159f0ee0

View file

@ -181,6 +181,51 @@ function setupIo(socketio, log4js, settings, socketIORouter, app){
socketIORouter.addComponent("timeslider", timesliderMessageHandler); socketIORouter.addComponent("timeslider", timesliderMessageHandler);
return io; return io;
} }
function setupShutdown(db, app){
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);
});
setTimeout(function(){
process.exit(1);
}, 3000);
}
//connect graceful shutdown with sigint and uncaughtexception
if(os.type().indexOf("Windows") == -1)
{
//sigint is so far not working on windows
//https://github.com/joyent/node/issues/1553
process.on('SIGINT', gracefulShutdown);
}
return process.on('uncaughtException', gracefulShutdown);
}
async.waterfall([ async.waterfall([
//initalize the database //initalize the database
setupDb, setupDb,
@ -526,49 +571,7 @@ async.waterfall([
app.listen(settings.port, settings.ip); app.listen(settings.port, settings.ip);
console.log("Server is listening at " + settings.ip + ":" + settings.port); console.log("Server is listening at " + settings.ip + ":" + settings.port);
var onShutdown = false; setupShutdown(db, app);
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);
});
setTimeout(function(){
process.exit(1);
}, 3000);
}
//connect graceful shutdown with sigint and uncaughtexception
if(os.type().indexOf("Windows") == -1)
{
//sigint is so far not working on windows
//https://github.com/joyent/node/issues/1553
process.on('SIGINT', gracefulShutdown);
}
process.on('uncaughtException', gracefulShutdown);
var io = setupIo(socketio, log4js, settings, socketIORouter, app); var io = setupIo(socketio, log4js, settings, socketIORouter, app);