workers: better signal handling.
This commit is contained in:
parent
90f783fd05
commit
0250cf4296
@ -202,17 +202,7 @@ RPC.prototype.stop = async function stop(args, help) {
|
|||||||
if (help || args.length !== 0)
|
if (help || args.length !== 0)
|
||||||
throw new RPCError(errs.MISC_ERROR, 'stop');
|
throw new RPCError(errs.MISC_ERROR, 'stop');
|
||||||
|
|
||||||
(async () => {
|
this.node.close().catch(() => {});
|
||||||
try {
|
|
||||||
await this.node.close();
|
|
||||||
} catch (e) {
|
|
||||||
if (process.exit)
|
|
||||||
process.exit(1);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (process.exit)
|
|
||||||
process.exit(0);
|
|
||||||
})();
|
|
||||||
|
|
||||||
return 'Stopping.';
|
return 'Stopping.';
|
||||||
};
|
};
|
||||||
|
|||||||
@ -121,7 +121,7 @@ WorkerPool.bound = false;
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
WorkerPool.bindExit = function bindExit() {
|
WorkerPool.bindExit = function bindExit() {
|
||||||
let onSignal, onError;
|
let onSighup, onSigint, onSigterm, onError;
|
||||||
|
|
||||||
if (!HAS_CP)
|
if (!HAS_CP)
|
||||||
return;
|
return;
|
||||||
@ -131,13 +131,19 @@ WorkerPool.bindExit = function bindExit() {
|
|||||||
|
|
||||||
WorkerPool.bound = true;
|
WorkerPool.bound = true;
|
||||||
|
|
||||||
onSignal = () => {
|
onSighup = () => {
|
||||||
WorkerPool.cleanup();
|
process.exit(1 | 0x80);
|
||||||
process.exit(0);
|
};
|
||||||
|
|
||||||
|
onSigint = () => {
|
||||||
|
process.exit(2 | 0x80);
|
||||||
|
};
|
||||||
|
|
||||||
|
onSigterm = () => {
|
||||||
|
process.exit(15 | 0x80);
|
||||||
};
|
};
|
||||||
|
|
||||||
onError = (err) => {
|
onError = (err) => {
|
||||||
WorkerPool.cleanup();
|
|
||||||
if (err && err.stack)
|
if (err && err.stack)
|
||||||
util.error(err.stack + '');
|
util.error(err.stack + '');
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
@ -147,20 +153,28 @@ WorkerPool.bindExit = function bindExit() {
|
|||||||
WorkerPool.cleanup();
|
WorkerPool.cleanup();
|
||||||
});
|
});
|
||||||
|
|
||||||
if (process.listeners('SIGINT').length === 0)
|
if (process.listenerCount('SIGHUP') === 0)
|
||||||
process.once('SIGINT', onSignal);
|
process.once('SIGHUP', onSighup);
|
||||||
|
|
||||||
if (process.listeners('SIGTERM').length === 0)
|
if (process.listenerCount('SIGINT') === 0)
|
||||||
process.once('SIGTERM', onSignal);
|
process.once('SIGINT', onSigint);
|
||||||
|
|
||||||
if (process.listeners('uncaughtException').length === 0)
|
if (process.listenerCount('SIGTERM') === 0)
|
||||||
|
process.once('SIGTERM', onSigterm);
|
||||||
|
|
||||||
|
if (process.listenerCount('uncaughtException') === 0)
|
||||||
process.once('uncaughtException', onError);
|
process.once('uncaughtException', onError);
|
||||||
|
|
||||||
process.on('newListener', (name) => {
|
process.on('newListener', (name) => {
|
||||||
switch (name) {
|
switch (name) {
|
||||||
|
case 'SIGHUP':
|
||||||
|
process.removeListener(name, onSighup);
|
||||||
|
break;
|
||||||
case 'SIGINT':
|
case 'SIGINT':
|
||||||
|
process.removeListener(name, onSigint);
|
||||||
|
break;
|
||||||
case 'SIGTERM':
|
case 'SIGTERM':
|
||||||
process.removeListener(name, onSignal);
|
process.removeListener(name, onSigterm);
|
||||||
break;
|
break;
|
||||||
case 'uncaughtException':
|
case 'uncaughtException':
|
||||||
process.removeListener(name, onError);
|
process.removeListener(name, onError);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user