redis-smq
Version:
A high-performance, reliable, and scalable message queue for Node.js.
145 lines • 7.57 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports._executeUnacknowledgementScript = _executeUnacknowledgementScript;
const index_js_1 = require("../../../../queue-manager/index.js");
const index_js_2 = require("../types/index.js");
const redis_keys_js_1 = require("../../../../common/redis/redis-keys/redis-keys.js");
const scripts_js_1 = require("../../../../common/redis/scripts.js");
const index_js_3 = require("../../../../errors/index.js");
const configuration_js_1 = require("../../../../config-manager/configuration.js");
const index_js_4 = require("../../../../message/index.js");
const with_shared_pool_connection_js_1 = require("../../../../common/redis/redis-connection-pool/with-shared-pool-connection.js");
function _executeUnacknowledgementScript(queue, messages, consumerId, logger, cb) {
const { queueParams, groupId } = queue;
(0, with_shared_pool_connection_js_1.withSharedPoolConnection)((client, done) => {
var _a, _b;
const { keyQueueRequeued, keyQueueDeadLetter, keyQueueProperties } = redis_keys_js_1.redisKeys.getQueueKeys(queueParams.ns, queueParams.name, groupId);
const { enabled, expire, queueSize } = configuration_js_1.Configuration.getConfig().messageAudit.deadLetteredMessages;
const historyConfig = configuration_js_1.Configuration.getConfig().messageAudit.unacknowledgementHistory;
const maxHistorySize = historyConfig.maxSize;
const historyEnabled = historyConfig.enabled;
const staticKeys = [
keyQueueRequeued,
keyQueueDeadLetter,
keyQueueProperties,
];
const staticArgs = [
index_js_2.EMessageUnacknowledgementAction.DELAY,
index_js_2.EMessageUnacknowledgementAction.REQUEUE,
Number(enabled),
expire,
queueSize * -1,
index_js_4.EMessageProperty.STATUS,
index_js_1.EQueueProperty.PROCESSING_MESSAGES_COUNT,
index_js_1.EQueueProperty.DEAD_LETTERED_MESSAGES_COUNT,
index_js_1.EQueueProperty.REQUEUED_MESSAGES_COUNT,
index_js_4.EMessagePropertyStatus.UNACK_REQUEUING,
index_js_4.EMessagePropertyStatus.DEAD_LETTERED,
index_js_4.EMessageProperty.DEAD_LETTERED_AT,
index_js_4.EMessageProperty.UNACKNOWLEDGED_AT,
index_js_4.EMessageProperty.LAST_UNACKNOWLEDGED_AT,
index_js_4.EMessageProperty.EXPIRED,
index_js_1.EQueueProperty.OPERATIONAL_STATE,
index_js_1.EQueueOperationalState.ACTIVE,
index_js_1.EQueueOperationalState.PAUSED,
index_js_1.EQueueOperationalState.STOPPED,
index_js_1.EQueueOperationalState.LOCKED,
maxHistorySize,
];
const dynamicKeys = [];
const dynamicArgs = [];
const actions = new Map();
for (const msg of messages) {
const messageId = msg.message.getId();
const { keyMessage, keyMessageUnacknowledgementHistory } = redis_keys_js_1.redisKeys.getMessageKeys(messageId);
const { keyQueueProcessing } = redis_keys_js_1.redisKeys.getQueueConsumerKeys(queueParams, consumerId);
const state = msg.message.getMessageState();
const now = Date.now();
const unacknowledgedAt = (_a = state === null || state === void 0 ? void 0 : state.getUnacknowledgedAt()) !== null && _a !== void 0 ? _a : now;
const lastUnacknowledgedAt = now;
const deadLetteredAt = msg.resolution.action === index_js_2.EMessageUnacknowledgementAction.DEAD_LETTER
? now
: '';
dynamicKeys.push(keyQueueProcessing, keyMessage, keyMessageUnacknowledgementHistory);
let historyRecordStr = '';
if (historyEnabled) {
const historyRecord = {
messageId,
cause: msg.resolution.cause,
action: msg.resolution.action,
timestamp: lastUnacknowledgedAt,
retryCount: state.getAttempts(),
queue,
consumerId,
deadLetterCause: msg.resolution.action ===
index_js_2.EMessageUnacknowledgementAction.DEAD_LETTER
? msg.resolution.deadLetterCause
: undefined,
};
historyRecordStr = JSON.stringify(historyRecord);
}
dynamicArgs.push(messageId, msg.resolution.action, deadLetteredAt, Number((_b = state === null || state === void 0 ? void 0 : state.getExpired()) !== null && _b !== void 0 ? _b : false), unacknowledgedAt, lastUnacknowledgedAt, historyRecordStr);
actions.set(messageId, msg.resolution);
}
logger.debug(`Executing script for queue ${queueParams.ns}:${queueParams.name} with ${messages.length} messages`);
client.runScript(scripts_js_1.ERedisScriptName.UNACKNOWLEDGE_MESSAGE, [...staticKeys, ...dynamicKeys], [...staticArgs, ...dynamicArgs], (err, reply) => {
if (err) {
logger.error(`Script execution failed: ${err.message}`);
return done(err);
}
if (typeof reply === 'string') {
if (reply === 'QUEUE_NOT_FOUND') {
return done(new index_js_3.QueueNotFoundError({
metadata: {
queue: queueParams,
},
}));
}
if (reply === 'QUEUE_STOPPED') {
return done(new index_js_3.QueueStoppedError({
metadata: {
queue: queueParams,
},
}));
}
if (reply === 'QUEUE_LOCKED') {
return done(new index_js_3.QueueLockedError({
metadata: {
queue: queueParams,
},
}));
}
if (reply === 'QUEUE_INVALID_STATE') {
return done(new index_js_3.InvalidQueueStateError({
metadata: {
queue: queueParams,
},
}));
}
if (reply === 'INVALID_ARGS_ERROR') {
return done(new index_js_3.UnexpectedScriptReplyError({
message: 'Invalid arguments error in UNACKNOWLEDGE_MESSAGE script',
metadata: {
reply,
},
}));
}
return done(new index_js_3.UnexpectedScriptReplyError({
metadata: { reply },
}));
}
const count = Number(reply);
if (isNaN(count)) {
return done(new index_js_3.UnexpectedScriptReplyError({
metadata: { reply },
}));
}
if (count !== messages.length) {
logger.warn(`Script processed ${count}/${messages.length} messages for queue ${queueParams.ns}:${queueParams.name}`);
}
logger.debug(`Successfully processed ${count} messages for queue ${queueParams.ns}:${queueParams.name}`);
done(null, Object.fromEntries(actions));
});
}, cb);
}
//# sourceMappingURL=_execute-unacknowledgement-script.js.map