Child processes are now terminated when the parent process dies. This means that if the main mailtrain process gets killed, there are no processes which remain running.

This commit is contained in:
Tomas Bures 2019-05-25 21:57:11 +02:00
parent 1270ca71f8
commit fcd2a61b65
14 changed files with 48 additions and 18 deletions

31
server/lib/fork.js Normal file
View file

@ -0,0 +1,31 @@
'use strict';
const builtinFork = require('child_process').fork;
const cleanExit = () => process.exit();
process.on('SIGINT', cleanExit); // catch ctrl-c
process.on('SIGTERM', cleanExit); // catch kill
const children = [];
process.on('message', msg => {
if (msg === 'exit') {
cleanExit();
}
});
process.on('exit', function() {
for (const child of children) {
child.send('exit');
}
});
function fork(path, args, opts) {
const child = builtinFork(path, args, opts);
children.push(child);
return child;
}
module.exports.fork = fork;