@jovian/type-tools
Version:
TypeTools is a Typescript library for providing extensible tooling runtime validations and type helpers.
71 lines • 2.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProcessExit = void 0;
var src_1 = require("../../src");
var env_profile_1 = require("../../src/common/env/env.profile");
var ProcessExitHandler = (function () {
function ProcessExitHandler() {
this.allExitEvents = ["exit", "SIGINT", "SIGUSR1", "SIGUSR2", "uncaughtException", "SIGTERM"];
this.handlers = [];
this.exiting = false;
this.gracefulExitPromises = [];
this.defaultGracePeriod = 7000;
}
ProcessExitHandler.prototype.addEndingTask = function (taskPromise) {
this.gracefulExitPromises.push(taskPromise);
};
ProcessExitHandler.prototype.gracefully = function (exitCode, gracePeriod, lastCallback) {
if (exitCode === void 0) { exitCode = 0; }
if (gracePeriod === void 0) { gracePeriod = this.defaultGracePeriod; }
if (!gracePeriod) {
gracePeriod = this.defaultGracePeriod;
}
if (this.exiting) {
return false;
}
this.exiting = true;
for (var _i = 0, _a = this.handlers; _i < _a.length; _i++) {
var handlerData = _a[_i];
try {
handlerData.handler("graceful");
}
catch (e) {
console.error(e);
}
}
src_1.PromUtil.allSettled(this.gracefulExitPromises).finally(function () {
if (lastCallback) {
lastCallback();
}
setTimeout(function () { process.exit(exitCode); }, 100);
});
setTimeout(function () {
if (lastCallback) {
lastCallback();
}
setTimeout(function () { process.exit(exitCode); }, 100);
}, gracePeriod);
return true;
};
ProcessExitHandler.prototype.addHandler = function (handler) {
this.handlers.push({ handler: handler, source: new Error });
this.allExitEvents.forEach(function (eventType) {
var bound = handler.bind(null, eventType);
process.on(eventType, function () {
try {
bound();
}
catch (e) {
console.error(e);
}
});
});
};
return ProcessExitHandler;
}());
var processExitHandler = new ProcessExitHandler;
if (!env_profile_1.globalRoot.ProcessExitHandler) {
env_profile_1.globalRoot.ProcessExitHandler = processExitHandler;
}
exports.ProcessExit = new Proxy(env_profile_1.globalRoot.ProcessExitHandler, {});
//# sourceMappingURL=process.exit.handler.js.map