UNPKG

watchdog-timer

Version:

Detects and notifies when program does not check-in within a timeout.

72 lines (50 loc) 1.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _Logger = _interopRequireDefault(require("../Logger")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const createWatchdogTimer = configuration => { let consequentTimeouts = 0; let timeoutId; const routine = () => { timeoutId = setTimeout(() => { consequentTimeouts++; _Logger.default.error('watchdog timer timeout'); configuration.onTimeout({ consequentTimeouts }); if (timeoutId) { routine(); } }, configuration.timeout); // $FlowFixMe timeoutId.unref(); }; routine(); const destroy = () => { if (!timeoutId) { _Logger.default.warn('watchdog timer has been already destroyed'); return; } _Logger.default.trace('watchdog timer has been destroyed'); clearTimeout(timeoutId); timeoutId = undefined; }; const reset = () => { if (!timeoutId) { _Logger.default.warn('watchdog timer has been already destroyed'); return; } consequentTimeouts = 0; _Logger.default.trace('watchdog timer has been reset'); // $FlowFixMe timeoutId.refresh(); }; return { destroy, reset }; }; var _default = createWatchdogTimer; exports.default = _default; //# sourceMappingURL=createWatchdogTimer.js.map