-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(process): terminate child processes on SIGINT and SIGTERM
- Loading branch information
Simon Mollweide
committed
Jan 24, 2018
1 parent
abe8468
commit 3287edc
Showing
3 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}, | ||
}), | ||
})(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters