Skip to content

Commit

Permalink
Properly add/cleanup process event listeners.
Browse files Browse the repository at this point in the history
Fixes warnings when doing lift/lower multiple times, e.g in before() and
after() functions in a test suite.

See test/integration/lift.lower.test.js
  • Loading branch information
haavardw committed Mar 25, 2015
1 parent 6eaf671 commit ae3c428
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
9 changes: 9 additions & 0 deletions lib/app/lower.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
40 changes: 23 additions & 17 deletions lib/app/private/initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit ae3c428

Please sign in to comment.