UNPKG

redis-smq

Version:

A simple high-performance Redis message queue for Node.js.

74 lines 2.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Worker = void 0; const redis_smq_common_1 = require("redis-smq-common"); const redis_client_js_1 = require("../../../common/redis-client/redis-client.js"); const index_js_1 = require("../../../config/index.js"); class Worker extends redis_smq_common_1.Runnable { constructor({ config, queueParsedParams, }) { super(); this.onTick = () => { if (this.isRunning()) { this.logger.debug('Worker tick triggered'); this.work((err) => { if (err) { this.logger.error('Error during worker execution', err); this.handleError(err); } else { this.logger.debug('Scheduling next worker tick'); this.timer.setTimeout(this.onTick, 1000); } }); } }; this.handleError = (err) => { if (this.isRunning()) { this.logger.error(`Fatal error in worker ${this.constructor.name}`, err); throw err; } }; this.config = config; index_js_1.Configuration.getSetConfig(config); this.queueParsedParams = queueParsedParams; this.logger = redis_smq_common_1.logger.getLogger(config.logger, this.constructor.name.toLowerCase()); this.logger.info(`Initializing worker: ${this.constructor.name}`); this.redisClient = new redis_client_js_1.RedisClient(); this.redisClient.on('error', (err) => this.handleError(err)); this.timer = new redis_smq_common_1.Timer(); this.timer.on('error', (err) => this.handleError(err)); } getLogger() { return this.logger; } goingUp() { this.logger.debug('Worker going up'); return super.goingUp().concat([ (cb) => { this.logger.debug('Initializing Redis client'); this.redisClient.init(cb); }, (cb) => { this.logger.debug('Setting up worker timer'); this.timer.setTimeout(this.onTick, 1000); cb(); }, ]); } goingDown() { this.logger.debug('Worker going down'); return [ (cb) => { this.logger.debug('Resetting worker timer'); this.timer.reset(); cb(); }, (cb) => { this.logger.debug('Shutting down Redis client'); this.redisClient.shutdown(cb); }, ].concat(super.goingDown()); } } exports.Worker = Worker; //# sourceMappingURL=worker.js.map