UNPKG

@splitsoftware/splitio-commons

Version:
69 lines (68 loc) 2.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NodeSignalListener = void 0; var thenable_1 = require("../utils/promise/thenable"); var constants_1 = require("../logger/constants"); var SIGTERM = 'SIGTERM'; var EVENT_NAME = 'for SIGTERM signal.'; /** * We'll listen for SIGTERM since it's the standard signal for server shutdown. * * If you're stopping the execution yourself via the keyboard, or by calling process.exit, * you should call the cleanup logic yourself, since we cannot ensure the data is sent after * the process is already exiting. */ var NodeSignalListener = /** @class */ (function () { function NodeSignalListener(syncManager, // private handler: () => MaybeThenable<void>, settings) { // @TODO review handler logic when implementing Node.js SDK this.handler = function () { if (syncManager) { // syncManager.stop(); return syncManager.flush(); } }; this.settings = settings; this._sigtermHandler = this._sigtermHandler.bind(this); } NodeSignalListener.prototype.start = function () { this.settings.log.debug(constants_1.CLEANUP_REGISTERING, [EVENT_NAME]); // eslint-disable-next-line no-undef process.on(SIGTERM, this._sigtermHandler); }; NodeSignalListener.prototype.stop = function () { this.settings.log.debug(constants_1.CLEANUP_DEREGISTERING, [EVENT_NAME]); // eslint-disable-next-line no-undef process.removeListener(SIGTERM, this._sigtermHandler); }; /** * Call the handler, clean up listeners and emit the signal again. */ NodeSignalListener.prototype._sigtermHandler = function () { var _this = this; var wrapUp = function () { // Cleaned up, remove handlers. _this.stop(); // This handler prevented the default behavior, start again. // eslint-disable-next-line no-undef process.kill(process.pid, SIGTERM); }; this.settings.log.debug(constants_1.LOG_PREFIX_CLEANUP + "Split SDK graceful shutdown after SIGTERM."); var handlerResult = null; try { handlerResult = this.handler(); } catch (err) { this.settings.log.error(constants_1.LOG_PREFIX_CLEANUP + "Error with Split SDK graceful shutdown: " + err); } if ((0, thenable_1.thenable)(handlerResult)) { // Always exit, even with errors. The promise is returned for UT purposes. return handlerResult.then(wrapUp).catch(wrapUp); } else { wrapUp(); } }; return NodeSignalListener; }()); exports.NodeSignalListener = NodeSignalListener;