UNPKG

redis-smq

Version:

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

164 lines 8.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports._publishMessage = _publishMessage; const scripts_js_1 = require("../../common/redis/scripts.js"); const redis_keys_js_1 = require("../../common/redis/redis-keys/redis-keys.js"); const index_js_1 = require("../../message/index.js"); const index_js_2 = require("../../queue-manager/index.js"); const index_js_3 = require("../../errors/index.js"); function _publishMessage(redisClient, message, logger, cb) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p; const messageState = message.getMessageState(); const messageId = message.getId(); const destinationQueue = message.getDestinationQueue(); const queueName = `${destinationQueue.name}@${destinationQueue.ns}`; const consumerGroupId = message.getConsumerGroupId(); logger.debug(`Enqueuing message ${messageId} to queue ${queueName}${consumerGroupId ? ` for consumer group ${consumerGroupId}` : ''}`); const keys = redis_keys_js_1.redisKeys.getQueueKeys(destinationQueue.ns, destinationQueue.name, consumerGroupId); const { keyMessage } = redis_keys_js_1.redisKeys.getMessageKeys(messageId); const messagePriority = message.producibleMessage.getPriority(); logger.debug(`Message ${messageId} details: priority=${messagePriority !== null && messagePriority !== void 0 ? messagePriority : 'none'}, queue=${queueName}`); const scheduledTimestamp = message.getNextScheduledTimestamp(); const messageStatus = scheduledTimestamp ? index_js_1.EMessagePropertyStatus.SCHEDULED : index_js_1.EMessagePropertyStatus.PENDING; const queuePropertyValues = [ index_js_2.EQueueProperty.QUEUE_TYPE, index_js_2.EQueueProperty.MESSAGES_COUNT, index_js_2.EQueueProperty.PENDING_MESSAGES_COUNT, index_js_2.EQueueProperty.SCHEDULED_MESSAGES_COUNT, index_js_2.EQueueType.PRIORITY_QUEUE, index_js_2.EQueueType.LIFO_QUEUE, index_js_2.EQueueType.FIFO_QUEUE, index_js_2.EQueueProperty.OPERATIONAL_STATE, index_js_2.EQueueProperty.LOCK_ID, index_js_2.EQueueOperationalState.ACTIVE, index_js_2.EQueueOperationalState.PAUSED, index_js_2.EQueueOperationalState.STOPPED, index_js_2.EQueueOperationalState.LOCKED, ]; const schedulingValues = [ messagePriority !== null && messagePriority !== void 0 ? messagePriority : '', scheduledTimestamp, index_js_1.EMessagePropertyStatus.SCHEDULED, index_js_1.EMessagePropertyStatus.PENDING, ]; const messagePropertyKeys = [ index_js_1.EMessageProperty.ID, index_js_1.EMessageProperty.STATUS, index_js_1.EMessageProperty.MESSAGE, index_js_1.EMessageProperty.SCHEDULED_AT, index_js_1.EMessageProperty.PUBLISHED_AT, index_js_1.EMessageProperty.PROCESSING_STARTED_AT, index_js_1.EMessageProperty.DEAD_LETTERED_AT, index_js_1.EMessageProperty.ACKNOWLEDGED_AT, index_js_1.EMessageProperty.UNACKNOWLEDGED_AT, index_js_1.EMessageProperty.LAST_UNACKNOWLEDGED_AT, index_js_1.EMessageProperty.LAST_SCHEDULED_AT, index_js_1.EMessageProperty.REQUEUED_AT, index_js_1.EMessageProperty.REQUEUE_COUNT, index_js_1.EMessageProperty.LAST_REQUEUED_AT, index_js_1.EMessageProperty.LAST_RETRIED_ATTEMPT_AT, index_js_1.EMessageProperty.SCHEDULED_CRON_FIRED, index_js_1.EMessageProperty.ATTEMPTS, index_js_1.EMessageProperty.SCHEDULED_REPEAT_COUNT, index_js_1.EMessageProperty.EXPIRED, index_js_1.EMessageProperty.EFFECTIVE_SCHEDULED_DELAY, index_js_1.EMessageProperty.SCHEDULED_TIMES, index_js_1.EMessageProperty.SCHEDULED_MESSAGE_PARENT_ID, index_js_1.EMessageProperty.REQUEUED_MESSAGE_PARENT_ID, index_js_1.EMessageProperty.LAST_PROCESSED_AT, ]; const messagePropertyValues = [ messageId, messageStatus, JSON.stringify(message.toJSON()), (_a = messageState.getScheduledAt()) !== null && _a !== void 0 ? _a : '', (_b = messageState.getPublishedAt()) !== null && _b !== void 0 ? _b : '', (_c = messageState.getProcessingStartedAt()) !== null && _c !== void 0 ? _c : '', (_d = messageState.getDeadLetteredAt()) !== null && _d !== void 0 ? _d : '', (_e = messageState.getAcknowledgedAt()) !== null && _e !== void 0 ? _e : '', (_f = messageState.getUnacknowledgedAt()) !== null && _f !== void 0 ? _f : '', (_g = messageState.getLastUnacknowledgedAt()) !== null && _g !== void 0 ? _g : '', (_h = messageState.getLastScheduledAt()) !== null && _h !== void 0 ? _h : '', (_j = messageState.getRequeuedAt()) !== null && _j !== void 0 ? _j : '', messageState.getRequeueCount(), (_k = messageState.getLastRequeuedAt()) !== null && _k !== void 0 ? _k : '', (_l = messageState.getLastRetriedAttemptAt()) !== null && _l !== void 0 ? _l : '', Number(messageState.isScheduledCronFired()), messageState.getAttempts(), messageState.getScheduledRepeatCount(), Number(messageState.getExpired()), messageState.getEffectiveScheduledDelay(), messageState.getScheduledTimes(), (_m = messageState.getScheduledMessageParentId()) !== null && _m !== void 0 ? _m : '', (_o = messageState.getRequeuedMessageParentId()) !== null && _o !== void 0 ? _o : '', (_p = messageState.getLastProcessedAt()) !== null && _p !== void 0 ? _p : '', ]; const scriptArgs = [ ...queuePropertyValues, ...schedulingValues, ...messagePropertyKeys, ...messagePropertyValues, consumerGroupId !== null && consumerGroupId !== void 0 ? consumerGroupId : '', '', ]; redisClient.runScript(scripts_js_1.ERedisScriptName.PUBLISH_MESSAGE, [ keys.keyQueueProperties, keys.keyQueuePriority, keys.keyQueuePending, keys.keyQueueScheduled, keys.keyQueuePublished, keys.keyQueueConsumerGroups, keyMessage, ], scriptArgs, (err, reply) => { if (err) { logger.error(`Failed to publish message ${messageId}`, err); return cb(err); } switch (reply) { case 'OK': logger.debug(`Successfully published message ${messageId} to queue ${queueName}`); return cb(); case 'QUEUE_NOT_FOUND': logger.error(`Queue ${queueName} not found for message ${messageId}`); return cb(new index_js_3.QueueNotFoundError({ metadata: { queue: destinationQueue, }, })); case 'CONSUMER_GROUP_NOT_FOUND': logger.error(`Consumer group ${consumerGroupId} not found for queue ${queueName}`); return cb(new index_js_3.ConsumerGroupNotFoundError()); case 'MESSAGE_PRIORITY_REQUIRED': logger.error(`Priority required for message ${messageId} but not provided`); return cb(new index_js_3.MessagePriorityRequiredError()); case 'MESSAGE_ALREADY_EXISTS': logger.error(`A message with ${messageId} already exists`); return cb(new index_js_3.MessageAlreadyExistsError()); case 'PRIORITY_QUEUING_NOT_ENABLED': logger.error(`Priority queuing not enabled for queue ${queueName}`); return cb(new index_js_3.PriorityQueuingNotEnabledError()); case 'UNKNOWN_QUEUE_TYPE': logger.error(`Unknown queue type for queue ${queueName}`); return cb(new index_js_3.InvalidQueueTypeError()); case 'QUEUE_STOPPED': logger.error(`Queue ${queueName} is in STOPPED state, cannot publish message ${messageId}`); return cb(new index_js_3.QueueStoppedError({ metadata: { queue: destinationQueue } })); case 'QUEUE_LOCKED': logger.error(`Queue ${queueName} is in LOCKED state, cannot publish message ${messageId}. Provide a valid lock ID to publish.`); return cb(new index_js_3.QueueLockedError({ metadata: { queue: destinationQueue } })); case 'QUEUE_INVALID_STATE': logger.error(`Queue ${queueName} is in invalid state, cannot publish message ${messageId}`); return cb(new index_js_3.InvalidQueueStateError({ metadata: { queue: destinationQueue, }, })); default: logger.error(`Unknown error while publishing message ${messageId}: ${reply}`); return cb(new index_js_3.UnexpectedScriptReplyError({ metadata: { reply } })); } }); } //# sourceMappingURL=_publish-message.js.map