diff --git a/lib/app/lower.js b/lib/app/lower.js index 6f2115e98..81aa888a8 100644 --- a/lib/app/lower.js +++ b/lib/app/lower.js @@ -111,6 +111,15 @@ module.exports = function lower(cb) { sails.removeAllListeners(key); } + var listeners = sails._processListeners; + if (listeners) { + process.removeListener('SIGUSR2', listeners.sigusr2); + process.removeListener('SIGINT', listeners.sigint); + process.removeListener('SIGTERM', listeners.sigterm); + process.removeListener('exit', listeners.exit); + } + sails._processListeners = null; + // If `sails.config.process.removeAllListeners` is set, do that. if (sails.config && sails.config.process && sails.config.process.removeAllListeners) { process.removeAllListeners(); diff --git a/lib/app/private/initialize.js b/lib/app/private/initialize.js index 7d30996ed..6a4187d5c 100644 --- a/lib/app/private/initialize.js +++ b/lib/app/private/initialize.js @@ -24,25 +24,31 @@ module.exports = function initialize(cb) { // Indicate that server is starting sails.log.verbose('Starting app at ' + sails.config.appPath + '...'); + var listeners = { + sigusr2: function() { + sails.lower(function() { + process.kill(process.pid, 'SIGUSR2'); + }); + }, + sigint: function() { + sails.lower(process.exit); + }, + sigterm: function() { + sails.lower(process.exit); + }, + exit: function() { + if (!sails._exiting) sails.lower(); + } + }; + // Add "beforeShutdown" events - process.once('SIGUSR2', function() { - sails.lower(function() { - process.kill(process.pid, 'SIGUSR2'); - }); - }); + process.once('SIGUSR2', listeners.sigusr2); - // Roadmap: can we get away w/ making this `.once()`? - process.on('SIGINT', function() { - sails.lower(process.exit); - }); - // Roadmap: can we get away w/ making this `.once()`? - process.on('SIGTERM', function() { - sails.lower(process.exit); - }); - // Roadmap: can we get away w/ making this `.once()`? - process.on('exit', function() { - if (!sails._exiting) sails.lower(); - }); + process.on('SIGINT', listeners.sigint); + process.on('SIGTERM', listeners.sigterm); + process.on('exit', listeners.exit); + + sails._processListeners = listeners; // Run the app bootstrap sails.runBootstrap(function afterBootstrap(err) {