redis-smq
Version:
A simple high-performance Redis message queue for Node.js.
81 lines • 4.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports._deleteMessage = _deleteMessage;
const redis_smq_common_1 = require("redis-smq-common");
const scripts_js_1 = require("../../../common/redis-client/scripts/scripts.js");
const redis_keys_js_1 = require("../../../common/redis-keys/redis-keys.js");
const index_js_1 = require("../../queue/index.js");
const index_js_2 = require("../errors/index.js");
const index_js_3 = require("../types/index.js");
const _get_message_js_1 = require("./_get-message.js");
function isDeleteMessageResponse(reply) {
return (typeof reply === 'string' || (Array.isArray(reply) && reply.length === 4));
}
function _deleteMessage(redisClient, messageId, cb) {
const keys = [];
const argv = [];
const ids = typeof messageId === 'string' ? [messageId] : messageId;
argv.push(index_js_1.EQueueProperty.QUEUE_TYPE, index_js_1.EQueueProperty.MESSAGES_COUNT, index_js_1.EQueueType.PRIORITY_QUEUE, index_js_1.EQueueType.LIFO_QUEUE, index_js_1.EQueueType.FIFO_QUEUE, index_js_3.EMessageProperty.STATUS, index_js_3.EMessagePropertyStatus.PROCESSING, index_js_3.EMessagePropertyStatus.ACKNOWLEDGED, index_js_3.EMessagePropertyStatus.PENDING, index_js_3.EMessagePropertyStatus.SCHEDULED, index_js_3.EMessagePropertyStatus.DEAD_LETTERED, index_js_3.EMessagePropertyStatus.UNACK_DELAYING, index_js_3.EMessagePropertyStatus.UNACK_REQUEUING);
const deleteResponse = {
status: 'MESSAGE_NOT_DELETED',
stats: {
processed: 0,
success: 0,
notFound: 0,
inProcess: 0,
},
};
const stats = deleteResponse.stats;
if (ids.length === 0) {
return cb(null, deleteResponse);
}
redis_smq_common_1.async.each(ids, (id, _, done) => {
(0, _get_message_js_1._getMessage)(redisClient, id, (err, message) => {
if (err) {
if (err instanceof index_js_2.MessageMessageNotFoundError) {
stats.notFound += 1;
stats.processed += 1;
return done();
}
return done(err);
}
else if (!message)
done(new redis_smq_common_1.CallbackEmptyReplyError());
else {
const { keyQueueScheduled, keyQueueDelayed, keyQueueRequeued, keyQueueProperties, keyQueueDL, keyQueueAcknowledged, keyQueuePriorityPending, keyQueuePending, keyQueueMessages, } = redis_keys_js_1.redisKeys.getQueueKeys(message.getDestinationQueue(), message.getConsumerGroupId());
const { keyMessage } = redis_keys_js_1.redisKeys.getMessageKeys(id);
keys.push(keyQueueScheduled, keyQueueDelayed, keyQueueRequeued, keyMessage, keyQueueProperties, keyQueuePending, keyQueueDL, keyQueueAcknowledged, keyQueuePriorityPending, keyQueueMessages);
argv.push(id);
done();
}
});
}, (err) => {
if (err)
return cb(err);
if (keys.length && argv.length) {
return redisClient.runScript(scripts_js_1.ELuaScriptName.DELETE_MESSAGE, keys, argv, (err, reply) => {
if (err)
return cb(err);
if (!isDeleteMessageResponse(reply))
return cb(new redis_smq_common_1.CallbackInvalidReplyError());
if (typeof reply === 'string') {
if (reply === 'INVALID_PARAMETERS')
return cb(new index_js_2.MessageInvalidParametersError());
return cb(new redis_smq_common_1.CallbackInvalidReplyError());
}
const [processed, success, notFound, inProcess] = reply;
stats.processed += processed;
stats.success += success;
stats.notFound += notFound;
stats.inProcess += inProcess;
deleteResponse.status =
(stats.processed === stats.success && 'OK') ||
(stats.success && 'PARTIAL_SUCCESS') ||
'MESSAGE_NOT_DELETED';
cb(null, deleteResponse);
});
}
return cb(null, deleteResponse);
});
}
//# sourceMappingURL=_delete-message.js.map