redis-smq
Version:
A high-performance, reliable, and scalable message queue for Node.js.
144 lines • 7.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports._requeueMessage = _requeueMessage;
const redis_smq_common_1 = require("redis-smq-common");
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("../../queue-manager/index.js");
const index_js_2 = require("../../errors/index.js");
const _from_message_js_1 = require("./_from-message.js");
const _get_message_js_1 = require("./_get-message.js");
const index_js_3 = require("../../message/index.js");
const _validate_operation_js_1 = require("../../queue-operation-validator/_/_validate-operation.js");
const index_js_4 = require("../../queue-operation-validator/index.js");
function _requeueMessage(redisClient, messageId, cb) {
redis_smq_common_1.async.waterfall([
(cb) => {
(0, _get_message_js_1._getMessage)(redisClient, messageId, (err, message) => {
if (err)
return cb(err);
if (!message)
return cb(new redis_smq_common_1.CallbackEmptyReplyError());
if (![
index_js_3.EMessagePropertyStatus.ACKNOWLEDGED,
index_js_3.EMessagePropertyStatus.DEAD_LETTERED,
].includes(message.getStatus())) {
return cb(new index_js_2.MessageNotRequeuableError());
}
cb(null, message);
});
},
(message, cb) => {
const queue = message.getDestinationQueue();
(0, _validate_operation_js_1._validateOperation)(redisClient, queue, index_js_4.EQueueOperation.REQUEUE_MESSAGE, (err) => {
if (err)
return cb(err);
cb(null, message);
});
},
(message, cb) => {
var _a, _b;
const ts = Date.now();
const newMessage = (0, _from_message_js_1._fromMessage)(message);
newMessage.producibleMessage.resetScheduledParams();
const newMessageState = newMessage
.getMessageState()
.setPublishedAt(ts)
.setRequeuedMessageParentId(messageId);
const { keyMessage: keyOriginalMessage } = redis_keys_js_1.redisKeys.getMessageKeys(messageId);
const newChildMessageId = newMessageState.getId();
const { keyMessage: keyNewMessage } = redis_keys_js_1.redisKeys.getMessageKeys(newChildMessageId);
const destinationQueue = message.getDestinationQueue();
const consumerGroupId = message.getConsumerGroupId();
const { keyQueueProperties, keyQueuePending, keyQueuePriority, keyQueuePublished, keyQueueScheduled, keyQueueConsumerGroups, } = redis_keys_js_1.redisKeys.getQueueKeys(destinationQueue.ns, destinationQueue.name, consumerGroupId);
const keys = [
keyQueueProperties,
keyQueuePriority,
keyQueuePending,
keyQueuePublished,
keyQueueScheduled,
keyQueueConsumerGroups,
keyOriginalMessage,
keyNewMessage,
];
const argv = [
index_js_1.EQueueProperty.QUEUE_TYPE,
index_js_1.EQueueProperty.MESSAGES_COUNT,
index_js_1.EQueueProperty.PENDING_MESSAGES_COUNT,
index_js_1.EQueueProperty.SCHEDULED_MESSAGES_COUNT,
index_js_1.EQueueType.PRIORITY_QUEUE,
index_js_1.EQueueType.LIFO_QUEUE,
index_js_1.EQueueType.FIFO_QUEUE,
index_js_1.EQueueProperty.OPERATIONAL_STATE,
index_js_1.EQueueProperty.LOCK_ID,
index_js_1.EQueueOperationalState.ACTIVE,
index_js_1.EQueueOperationalState.PAUSED,
index_js_1.EQueueOperationalState.STOPPED,
index_js_1.EQueueOperationalState.LOCKED,
index_js_3.EMessagePropertyStatus.SCHEDULED,
index_js_3.EMessagePropertyStatus.PENDING,
index_js_3.EMessageProperty.ID,
index_js_3.EMessageProperty.STATUS,
index_js_3.EMessageProperty.MESSAGE,
index_js_3.EMessageProperty.SCHEDULED_AT,
index_js_3.EMessageProperty.PUBLISHED_AT,
index_js_3.EMessageProperty.PROCESSING_STARTED_AT,
index_js_3.EMessageProperty.DEAD_LETTERED_AT,
index_js_3.EMessageProperty.ACKNOWLEDGED_AT,
index_js_3.EMessageProperty.UNACKNOWLEDGED_AT,
index_js_3.EMessageProperty.LAST_UNACKNOWLEDGED_AT,
index_js_3.EMessageProperty.LAST_SCHEDULED_AT,
index_js_3.EMessageProperty.REQUEUED_AT,
index_js_3.EMessageProperty.REQUEUE_COUNT,
index_js_3.EMessageProperty.LAST_REQUEUED_AT,
index_js_3.EMessageProperty.LAST_RETRIED_ATTEMPT_AT,
index_js_3.EMessageProperty.SCHEDULED_CRON_FIRED,
index_js_3.EMessageProperty.ATTEMPTS,
index_js_3.EMessageProperty.SCHEDULED_REPEAT_COUNT,
index_js_3.EMessageProperty.EXPIRED,
index_js_3.EMessageProperty.EFFECTIVE_SCHEDULED_DELAY,
index_js_3.EMessageProperty.SCHEDULED_TIMES,
index_js_3.EMessageProperty.SCHEDULED_MESSAGE_PARENT_ID,
index_js_3.EMessageProperty.REQUEUED_MESSAGE_PARENT_ID,
index_js_3.EMessageProperty.LAST_PROCESSED_AT,
'',
newChildMessageId,
JSON.stringify(newMessage.toJSON()),
(_a = message.producibleMessage.getPriority()) !== null && _a !== void 0 ? _a : '',
ts,
(_b = message.getMessageState().getRequeuedAt()) !== null && _b !== void 0 ? _b : ts,
ts,
consumerGroupId !== null && consumerGroupId !== void 0 ? consumerGroupId : '',
];
redisClient.runScript(scripts_js_1.ERedisScriptName.REQUEUE_MESSAGE, keys, argv, (err, reply) => {
if (err)
return cb(err);
if (reply === 1)
return cb(null, newChildMessageId);
if (reply === 0) {
return cb(new index_js_2.MessageNotFoundError({
message: `Could not requeue message ID ${messageId}. Message or consumer group not found.`,
}));
}
if (typeof reply === 'string') {
if (reply.startsWith('REQUEUE_ERROR:')) {
return cb(new index_js_2.RequeueMessageScriptError({
metadata: {
scriptReply: reply,
},
}));
}
return cb(new index_js_2.RequeueMessageScriptError({
metadata: {
scriptReply: reply,
},
}));
}
cb(new index_js_2.UnexpectedScriptReplyError({
metadata: { reply },
}));
});
},
], cb);
}
//# sourceMappingURL=_requeue-message.js.map