UNPKG

redis-smq

Version:

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

132 lines 7.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConsumerGroups = void 0; const redis_smq_common_1 = require("redis-smq-common"); const index_js_1 = require("../../common/index.js"); const redis_client_js_1 = require("../../common/redis-client/redis-client.js"); const index_js_2 = require("../../config/index.js"); const _parse_queue_params_js_1 = require("../queue/_/_parse-queue-params.js"); const _delete_consumer_group_js_1 = require("./_/_delete-consumer-group.js"); const _get_consumer_groups_js_1 = require("./_/_get-consumer-groups.js"); const _save_consumer_group_js_1 = require("./_/_save-consumer-group.js"); class ConsumerGroups { constructor() { this.shutdown = (cb) => { this.logger.info('Shutting down ConsumerGroups manager'); redis_smq_common_1.async.series([ (next) => { this.logger.debug('Shutting down RedisClient'); this.redisClient.shutdown((err) => { if (err) { this.logger.warn(`Error during RedisClient shutdown (continuing): ${err.message}`); } else { this.logger.debug('RedisClient shutdown successful'); } next(); }); }, (next) => { this.logger.debug('Shutting down EventBus'); this.eventBus.shutdown((err) => { if (err) { this.logger.warn(`Error during EventBus shutdown (continuing): ${err.message}`); } else { this.logger.debug('EventBus shutdown successful'); } next(); }); }, ], (err) => { if (err) { this.logger.error(`Error during shutdown: ${err.message}`); return cb(err); } this.logger.info('ConsumerGroups manager shutdown complete'); cb(); }); }; this.logger = redis_smq_common_1.logger.getLogger(index_js_2.Configuration.getSetConfig().logger, this.constructor.name.toLowerCase()); this.logger.info('Initializing ConsumerGroups manager'); this.eventBus = new index_js_1.EventBus(); this.eventBus.on('error', (err) => { this.logger.error(`EventBus error: ${err.message}`, err); }); this.logger.debug('EventBus initialized'); this.redisClient = new redis_client_js_1.RedisClient(); this.redisClient.on('error', (err) => { this.logger.error(`RedisClient error: ${err.message}`, err); }); this.logger.debug('RedisClient initialized'); } saveConsumerGroup(queue, groupId, cb) { this.logger.debug(`Saving consumer group '${groupId}' to queue: ${typeof queue === 'string' ? queue : JSON.stringify(queue)}`); const queueParams = (0, _parse_queue_params_js_1._parseQueueParams)(queue); if (queueParams instanceof Error) { this.logger.error(`Failed to parse queue parameters: ${queueParams.message}`); return cb(queueParams); } this.logger.debug(`Parsed queue parameters: ${JSON.stringify(queueParams)}`); (0, redis_smq_common_1.withRedisClient)(this.redisClient, (client, cb) => { (0, redis_smq_common_1.withEventBus)(this.eventBus, (eventBus, cb) => { this.logger.debug('EventBus instance obtained successfully'); (0, _save_consumer_group_js_1._saveConsumerGroup)(client, eventBus, queueParams, groupId, (err, result) => { if (err) { this.logger.error(`Failed to save consumer group '${groupId}': ${err.message}`); return cb(err); } this.logger.info(`Consumer group '${groupId}' ${result === 1 ? 'created' : 'already exists'} for queue: ${queueParams.name}`); cb(null, result); }); }, cb); }, cb); } deleteConsumerGroup(queue, groupId, cb) { this.logger.debug(`Deleting consumer group '${groupId}' from queue: ${typeof queue === 'string' ? queue : JSON.stringify(queue)}`); const queueParams = (0, _parse_queue_params_js_1._parseQueueParams)(queue); if (queueParams instanceof Error) { this.logger.error(`Failed to parse queue parameters: ${queueParams.message}`); return cb(queueParams); } this.logger.debug(`Parsed queue parameters: ${JSON.stringify(queueParams)}`); redis_smq_common_1.async.withCallbackList([ (cb) => (0, redis_smq_common_1.withRedisClient)(this.redisClient, (client, cb) => cb(null, client), cb), (cb) => (0, redis_smq_common_1.withEventBus)(this.eventBus, (eventBus, cb) => cb(null, eventBus), cb), ], ([client, eventBus], cb) => { this.logger.debug(`Executing delete operation for consumer group '${groupId}'`); (0, _delete_consumer_group_js_1._deleteConsumerGroup)(client, eventBus, queueParams, groupId, (err) => { if (err) { this.logger.error(`Failed to delete consumer group '${groupId}': ${err.message}`); return cb(err); } this.logger.info(`Consumer group '${groupId}' successfully deleted from queue: ${queueParams.name}`); cb(); }); }, cb); } getConsumerGroups(queue, cb) { this.logger.debug(`Getting consumer groups for queue: ${typeof queue === 'string' ? queue : JSON.stringify(queue)}`); const queueParams = (0, _parse_queue_params_js_1._parseQueueParams)(queue); if (queueParams instanceof Error) { this.logger.error(`Failed to parse queue parameters: ${queueParams.message}`); return cb(queueParams); } this.logger.debug(`Parsed queue parameters: ${JSON.stringify(queueParams)}`); (0, redis_smq_common_1.withRedisClient)(this.redisClient, (client, cb) => { (0, _get_consumer_groups_js_1._getConsumerGroups)(client, queueParams, (err, groups) => { if (err) { this.logger.error(`Failed to get consumer groups: ${err.message}`); return cb(err); } this.logger.info(`Retrieved ${Number(groups === null || groups === void 0 ? void 0 : groups.length)} consumer groups for queue: ${queueParams.name}`); if (groups && groups.length > 0) { this.logger.debug(`Consumer groups: ${JSON.stringify(groups)}`); } cb(null, groups); }); }, cb); } } exports.ConsumerGroups = ConsumerGroups; //# sourceMappingURL=consumer-groups.js.map