2012-02-25 16:20:40 +01:00
|
|
|
var os = require("os");
|
2012-02-25 17:23:44 +01:00
|
|
|
var db = require('../../db/DB');
|
2013-10-27 21:43:32 +01:00
|
|
|
var stats = require('ep_etherpad-lite/node/stats')
|
2012-02-25 16:20:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
exports.onShutdown = false;
|
|
|
|
exports.gracefulShutdown = function(err) {
|
|
|
|
if(err && err.stack) {
|
|
|
|
console.error(err.stack);
|
|
|
|
} else if(err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
|
2019-02-08 23:20:57 +01:00
|
|
|
// ensure there is only one graceful shutdown running
|
|
|
|
if (exports.onShutdown) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-02-25 16:20:40 +01:00
|
|
|
exports.onShutdown = true;
|
|
|
|
|
|
|
|
console.log("graceful shutdown...");
|
|
|
|
|
2019-02-08 23:20:57 +01:00
|
|
|
// do the db shutdown
|
2019-01-31 12:14:27 +01:00
|
|
|
db.doShutdown().then(function() {
|
2012-02-25 16:20:40 +01:00
|
|
|
console.log("db sucessfully closed.");
|
|
|
|
|
|
|
|
process.exit(0);
|
|
|
|
});
|
|
|
|
|
2019-02-08 23:20:57 +01:00
|
|
|
setTimeout(function() {
|
2012-02-25 16:20:40 +01:00
|
|
|
process.exit(1);
|
|
|
|
}, 3000);
|
|
|
|
}
|
|
|
|
|
2013-03-26 21:19:09 +01:00
|
|
|
process.on('uncaughtException', exports.gracefulShutdown);
|
2012-02-25 16:20:40 +01:00
|
|
|
|
|
|
|
exports.expressCreateServer = function (hook_name, args, cb) {
|
|
|
|
exports.app = args.app;
|
|
|
|
|
2012-09-22 15:22:15 +02:00
|
|
|
// Handle errors
|
2019-02-08 23:20:57 +01:00
|
|
|
args.app.use(function(err, req, res, next) {
|
2012-09-12 20:34:33 +02:00
|
|
|
// if an error occurs Connect will pass it down
|
|
|
|
// through these "error-handling" middleware
|
|
|
|
// allowing you to respond however you like
|
2015-04-10 21:10:55 +02:00
|
|
|
res.status(500).send({ error: 'Sorry, something bad happened!' });
|
2012-09-22 15:22:15 +02:00
|
|
|
console.error(err.stack? err.stack : err.toString());
|
2013-10-27 21:43:32 +01:00
|
|
|
stats.meter('http500').mark()
|
2019-02-08 23:20:57 +01:00
|
|
|
});
|
2012-09-12 20:34:33 +02:00
|
|
|
|
2019-02-16 00:14:39 +01:00
|
|
|
/*
|
|
|
|
* Connect graceful shutdown with sigint and uncaught exception
|
|
|
|
*
|
|
|
|
* Until Etherpad 1.7.5, process.on('SIGTERM') and process.on('SIGINT') were
|
|
|
|
* not hooked up under Windows, because old nodejs versions did not support
|
|
|
|
* them.
|
|
|
|
*
|
|
|
|
* According to nodejs 6.x documentation, it is now safe to do so. This
|
|
|
|
* allows to gracefully close the DB connection when hitting CTRL+C under
|
|
|
|
* Windows, for example.
|
|
|
|
*
|
|
|
|
* Source: https://nodejs.org/docs/latest-v6.x/api/process.html#process_signal_events
|
|
|
|
*
|
|
|
|
* - SIGTERM is not supported on Windows, it can be listened on.
|
|
|
|
* - SIGINT from the terminal is supported on all platforms, and can usually
|
|
|
|
* be generated with <Ctrl>+C (though this may be configurable). It is not
|
|
|
|
* generated when terminal raw mode is enabled.
|
|
|
|
*/
|
|
|
|
process.on('SIGINT', exports.gracefulShutdown);
|
|
|
|
|
|
|
|
// when running as PID1 (e.g. in docker container)
|
|
|
|
// allow graceful shutdown on SIGTERM c.f. #3265
|
|
|
|
process.on('SIGTERM', exports.gracefulShutdown);
|
2015-04-10 21:10:55 +02:00
|
|
|
}
|