UNPKG

redis-smq

Version:

A high-performance, reliable, and scalable message queue for Node.js.

139 lines 5.97 kB
import { async } from 'redis-smq-common'; import { redisKeys } from '../../common/redis/redis-keys/redis-keys.js'; import { _getConsumerGroups } from '../../consumer-groups/_/_get-consumer-groups.js'; import { ConsumerSetMismatchError, QueueHasActiveConsumersError, QueueHasBoundExchangesError, QueueLockedError, QueueNotEmptyError, QueueNotFoundError, UnexpectedScriptReplyError, } from '../../errors/index.js'; import { EQueueOperationalState, EQueueProperty, } from '../types/index.js'; import { _getQueueConsumerIds } from './_get-queue-consumer-ids.js'; import { ERedisScriptName } from '../../common/redis/scripts.js'; import { _validateOperation } from '../../queue-operation-validator/_/_validate-operation.js'; import { EQueueOperation } from '../../queue-operation-validator/index.js'; import { _getProcessingQueues } from './_get-processing-queues.js'; export function _deleteQueue(redisClient, queueParams, cb) { let consumerIds = []; let consumerGroups = []; let processingQueues = []; async.series([ (cb) => { _validateOperation(redisClient, queueParams, EQueueOperation.DELETE, cb); }, (cb) => { _getQueueConsumerIds(redisClient, queueParams, (err, reply) => { if (err) cb(err); else { consumerIds = reply ?? []; cb(); } }); }, (cb) => { _getConsumerGroups(redisClient, queueParams, (err, reply) => { if (err) cb(err); else { consumerGroups = reply ?? []; cb(); } }); }, (cb) => { _getProcessingQueues(redisClient, queueParams, (err, reply) => { if (err) cb(err); else { processingQueues = Object.keys(reply ?? {}); cb(); } }); }, ], (err) => { if (err) return cb(err); const { keyQueues } = redisKeys.getMainKeys(); const { keyNamespaceQueues } = redisKeys.getNamespaceKeys(queueParams.ns); const { keyQueueProperties, keyQueuePending, keyQueueDeadLetter, keyQueueProcessingQueues, keyQueuePriority, keyQueueAcknowledged, keyQueueConsumers, keyQueueRateLimit, keyQueueScheduled, keyQueueDelayed, keyQueueRequeued, keyQueuePublished, keyQueueConsumerGroups, keyQueueWorkersLock, keyQueueExchangeBindings, } = redisKeys.getQueueKeys(queueParams.ns, queueParams.name, null); const heartbeatKeys = consumerIds.map((id) => redisKeys.getConsumerKeys(id).keyConsumerHeartbeat); const consumerGroupKeys = consumerGroups.flatMap((groupId) => { const { keyQueuePriority, keyQueuePending } = redisKeys.getQueueKeys(queueParams.ns, queueParams.name, groupId); return [keyQueuePending, keyQueuePriority]; }); const keysToDelete = new Set([ keyQueueProperties, keyQueuePending, keyQueueDeadLetter, keyQueueProcessingQueues, keyQueuePriority, keyQueueAcknowledged, keyQueueConsumers, keyQueueRateLimit, keyQueueScheduled, keyQueueDelayed, keyQueueRequeued, keyQueuePublished, keyQueueConsumerGroups, keyQueueWorkersLock, keyQueueExchangeBindings, ...consumerGroupKeys, ...processingQueues, ]); const scriptKeys = [ keyQueues, keyNamespaceQueues, keyQueueProperties, keyQueueExchangeBindings, keyQueueConsumers, ...heartbeatKeys, ...Array.from(keysToDelete).filter((k) => k !== keyQueueProperties && k !== keyQueueExchangeBindings && k !== keyQueueConsumers), ]; const queueParamsStr = JSON.stringify(queueParams); const scriptArgs = [ queueParamsStr, EQueueProperty.MESSAGES_COUNT, String(heartbeatKeys.length), EQueueProperty.OPERATIONAL_STATE, EQueueOperationalState.LOCKED, EQueueProperty.LOCK_ID, '', ...consumerIds, ]; redisClient.runScript(ERedisScriptName.DELETE_QUEUE, scriptKeys, scriptArgs, (err, reply) => { if (err) cb(err); else if (reply !== 'OK') { if (reply === 'QUEUE_LOCKED') { cb(new QueueLockedError({ metadata: { queue: queueParams, }, message: `Cannot delete queue ${queueParams.name}: Queue is locked.`, })); } else if (reply === 'QUEUE_NOT_FOUND') cb(new QueueNotFoundError({ metadata: { queue: queueParams, }, })); else if (reply === 'QUEUE_NOT_EMPTY') cb(new QueueNotEmptyError()); else if (reply === 'QUEUE_HAS_ACTIVE_CONSUMERS') cb(new QueueHasActiveConsumersError({ metadata: { queue: queueParams, }, })); else if (reply === 'QUEUE_HAS_BOUND_EXCHANGE') cb(new QueueHasBoundExchangesError()); else if (reply === 'CONSUMER_SET_MISMATCH') cb(new ConsumerSetMismatchError()); else cb(new UnexpectedScriptReplyError({ metadata: { reply } })); } else cb(); }); }); } //# sourceMappingURL=_delete-queue.js.map