40 lines
5.2 KiB
JavaScript
40 lines
5.2 KiB
JavaScript
|
"use strict";
|
||
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||
|
};
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
const events_1 = __importDefault(require("events"));
|
||
|
const TERMINATION_TYPES = {
|
||
|
sigint: 'sigint',
|
||
|
sigbreak: 'sigbreak',
|
||
|
shutdown: 'shutdown',
|
||
|
};
|
||
|
const TERMINATION_LEVEL_INCREASED_EVENT = 'termination-level-increased';
|
||
|
class TerminationHandler extends events_1.default {
|
||
|
constructor() {
|
||
|
super();
|
||
|
this.handledSignalsCount = {
|
||
|
[TERMINATION_TYPES.sigint]: 0,
|
||
|
[TERMINATION_TYPES.sigbreak]: 0,
|
||
|
[TERMINATION_TYPES.shutdown]: 0,
|
||
|
};
|
||
|
this.terminationLevel = 0;
|
||
|
this._setupHandlers();
|
||
|
}
|
||
|
_exitEventHandler(terminationType) {
|
||
|
this.handledSignalsCount[terminationType]++;
|
||
|
if (this.handledSignalsCount[terminationType] > this.terminationLevel) {
|
||
|
this.terminationLevel = this.handledSignalsCount[terminationType];
|
||
|
this.emit(TERMINATION_LEVEL_INCREASED_EVENT, this.terminationLevel);
|
||
|
}
|
||
|
}
|
||
|
_setupHandlers() {
|
||
|
process.on('SIGINT', () => this._exitEventHandler(TERMINATION_TYPES.sigint));
|
||
|
process.on('SIGBREAK', () => this._exitEventHandler(TERMINATION_TYPES.sigbreak));
|
||
|
process.on('message', message => message === 'shutdown' && this._exitEventHandler(TERMINATION_TYPES.shutdown));
|
||
|
}
|
||
|
}
|
||
|
exports.default = TerminationHandler;
|
||
|
TerminationHandler.TERMINATION_LEVEL_INCREASED_EVENT = TERMINATION_LEVEL_INCREASED_EVENT;
|
||
|
module.exports = exports.default;
|
||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVybWluYXRpb24taGF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