UNPKG

redis-smq

Version:

A high-performance, reliable, and scalable message queue for Node.js.

67 lines 2.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WorkerAbstract = void 0; const redis_smq_common_1 = require("redis-smq-common"); const index_js_1 = require("../../../redis-smq/index.js"); class WorkerAbstract extends redis_smq_common_1.Runnable { constructor(payload) { super(); this.timer = null; this.initialized = false; this.onTick = () => { if (this.isOperational()) { this.work((err) => { var _a; if (err) { this.logger.error('Error during worker execution', err); this.handleError(err); return; } (_a = this.timer) === null || _a === void 0 ? void 0 : _a.schedule(this.onTick, 1000); }); } }; this.handleError = (err) => { if (this.isOperational()) { this.logger.error(`Fatal error in worker ${this.constructor.name}`, err); throw err; } }; const { redisConfig, config } = payload; this.redisConfig = redisConfig; this.config = config; } finalizeUp() { var _a; (_a = this.timer) === null || _a === void 0 ? void 0 : _a.schedule(this.onTick, 1000); super.finalizeUp(); } goingUp() { return super.goingUp().concat([ (cb) => { if (index_js_1.RedisSMQ.isRunning()) return cb(); index_js_1.RedisSMQ.initialize(this.redisConfig, cb); }, (cb) => { this.logger.debug('Setting up worker timer'); this.timer = new redis_smq_common_1.Timer(this.logger); this.timer.run(cb); }, ]); } goingDown() { return [ (cb) => { this.logger.debug('Resetting worker timer'); if (this.timer) { this.timer.shutdown(cb); return; } cb(); }, ].concat(super.goingDown()); } } exports.WorkerAbstract = WorkerAbstract; //# sourceMappingURL=worker-abstract.js.map