UNPKG

redis-smq

Version:

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

123 lines 5.32 kB
import { async, } from 'redis-smq-common'; import { redisKeys } from '../../../common/redis-keys/redis-keys.js'; import { _getConsumerGroups } from '../../consumer-groups/_/_get-consumer-groups.js'; import { ConsumerHeartbeat } from '../../consumer/consumer-heartbeat/consumer-heartbeat.js'; import { consumerQueues } from '../../consumer/consumer-queues.js'; import { processingQueue } from '../../consumer/message-handler/processing-queue/processing-queue.js'; import { QueueQueueHasRunningConsumersError, QueueQueueNotEmptyError, QueueQueueNotFoundError, } from '../errors/index.js'; import { EQueueDeliveryModel } from '../types/index.js'; import { _getQueueProperties } from './_get-queue-properties.js'; function checkOnlineConsumers(redisClient, queue, cb) { consumerQueues.getQueueConsumers(redisClient, queue, false, (err, consumers) => { if (err) cb(err); async.eachIn(consumers ?? {}, (_, consumerId, done) => { ConsumerHeartbeat.isConsumerAlive(redisClient, consumerId, (err, alive) => { if (err) done(err); else if (alive) done(new QueueQueueHasRunningConsumersError()); else done(); }); }, (err) => cb(err)); }); } export function _deleteQueue(redisClient, queueParams, multi, cb) { const { keyQueuePending, keyQueueDL, keyQueueProcessingQueues, keyQueuePriorityPending, keyQueueAcknowledged, keyQueueConsumers, keyQueueRateLimitCounter, keyQueueProperties, keyQueueScheduled, keyQueueMessages, keyQueueConsumerGroups, } = redisKeys.getQueueKeys(queueParams, null); const { keyNamespaceQueues } = redisKeys.getNamespaceKeys(queueParams.ns); const { keyQueues } = redisKeys.getMainKeys(); const keys = [ keyQueuePending, keyQueueDL, keyQueueProcessingQueues, keyQueuePriorityPending, keyQueueAcknowledged, keyQueueConsumers, keyQueueRateLimitCounter, keyQueueProperties, keyQueueScheduled, keyQueueMessages, keyQueueConsumerGroups, ]; let exchange = null; let pubSubDelivery = false; redisClient.watch([ keyQueueConsumers, keyQueueProcessingQueues, keyQueueProperties, keyQueueConsumerGroups, ], (err) => { if (err) cb(err); else { const processingQueues = []; async.waterfall([ (cb) => _getQueueProperties(redisClient, queueParams, (err, reply) => { if (err) cb(err); else if (!reply) cb(new QueueQueueNotFoundError()); else { const messagesCount = reply.messagesCount; if (messagesCount) cb(new QueueQueueNotEmptyError()); else { exchange = reply.exchange ?? null; pubSubDelivery = reply.deliveryModel === EQueueDeliveryModel.PUB_SUB; cb(); } } }), (cb) => { if (pubSubDelivery) { _getConsumerGroups(redisClient, queueParams, (err, groups) => { if (err) cb(err); else { async.eachOf(groups ?? [], (groupId, _, cb) => { const { keyQueuePriorityPending, keyQueuePending } = redisKeys.getQueueKeys(queueParams, groupId); keys.push(keyQueuePending, keyQueuePriorityPending); cb(); }, cb); } }); } else cb(); }, (cb) => { checkOnlineConsumers(redisClient, queueParams, cb); }, (cb) => { processingQueue.getQueueProcessingQueues(redisClient, queueParams, (err, reply) => { if (err) cb(err); else { processingQueues.push(...Object.keys(reply ?? {})); cb(); } }); }, ], (err) => { if (err) redisClient.unwatch(() => cb(err)); else { const tx = multi || redisClient.multi(); const str = JSON.stringify(queueParams); tx.srem(keyQueues, str); tx.srem(keyNamespaceQueues, str); if (processingQueues.length) { keys.push(...processingQueues); } tx.del(keys); if (exchange) tx.srem(exchange, str); cb(null, tx); } }); } }); } //# sourceMappingURL=_delete-queue.js.map