Moved errorhandling

pull/528/head
Egil Moeller 2012-02-25 16:20:40 +01:00
parent 24dda0c54d
commit 6a6191f67e
3 changed files with 54 additions and 50 deletions

52
node/errorhandling.js Normal file
View File

@ -0,0 +1,52 @@
var db = require('./db/DB');
var os = require("os");
exports.onShutdown = false;
exports.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(exports.onShutdown) return;
exports.onShutdown = true;
console.log("graceful shutdown...");
//stop the http server
exports.app.close();
//do the db shutdown
db.db.doShutdown(function() {
console.log("db sucessfully closed.");
process.exit(0);
});
setTimeout(function(){
process.exit(1);
}, 3000);
}
exports.expressCreateServer = function (hook_name, args, cb) {
exports.app = args.app;
args.app.error(function(err, req, res, next){
res.send(500);
console.error(err.stack ? err.stack : err.toString());
exports.gracefulShutdown();
});
//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', exports.gracefulShutdown);
}
process.on('uncaughtException', exports.gracefulShutdown);
}

View File

@ -112,61 +112,12 @@ async.waterfall([
var httpLogger = log4js.getLogger("http");
app.configure(function() { hooks.callAll("expressConfigure", {"app": app}); });
app.error(function(err, req, res, next){
res.send(500);
console.error(err.stack ? err.stack : err.toString());
gracefulShutdown();
});
//let the server listen
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);
});
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);
//init socket.io and redirect all requests to the MessageHandler
var io = socketio.listen(app);

View File

@ -7,7 +7,8 @@
{ "name": "padreadonly", "hooks": { "expressCreateServer": "../padreadonly:expressServer" } },
{ "name": "webaccess", "hooks": { "expressConfigure": "../webaccess:expressConfigure" } },
{ "name": "apicalls", "hooks": { "expressCreateServer": "../apicalls:expressCreateServer" } },
{ "name": "importexport", "hooks": { "expressCreateServer": "../importexport:expressCreateServer" } }
{ "name": "importexport", "hooks": { "expressCreateServer": "../importexport:expressCreateServer" } },
{ "name": "errorhandling", "hooks": { "expressCreateServer": "../errorhandling:expressCreateServer" } }
]
}