UNPKG

@splitsoftware/splitio-commons

Version:
66 lines (65 loc) 2.64 kB
import { thenable } from '../utils/promise/thenable'; import { LOG_PREFIX_CLEANUP, CLEANUP_REGISTERING, CLEANUP_DEREGISTERING } from '../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(_a) { var syncManager = _a.syncManager, settings = _a.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(CLEANUP_REGISTERING, [EVENT_NAME]); // eslint-disable-next-line no-undef process.on(SIGTERM, this._sigtermHandler); }; NodeSignalListener.prototype.stop = function () { this.settings.log.debug(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("".concat(LOG_PREFIX_CLEANUP, "SDK graceful shutdown after SIGTERM.")); var handlerResult = null; try { handlerResult = this.handler(); } catch (err) { this.settings.log.error("".concat(LOG_PREFIX_CLEANUP, "Error with SDK graceful shutdown: ").concat(err)); } if (thenable(handlerResult)) { // Always exit, even with errors. The promise is returned for UT purposes. return handlerResult.then(wrapUp).catch(wrapUp); } else { wrapUp(); } }; return NodeSignalListener; }()); export { NodeSignalListener };