redis-smq
Version:
A simple high-performance Redis message queue for Node.js.
141 lines • 3.87 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MessageState = void 0;
const uuid_1 = require("uuid");
class MessageState {
constructor() {
this.publishedAt = null;
this.scheduledAt = null;
this.scheduledCronFired = false;
this.attempts = 0;
this.scheduledRepeatCount = 0;
this.expired = false;
this.nextScheduledDelay = 0;
this.nextRetryDelay = 0;
this.uuid = (0, uuid_1.v4)();
}
setPublishedAt(timestamp) {
this.publishedAt = timestamp;
return this;
}
setScheduledAt(timestamp) {
this.scheduledAt = timestamp;
return this;
}
setNextScheduledDelay(delay) {
this.nextScheduledDelay = delay;
return this;
}
getSetNextScheduledDelay() {
if (this.nextScheduledDelay > 0) {
const delay = this.nextScheduledDelay;
this.nextScheduledDelay = 0;
return delay;
}
return 0;
}
setNextRetryDelay(delay) {
this.nextRetryDelay = delay;
return this;
}
getSetNextRetryDelay() {
if (this.nextRetryDelay > 0) {
const delay = this.nextRetryDelay;
this.nextRetryDelay = 0;
return delay;
}
return 0;
}
hasDelay() {
return this.nextScheduledDelay > 0 || this.nextRetryDelay > 0;
}
resetMessageScheduledRepeatCount() {
this.scheduledRepeatCount = 0;
return this;
}
incrAttempts() {
this.setAttempts(this.attempts + 1);
return this.attempts;
}
setAttempts(attempts) {
this.attempts = attempts;
return this;
}
setMessageScheduledCronFired(fired) {
this.scheduledCronFired = fired;
return this;
}
incrMessageScheduledRepeatCount() {
this.scheduledRepeatCount += 1;
return this.scheduledRepeatCount;
}
setExpired(expired) {
this.expired = expired;
return this;
}
reset() {
this.publishedAt = null;
this.scheduledAt = null;
this.attempts = 0;
this.expired = false;
this.nextScheduledDelay = 0;
this.scheduledCronFired = false;
this.scheduledRepeatCount = 0;
return this;
}
getPublishedAt() {
return this.publishedAt;
}
getScheduledAt() {
return this.scheduledAt;
}
getAttempts() {
return this.attempts;
}
getMessageScheduledRepeatCount() {
return this.scheduledRepeatCount;
}
getId() {
return this.uuid;
}
hasScheduledCronFired() {
return this.scheduledCronFired;
}
hasExpired() {
return this.expired;
}
getSetExpired(ttl, createdAt) {
if (ttl) {
const curTime = new Date().getTime();
const expired = createdAt + ttl - curTime <= 0;
this.setExpired(expired);
}
return this.hasExpired();
}
getSetNextDelay() {
const retryDelay = this.getSetNextRetryDelay();
if (retryDelay) {
return retryDelay;
}
const scheduledDelay = this.getSetNextScheduledDelay();
if (scheduledDelay) {
return scheduledDelay;
}
return 0;
}
toJSON() {
return {
uuid: this.uuid,
publishedAt: this.publishedAt,
scheduledAt: this.scheduledAt,
scheduledCronFired: this.scheduledCronFired,
attempts: this.attempts,
scheduledRepeatCount: this.scheduledRepeatCount,
expired: this.expired,
nextScheduledDelay: this.nextScheduledDelay,
nextRetryDelay: this.nextRetryDelay,
};
}
}
exports.MessageState = MessageState;
//# sourceMappingURL=message-state.js.map