Skip to content

Commit

Permalink
fix(process): terminate child processes on SIGINT and SIGTERM
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Mollweide committed Jan 24, 2018
1 parent abe8468 commit 3287edc
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/handleKillProcess/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';
const resolve = require('../resolve');
const cmdStop = require('../cmdStop');

/**
* @param {Object} di - dependency injection
* @returns {void}
**/
function handleKillProcess({ _process, _cmdStop }) {
const handleKill = () => _cmdStop(undefined, _process.exit);

// catch ctrl-c
_process.on('SIGINT', handleKill);
// catch kill
_process.on('SIGTERM', handleKill);
}

module.exports = resolve(handleKillProcess, {
process,
cmdStop,
});
module.exports.handleKillProcess = handleKillProcess;
58 changes: 58 additions & 0 deletions src/handleKillProcess/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const resolve = require('../resolve');
const handleKillProcess = require('./index');

const _getProcess = cmd => ({
on(type, cb) {
if (type === cmd) {
cb();
}
},
});

describe('handleKillProcess', () => {
it('cmdStop on SIGINT', done => {
resolve(handleKillProcess, {
cmdStop() {
done();
},
process: _getProcess('SIGINT'),
exit() {},
})();
});
it('cmdStop on SIGTERM', done => {
resolve(handleKillProcess, {
cmdStop() {
done();
},
process: _getProcess('SIGTERM'),
exit() {},
})();
});

it('run process.exit on SIGINT', done => {
resolve(handleKillProcess, {
cmdStop(packageName, cb) {
expect(packageName).toBe(undefined);
cb();
},
process: Object.assign({}, _getProcess('SIGINT'), {
exit() {
done();
},
}),
})();
});
it('run process.exit on SIGTERM', done => {
resolve(handleKillProcess, {
cmdStop(packageName, cb) {
expect(packageName).toBe(undefined);
cb();
},
process: Object.assign({}, _getProcess('SIGTERM'), {
exit() {
done();
},
}),
})();
});
});
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ const commandListener = require('./commandListener');
const runNpmScripts = require('./runNpmScripts');
const render = require('./render');
const executeCmd = require('./executeCmd');
const handleKillProcess = require('./handleKillProcess');

runCommander();
provideStore();
runNpmScripts();
resizeListener(render);
commandListener(executeCmd);
handleKillProcess();

0 comments on commit 3287edc

Please sign in to comment.