redis-smq
Version:
A simple high-performance Redis message queue for Node.js.
229 lines • 7.54 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProducibleMessage = void 0;
const cron_parser_1 = __importDefault(require("cron-parser"));
const _get_exchange_direct_transferable_js_1 = require("../exchange/exchange-direct/_/_get-exchange-direct-transferable.js");
const _get_exchange_fanout_transferable_js_1 = require("../exchange/exchange-fan-out/_/_get-exchange-fanout-transferable.js");
const _get_exchange_topic_transferable_js_1 = require("../exchange/exchange-topic/_/_get-exchange-topic-transferable.js");
const index_js_1 = require("../exchange/index.js");
const index_js_2 = require("./errors/index.js");
class ProducibleMessage {
constructor() {
this.ttl = 0;
this.retryThreshold = 3;
this.retryDelay = 60000;
this.consumeTimeout = 0;
this.body = null;
this.priority = null;
this.scheduledCron = null;
this.scheduledDelay = null;
this.scheduledRepeatPeriod = null;
this.scheduledRepeat = 0;
this.exchange = null;
this.createdAt = Date.now();
const { consumeTimeout, retryDelay, ttl, retryThreshold } = ProducibleMessage.defaultConsumeOptions;
this.setConsumeTimeout(consumeTimeout);
this.setRetryDelay(retryDelay);
this.setTTL(ttl);
this.setRetryThreshold(retryThreshold);
}
static setDefaultConsumeOptions(consumeOptions) {
const { ttl = null, retryThreshold = null, retryDelay = null, consumeTimeout = null, } = consumeOptions;
if (ttl !== null)
ProducibleMessage.defaultConsumeOptions.ttl =
ProducibleMessage.validateTTL(ttl);
if (retryDelay !== null)
ProducibleMessage.defaultConsumeOptions.retryDelay =
ProducibleMessage.validateRetryDelay(retryDelay);
if (retryThreshold !== null)
ProducibleMessage.defaultConsumeOptions.retryThreshold =
ProducibleMessage.validateRetryThreshold(retryThreshold);
if (consumeTimeout !== null)
ProducibleMessage.defaultConsumeOptions.consumeTimeout =
ProducibleMessage.validateConsumeTimeout(consumeTimeout);
}
static validateRetryDelay(delay) {
const value = Number(delay);
if (isNaN(value) || value < 0) {
throw new index_js_2.MessageMessagePropertyError();
}
return value;
}
static validateTTL(ttl) {
const value = Number(ttl);
if (isNaN(value) || value < 0) {
throw new index_js_2.MessageMessagePropertyError();
}
return value;
}
static validateConsumeTimeout(timeout) {
const value = Number(timeout);
if (isNaN(value) || value < 0) {
throw new index_js_2.MessageMessagePropertyError();
}
return value;
}
static validateRetryThreshold(threshold) {
const value = Number(threshold);
if (isNaN(value) || value < 0) {
throw new index_js_2.MessageMessagePropertyError();
}
return value;
}
getCreatedAt() {
return this.createdAt;
}
setScheduledRepeatPeriod(period) {
const value = Number(period);
if (isNaN(value) || value < 0) {
throw new index_js_2.MessageMessagePropertyError();
}
this.scheduledRepeatPeriod = value;
return this;
}
setScheduledDelay(delay) {
const value = Number(delay);
if (isNaN(value) || value < 0) {
throw new index_js_2.MessageMessagePropertyError();
}
this.scheduledDelay = value;
return this;
}
getScheduledDelay() {
return this.scheduledDelay;
}
setScheduledCRON(cron) {
cron_parser_1.default.parseExpression(cron);
this.scheduledCron = cron;
return this;
}
setScheduledRepeat(repeat) {
const value = Number(repeat);
if (isNaN(value) || value < 0) {
throw new index_js_2.MessageMessagePropertyError();
}
this.scheduledRepeat = value;
return this;
}
resetScheduledParams() {
this.scheduledCron = null;
this.scheduledDelay = null;
this.scheduledRepeatPeriod = null;
this.scheduledRepeat = 0;
return this;
}
setTTL(ttl) {
this.ttl = ProducibleMessage.validateTTL(ttl);
return this;
}
setConsumeTimeout(timeout) {
this.consumeTimeout = ProducibleMessage.validateConsumeTimeout(timeout);
return this;
}
setRetryThreshold(threshold) {
this.retryThreshold = ProducibleMessage.validateRetryThreshold(threshold);
return this;
}
setRetryDelay(delay) {
this.retryDelay = ProducibleMessage.validateRetryDelay(delay);
return this;
}
setBody(body) {
this.body = body;
return this;
}
setPriority(priority) {
this.priority = priority;
return this;
}
hasPriority() {
return this.priority !== null;
}
disablePriority() {
this.priority = null;
return this;
}
setFanOut(fanOutName) {
const exchange = (0, _get_exchange_fanout_transferable_js_1._getExchangeFanOutTransferable)(fanOutName);
if (exchange instanceof Error)
throw exchange;
this.exchange = exchange;
return this;
}
setTopic(topicParams) {
const exchange = (0, _get_exchange_topic_transferable_js_1._getExchangeTopicTransferable)(topicParams);
if (exchange instanceof Error)
throw exchange;
this.exchange = exchange;
return this;
}
setQueue(queueParams) {
const exchange = (0, _get_exchange_direct_transferable_js_1._getExchangeDirectTransferable)(queueParams);
if (exchange instanceof Error)
throw exchange;
this.exchange = exchange;
return this;
}
getQueue() {
if (this.exchange && this.exchange.type === index_js_1.EExchangeType.DIRECT) {
return this.exchange.params;
}
return null;
}
getTopic() {
if (this.exchange && this.exchange.type === index_js_1.EExchangeType.TOPIC) {
return this.exchange.params;
}
return null;
}
getFanOut() {
if (this.exchange && this.exchange.type === index_js_1.EExchangeType.FANOUT) {
return this.exchange.params;
}
return null;
}
getExchange() {
if (this.exchange) {
return this.exchange;
}
return null;
}
getScheduledRepeatPeriod() {
return this.scheduledRepeatPeriod;
}
getScheduledCRON() {
return this.scheduledCron;
}
getScheduledRepeat() {
return this.scheduledRepeat;
}
getTTL() {
return this.ttl;
}
getRetryThreshold() {
return this.retryThreshold;
}
getRetryDelay() {
return this.retryDelay;
}
getConsumeTimeout() {
return this.consumeTimeout;
}
getPriority() {
return this.priority;
}
getBody() {
return this.body;
}
}
exports.ProducibleMessage = ProducibleMessage;
ProducibleMessage.defaultConsumeOptions = {
ttl: 0,
retryThreshold: 3,
retryDelay: 60000,
consumeTimeout: 0,
};
//# sourceMappingURL=producible-message.js.map