UNPKG

redis-smq

Version:

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

138 lines 5.55 kB
import { async, CallbackEmptyReplyError, logger, } from 'redis-smq-common'; import { RedisClient } from '../../common/redis-client/redis-client.js'; import { ELuaScriptName } from '../../common/redis-client/scripts/scripts.js'; import { redisKeys } from '../../common/redis-keys/redis-keys.js'; import { Configuration } from '../../config/index.js'; import { EventBus } from '../event-bus/index.js'; import { _deleteQueue } from './_/_delete-queue.js'; import { _getQueueProperties } from './_/_get-queue-properties.js'; import { _getQueues } from './_/_get-queues.js'; import { _parseQueueParams } from './_/_parse-queue-params.js'; import { _queueExists } from './_/_queue-exists.js'; import { QueueQueueExistsError } from './errors/index.js'; import { EQueueProperty, } from './types/index.js'; export class Queue { redisClient; eventBus; logger; constructor() { this.logger = logger.getLogger(Configuration.getSetConfig().logger, `queue`); this.eventBus = new EventBus(); this.eventBus.on('error', (err) => this.logger.error(err)); this.redisClient = new RedisClient(); this.redisClient.on('error', (err) => this.logger.error(err)); } save(queue, queueType, deliveryModel, cb) { const queueParams = _parseQueueParams(queue); if (queueParams instanceof Error) return cb(queueParams); this.redisClient.getSetInstance((err, client) => { if (err) return cb(err); if (!client) return cb(new CallbackEmptyReplyError()); const { keyQueueProperties } = redisKeys.getQueueKeys(queueParams, null); const { keyNamespaces, keyQueues } = redisKeys.getMainKeys(); const { keyNamespaceQueues } = redisKeys.getNamespaceKeys(queueParams.ns); const queueParamsStr = JSON.stringify(queueParams); client.runScript(ELuaScriptName.CREATE_QUEUE, [keyNamespaces, keyNamespaceQueues, keyQueues, keyQueueProperties], [ queueParams.ns, queueParamsStr, EQueueProperty.QUEUE_TYPE, queueType, EQueueProperty.DELIVERY_MODEL, deliveryModel, ], (err, reply) => { if (err) return cb(err); if (!reply) return cb(new CallbackEmptyReplyError()); if (reply !== 'OK') return cb(new QueueQueueExistsError()); this.getProperties(queueParams, (err, properties) => { if (err) return cb(err); if (!properties) return cb(new CallbackEmptyReplyError()); this.eventBus.getSetInstance((err, instance) => { if (err) return cb(err); instance?.emit('queue.queueCreated', queueParams, properties); cb(null, { queue: queueParams, properties }); }); }); }); }); } exists(queue, cb) { const queueParams = _parseQueueParams(queue); if (queueParams instanceof Error) cb(queueParams); else { this.redisClient.getSetInstance((err, client) => { if (err) cb(err); else if (!client) cb(new CallbackEmptyReplyError()); else _queueExists(client, queueParams, cb); }); } } delete(queue, cb) { const queueParams = _parseQueueParams(queue); if (queueParams instanceof Error) return cb(queueParams); this.redisClient.getSetInstance((err, client) => { if (err) return cb(err); if (!client) return cb(new CallbackEmptyReplyError()); _deleteQueue(client, queueParams, undefined, (err, multi) => { if (err) return cb(err); if (!multi) return cb(new CallbackEmptyReplyError()); multi.exec((err) => { if (err) return cb(err); this.eventBus.getSetInstance((err, instance) => { if (err) return cb(err); instance?.emit('queue.queueDeleted', queueParams); cb(); }); }); }); }); } getProperties(queue, cb) { const queueParams = _parseQueueParams(queue); if (queueParams instanceof Error) cb(queueParams); else { this.redisClient.getSetInstance((err, client) => { if (err) cb(err); else if (!client) cb(new CallbackEmptyReplyError()); else _getQueueProperties(client, queueParams, cb); }); } } getQueues(cb) { this.redisClient.getSetInstance((err, client) => { if (err) cb(err); else if (!client) cb(new CallbackEmptyReplyError()); else _getQueues(client, cb); }); } shutdown = (cb) => { async.waterfall([this.redisClient.shutdown, this.eventBus.shutdown], cb); }; } //# sourceMappingURL=queue.js.map