UNPKG

redis-smq

Version:

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

103 lines 4.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MultiplexedMessageHandlerRunner = void 0; const redis_smq_common_1 = require("redis-smq-common"); const multiplexed_message_handler_js_1 = require("../message-handler/message-handler/multiplexed-message-handler.js"); const message_handler_runner_js_1 = require("./message-handler-runner.js"); class MultiplexedMessageHandlerRunner extends message_handler_runner_js_1.MessageHandlerRunner { constructor(consumer, redisClient, eventBus) { super(consumer, redisClient, eventBus); this.index = 0; this.activeMessageHandler = null; this.tickIntervalMs = 1000; this.execNextMessageHandler = () => { if (!this.isRunning()) { this.logger.debug('execNextMessageHandler called while not running, ignoring'); return; } this.activeMessageHandler = this.getNextMessageHandler(); if (this.activeMessageHandler) { if (this.activeMessageHandler.isRunning()) { this.logger.debug(`Triggering dequeue on active handler (ID: ${this.activeMessageHandler.getId()})`); this.activeMessageHandler.dequeue(); } else { this.logger.debug(`Active handler (ID: ${this.activeMessageHandler.getId()}) is not running, scheduling next tick`); this.nextTick(); } } else { this.logger.debug('No active handler available, scheduling next tick'); this.nextTick(); } }; this.logger.info(`Initializing MultiplexedMessageHandlerRunner with ID: ${this.id}`); this.timer = new redis_smq_common_1.Timer(); this.timer.on('error', (err) => { this.logger.error(`Timer error: ${err.message}`); this.handleError(err); }); this.logger.debug('Timer initialized and error handler registered'); } nextTick() { if (!this.isRunning()) { this.logger.debug('nextTick called while not running, ignoring'); return; } this.activeMessageHandler = null; this.timer.reset(); this.logger.debug(`Scheduling next message handler execution in ${this.tickIntervalMs}ms`); this.timer.setTimeout(() => this.execNextMessageHandler(), this.tickIntervalMs); } getNextMessageHandler() { var _a, _b; const count = this.messageHandlerInstances.length; if (count === 0) { this.logger.debug('No message handlers registered'); return null; } if (this.index >= count) { this.index = 0; } const handler = this.messageHandlerInstances[this.index]; this.logger.debug(`Selected message handler at index ${this.index} (ID: ${(_b = (_a = handler === null || handler === void 0 ? void 0 : handler.getId) === null || _a === void 0 ? void 0 : _a.call(handler)) !== null && _b !== void 0 ? _b : 'N/A'})`); this.index = (this.index + 1) % count; return handler; } createMessageHandlerInstance(handlerParams) { this.logger.debug(`Creating MultiplexedMessageHandler for queue: ${JSON.stringify(handlerParams.queue)}`); const instance = new multiplexed_message_handler_js_1.MultiplexedMessageHandler(this.consumer, this.redisClient, handlerParams, this.eventBus, this.execNextMessageHandler); this.messageHandlerInstances.push(instance); this.logger.info(`Created MultiplexedMessageHandler (ID: ${instance.getId()}) for queue: ${handlerParams.queue.queueParams.name}. Total: ${this.messageHandlerInstances.length}`); return instance; } shutdownMessageHandler(messageHandler, cb) { const queue = messageHandler.getQueue(); this.logger.debug(`Shutting down handler (ID: ${messageHandler.getId()}) for queue: ${JSON.stringify(queue)}`); super.shutdownMessageHandler(messageHandler, () => { if (messageHandler === this.activeMessageHandler) { this.logger.debug('Shut down active handler, scheduling next tick'); this.nextTick(); } cb(); }); } goingUp() { this.logger.info('MultiplexedMessageHandlerRunner going up'); return super.goingUp().concat([ (cb) => { this.logger.debug('Starting message handler execution cycle'); this.execNextMessageHandler(); cb(); }, ]); } goingDown() { this.logger.info('MultiplexedMessageHandlerRunner going down'); this.logger.debug('Resetting timer during shutdown'); this.timer.reset(); return super.goingDown(); } } exports.MultiplexedMessageHandlerRunner = MultiplexedMessageHandlerRunner; //# sourceMappingURL=multiplexed-message-handler-runner.js.map