UNPKG

redis-smq

Version:

A simple high-performance Redis message queue for Node.js.

154 lines 5.98 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MessageEnvelope = void 0; const cron_parser_1 = __importDefault(require("cron-parser")); const index_js_1 = require("./errors/index.js"); const message_state_js_1 = require("./message-state.js"); const index_js_2 = require("./types/index.js"); class MessageEnvelope { constructor(producibleMessage) { this.status = index_js_2.EMessagePropertyStatus.UNPUBLISHED; this.destinationQueue = null; this.consumerGroupId = null; this.producibleMessage = producibleMessage; this.messageState = new message_state_js_1.MessageState(); const scheduledDelay = this.producibleMessage.getScheduledDelay(); if (scheduledDelay) this.messageState.setNextScheduledDelay(scheduledDelay); } getMessageState() { return this.messageState; } setMessageState(m) { this.messageState = m; return this; } getId() { return this.messageState.getId(); } getSetExpired() { return this.getMessageState().getSetExpired(this.producibleMessage.getTTL(), this.producibleMessage.getCreatedAt()); } getStatus() { return this.status; } setDestinationQueue(queue) { if (this.destinationQueue !== null) { throw new index_js_1.MessageDestinationQueueAlreadySetError(); } this.destinationQueue = queue; return this; } setStatus(s) { this.status = s; return this; } getDestinationQueue() { if (!this.destinationQueue) { throw new index_js_1.MessageDestinationQueueRequiredError(); } return this.destinationQueue; } hasNextDelay() { return this.messageState.hasDelay(); } getNextScheduledTimestamp() { if (this.isSchedulable()) { const messageState = this.getMessageState(); const delay = messageState.getSetNextDelay(); if (delay) { return Date.now() + delay; } const msgScheduledCron = this.producibleMessage.getScheduledCRON(); const cronTimestamp = msgScheduledCron ? cron_parser_1.default.parseExpression(msgScheduledCron).next().getTime() : 0; const msgScheduledRepeat = this.producibleMessage.getScheduledRepeat(); let repeatTimestamp = 0; if (msgScheduledRepeat) { const newCount = messageState.getMessageScheduledRepeatCount() + 1; if (newCount <= msgScheduledRepeat) { const scheduledRepeatPeriod = this.producibleMessage.getScheduledRepeatPeriod(); const now = Date.now(); if (scheduledRepeatPeriod) { repeatTimestamp = now + scheduledRepeatPeriod; } else { repeatTimestamp = now; } } } if (repeatTimestamp && cronTimestamp) { if (repeatTimestamp < cronTimestamp && messageState.hasScheduledCronFired()) { messageState.incrMessageScheduledRepeatCount(); return repeatTimestamp; } } if (cronTimestamp) { messageState.resetMessageScheduledRepeatCount(); messageState.setMessageScheduledCronFired(true); return cronTimestamp; } if (repeatTimestamp) { messageState.incrMessageScheduledRepeatCount(); return repeatTimestamp; } } return 0; } getExchange() { const exchange = this.producibleMessage.getExchange(); if (!exchange) { throw new index_js_1.MessageMessageExchangeRequiredError(); } return exchange; } toString() { return JSON.stringify(this); } setConsumerGroupId(consumerGroupId) { this.consumerGroupId = consumerGroupId; return this; } getConsumerGroupId() { return this.consumerGroupId; } toJSON() { return { createdAt: this.producibleMessage.getCreatedAt(), ttl: this.producibleMessage.getTTL(), retryThreshold: this.producibleMessage.getRetryThreshold(), retryDelay: this.producibleMessage.getRetryDelay(), consumeTimeout: this.producibleMessage.getConsumeTimeout(), body: this.producibleMessage.getBody(), priority: this.producibleMessage.getPriority(), scheduledCron: this.producibleMessage.getScheduledCRON(), scheduledDelay: this.producibleMessage.getScheduledDelay(), scheduledRepeatPeriod: this.producibleMessage.getScheduledRepeatPeriod(), scheduledRepeat: this.producibleMessage.getScheduledRepeat(), exchange: this.getExchange(), destinationQueue: this.getDestinationQueue(), consumerGroupId: this.getConsumerGroupId(), }; } transfer() { return Object.assign(Object.assign({}, this.toJSON()), { id: this.getId(), messageState: this.getMessageState().toJSON(), status: this.getStatus() }); } hasRetryThresholdExceeded() { const threshold = this.producibleMessage.getRetryThreshold(); return this.messageState.getAttempts() + 1 >= threshold; } isSchedulable() { return this.hasNextDelay() || this.isPeriodic(); } isPeriodic() { return (this.producibleMessage.getScheduledCRON() !== null || this.producibleMessage.getScheduledRepeat() > 0); } } exports.MessageEnvelope = MessageEnvelope; //# sourceMappingURL=message-envelope.js.map