redis-smq
Version:
A simple high-performance Redis message queue for Node.js.
131 lines • 6.34 kB
JavaScript
import { async, logger, withEventBus, withRedisClient, } from 'redis-smq-common';
import { EventBus } from '../../common/index.js';
import { RedisClient } from '../../common/redis-client/redis-client.js';
import { Configuration } from '../../config/index.js';
import { _parseQueueParams } from '../queue/_/_parse-queue-params.js';
import { _deleteConsumerGroup } from './_/_delete-consumer-group.js';
import { _getConsumerGroups } from './_/_get-consumer-groups.js';
import { _saveConsumerGroup } from './_/_save-consumer-group.js';
export class ConsumerGroups {
redisClient;
eventBus;
logger;
constructor() {
this.logger = logger.getLogger(Configuration.getSetConfig().logger, this.constructor.name.toLowerCase());
this.logger.info('Initializing ConsumerGroups manager');
this.eventBus = new EventBus();
this.eventBus.on('error', (err) => {
this.logger.error(`EventBus error: ${err.message}`, err);
});
this.logger.debug('EventBus initialized');
this.redisClient = new 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 = _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)}`);
withRedisClient(this.redisClient, (client, cb) => {
withEventBus(this.eventBus, (eventBus, cb) => {
this.logger.debug('EventBus instance obtained successfully');
_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 = _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)}`);
async.withCallbackList([
(cb) => withRedisClient(this.redisClient, (client, cb) => cb(null, client), cb),
(cb) => withEventBus(this.eventBus, (eventBus, cb) => cb(null, eventBus), cb),
], ([client, eventBus], cb) => {
this.logger.debug(`Executing delete operation for consumer group '${groupId}'`);
_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 = _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)}`);
withRedisClient(this.redisClient, (client, cb) => {
_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?.length)} consumer groups for queue: ${queueParams.name}`);
if (groups && groups.length > 0) {
this.logger.debug(`Consumer groups: ${JSON.stringify(groups)}`);
}
cb(null, groups);
});
}, cb);
}
shutdown = (cb) => {
this.logger.info('Shutting down ConsumerGroups manager');
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();
});
};
}
//# sourceMappingURL=consumer-groups.js.map