Skip to content

Commit

Permalink
Support an extra shutdown signal
Browse files Browse the repository at this point in the history
Beyond `SIGINT` and `SIGTERM`.
  • Loading branch information
wearhere committed Oct 11, 2016
1 parent 88c3c16 commit e3c273b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/throng.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ module.exports = function throng(options, startFunction) {
cluster.on('exit', revive);
emitter.once('shutdown', shutdown);
var shutdownSignals = ['SIGINT', 'SIGTERM'];
if (opts.extraShutdownSignal) shutdownSignals.push(opts.extraShutdownSignal);
shutdownSignals.forEach((signal) => {
process.once(signal, () => emitter.emit('shutdown', signal));
});
Expand Down
12 changes: 7 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,13 @@ Handling signals (for cleanup on a kill signal, for instance).

```js
throng({
workers: 4, // Number of workers (cpu count)
lifetime: 10000, // ms to keep cluster alive (Infinity)
grace: 4000, // ms grace period after worker SIGTERM (5000)
master: masterFn, // Function to call when starting the master process
start: startFn // Function to call when starting the worker processes
workers: 4, // Number of workers (cpu count)
lifetime: 10000, // ms to keep cluster alive (Infinity)
grace: 4000, // ms grace period after worker SIGTERM (5000)
master: masterFn, // Function to call when starting the master process
start: startFn, // Function to call when starting the worker processes
extraShutdownSignal: 'SIGUSR2' // Extra signal (beyond SIGINT & SIGTERM) in response
// to which to shut down the cluster
});
```

Expand Down
17 changes: 17 additions & 0 deletions test/fixtures/sigusr2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

const throng = require('../../lib/throng');

throng({
workers: 3,
lifetime: 500,
start: () => {
console.log('worker');

process.on('SIGTERM', function() {
console.log('exiting');
process.exit();
});
},
extraShutdownSignal: 'SIGUSR2'
});
15 changes: 15 additions & 0 deletions test/throng.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const masterCmd = path.join(__dirname, 'fixtures', 'master');
const gracefulCmd = path.join(__dirname, 'fixtures', 'graceful');
const killCmd = path.join(__dirname, 'fixtures', 'kill');
const infiniteCmd = path.join(__dirname, 'fixtures', 'infinite');
const sigusr2Cmd = path.join(__dirname, 'fixtures', 'sigusr2');

describe('throng()', function() {

Expand Down Expand Up @@ -173,6 +174,20 @@ describe('throng()', function() {
});
});

describe('with custom signal handling', function() {
before(function(done) {
var child = run(sigusr2Cmd, this, done);
setTimeout(function() { child.kill('SIGUSR2'); }, 750);
});
it('allows the workers to shut down', function() {
var exits = this.stdout.match(/exiting/g).length;
assert.equal(exits, 3);
});
it('exits with SIGUSR2', function() {
assert.equal(this.signal, 'SIGUSR2');
});
});

});

});
Expand Down

0 comments on commit e3c273b

Please sign in to comment.