From 6a6191f67efa0b044abd8c773dec9286ccd124db Mon Sep 17 00:00:00 2001 From: Egil Moeller Date: Sat, 25 Feb 2012 16:20:40 +0100 Subject: [PATCH] Moved errorhandling --- node/errorhandling.js | 52 +++++++++++++++++++++++++++++++++++++++++++ node/server.js | 49 ---------------------------------------- pluginomatic.json | 3 ++- 3 files changed, 54 insertions(+), 50 deletions(-) create mode 100644 node/errorhandling.js diff --git a/node/errorhandling.js b/node/errorhandling.js new file mode 100644 index 00000000..1e8a734e --- /dev/null +++ b/node/errorhandling.js @@ -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); +} diff --git a/node/server.js b/node/server.js index f97afd0f..abe12564 100644 --- a/node/server.js +++ b/node/server.js @@ -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); diff --git a/pluginomatic.json b/pluginomatic.json index 996aacdc..bb2e0473 100644 --- a/pluginomatic.json +++ b/pluginomatic.json @@ -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" } } ] }