redis-smq
Version:
A high-performance, reliable, and scalable message queue for Node.js.
197 lines • 12.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PublishScheduledWorker = void 0;
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 _from_message_js_1 = require("../../../../message-manager/_/_from-message.js");
const _get_message_js_1 = require("../../../../message-manager/_/_get-message.js");
const index_js_1 = require("../../../../message/index.js");
const index_js_2 = require("../../../../queue-manager/index.js");
const with_shared_pool_connection_js_1 = require("../../../../common/redis/redis-connection-pool/with-shared-pool-connection.js");
const index_js_3 = require("../../../../errors/index.js");
const queue_worker_abstract_js_1 = require("../queue-worker-abstract.js");
class PublishScheduledWorker extends queue_worker_abstract_js_1.QueueWorkerAbstract {
constructor() {
super(...arguments);
this.fetchMessageIds = (cb) => {
(0, with_shared_pool_connection_js_1.withSharedPoolConnection)((redisClient, cb) => {
const { keyQueueScheduled } = redis_keys_js_1.redisKeys.getQueueKeys(this.queueParsedParams.queueParams.ns, this.queueParsedParams.queueParams.name, this.queueParsedParams.groupId);
const currentTimestamp = Date.now();
redisClient.zrangebyscore(keyQueueScheduled, 0, currentTimestamp, 0, 99, (err, ids) => {
if (err) {
this.logger.error('Error fetching scheduled message IDs', err);
return cb(err);
}
cb(null, ids || []);
});
}, cb);
};
this.fetchMessages = (ids, cb) => {
if (!ids.length) {
cb(null, []);
return;
}
this.logger.debug(`Fetching ${ids.length} messages from storage`);
(0, with_shared_pool_connection_js_1.withSharedPoolConnection)((redisClient, cb) => {
(0, _get_message_js_1._getMessages)(redisClient, ids, (err, messages) => {
if (err) {
this.logger.error('Error fetching messages', err);
cb(err);
}
else {
const messageCount = (messages === null || messages === void 0 ? void 0 : messages.length) || 0;
this.logger.debug(`Successfully retrieved ${messageCount} messages`);
cb(null, messages || []);
}
});
}, cb);
};
this.enqueueMessages = (messages, cb) => {
if (!messages.length)
return cb();
this.logger.debug(`Preparing to enqueue ${messages.length} messages`);
(0, with_shared_pool_connection_js_1.withSharedPoolConnection)((redisClient, cb) => {
const { keyQueueProperties, keyQueuePending, keyQueuePriority, keyQueueScheduled, keyQueuePublished, keyQueueDeadLetter, keyQueueConsumerGroups, } = redis_keys_js_1.redisKeys.getQueueKeys(this.queueParsedParams.queueParams.ns, this.queueParsedParams.queueParams.name, this.queueParsedParams.groupId);
const keys = [
keyQueueProperties,
keyQueuePending,
keyQueuePublished,
keyQueuePriority,
keyQueueScheduled,
keyQueueDeadLetter,
keyQueueConsumerGroups,
];
const argv = [
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.EQueueProperty.DEAD_LETTERED_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,
index_js_1.EMessagePropertyStatus.PENDING,
index_js_1.EMessagePropertyStatus.SCHEDULED,
index_js_1.EMessagePropertyStatus.DEAD_LETTERED,
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,
];
redis_smq_common_1.async.eachOf(messages, (msg, index, done) => {
var _a, _b, _c;
const messageId = msg.getId();
this.logger.debug(`Processing message ${messageId} (${index + 1}/${messages.length})`);
const ts = Date.now();
const scheduledMessageId = msg.getId();
const consumerGroupId = msg.getConsumerGroupId();
const { keyMessage: keyScheduledMessage } = redis_keys_js_1.redisKeys.getMessageKeys(scheduledMessageId);
const nextScheduleTimestamp = msg.getNextScheduledTimestamp();
const scheduledMessageState = msg.getMessageState();
const messagePriority = (_a = msg.producibleMessage.getPriority()) !== null && _a !== void 0 ? _a : '';
const scheduledMessageCronFired = Number(scheduledMessageState.isScheduledCronFired());
const scheduledMessageEffectiveScheduledDelay = Number(scheduledMessageState.getEffectiveScheduledDelay());
const scheduledMessageRepeatCount = scheduledMessageState.getScheduledRepeatCount();
let newMessageId = '';
let newMessageJSON = '';
let newMessagePublishedAt = '';
let newKeyMessage = '';
if (nextScheduleTimestamp) {
const newMessage = (0, _from_message_js_1._fromMessage)(msg);
newMessage.producibleMessage.resetScheduledParams();
const newMessageState = newMessage
.getMessageState()
.setPublishedAt(ts)
.setScheduledMessageParentId(scheduledMessageId);
newMessageId = newMessageState.getId();
newKeyMessage = redis_keys_js_1.redisKeys.getMessageKeys(newMessageId).keyMessage;
newMessageJSON = JSON.stringify(newMessage.toJSON());
newMessagePublishedAt = ts;
scheduledMessageState.setLastScheduledAt(ts).incrScheduledTimes();
}
else {
scheduledMessageState.setPublishedAt(ts);
}
const scheduledMessageScheduledTimes = scheduledMessageState.getScheduledTimes();
const scheduledMessageLastScheduledAt = (_b = scheduledMessageState.getLastScheduledAt()) !== null && _b !== void 0 ? _b : '';
const scheduledMessagePublishedAt = (_c = scheduledMessageState.getPublishedAt()) !== null && _c !== void 0 ? _c : '';
keys.push(newKeyMessage, keyScheduledMessage);
argv.push(newMessageId, newMessageJSON, messagePriority, newMessagePublishedAt, scheduledMessageId, nextScheduleTimestamp, scheduledMessageLastScheduledAt, scheduledMessageScheduledTimes, scheduledMessagePublishedAt, scheduledMessageCronFired, scheduledMessageRepeatCount, scheduledMessageEffectiveScheduledDelay, consumerGroupId !== null && consumerGroupId !== void 0 ? consumerGroupId : '', ts);
done();
}, (err) => {
if (err) {
this.logger.error('Error during message processing for enqueue', err);
return cb(err);
}
this.logger.debug(`Executing PUBLISH_SCHEDULED_MESSAGE script for ${messages.length} messages`);
redisClient.runScript(scripts_js_1.ERedisScriptName.PUBLISH_SCHEDULED, keys, argv, (err, reply) => {
if (err) {
this.logger.error('Error executing publish scheduled message script', err);
return cb(err);
}
if (typeof reply === 'string') {
if (reply.startsWith('PUBLISH_ERROR:')) {
const [, failedMessageId, errorCode] = reply.split(':');
this.logger.error(`Failed to publish repeating message ${failedMessageId}: ${errorCode}`);
return cb(new redis_smq_common_1.PanicError({
message: `Failed to publish scheduled message: ${errorCode}`,
}));
}
this.logger.error(`Script execution returned an error: ${reply}`);
return cb(new redis_smq_common_1.PanicError({
message: `PUBLISH_SCHEDULED_MESSAGE script failed: ${reply}`,
}));
}
if (typeof reply === 'number') {
if (reply !== messages.length) {
this.logger.warn(`Script reported processing ${reply} messages, but expected ${messages.length}.`);
}
this.logger.info(`Successfully published ${reply} scheduled messages.`);
return cb();
}
this.logger.error(`Script execution returned unexpected response: ${reply}`);
cb(new index_js_3.UnexpectedScriptReplyError({ metadata: { reply } }));
});
});
}, cb);
};
this.work = (cb) => {
redis_smq_common_1.async.waterfall([this.fetchMessageIds, this.fetchMessages, this.enqueueMessages], (err) => {
if (err) {
this.logger.error('Error in publish scheduled messages workflow', err);
}
cb(err);
});
};
}
}
exports.PublishScheduledWorker = PublishScheduledWorker;
exports.default = PublishScheduledWorker;
//# sourceMappingURL=publish-scheduled.worker.js.map