@message-queue-toolkit/core 13.3.2 → 13.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -10,11 +10,20 @@ type CommonQueueLocator = {
|
|
|
10
10
|
queueName: string;
|
|
11
11
|
};
|
|
12
12
|
export declare abstract class AbstractQueueService<MessagePayloadSchemas extends object, MessageEnvelopeType extends object, DependenciesType extends QueueDependencies, QueueConfiguration extends object, QueueLocatorType extends object = CommonQueueLocator, OptionsType extends QueueOptions<QueueConfiguration, QueueLocatorType> = QueueOptions<QueueConfiguration, QueueLocatorType>, ExecutionContext = undefined, PrehandlerOutput = undefined> {
|
|
13
|
+
/**
|
|
14
|
+
* Used to keep track of the number of `retryLater` results received for a message to be able to
|
|
15
|
+
* calculate the delay for the next retry
|
|
16
|
+
*/
|
|
17
|
+
private readonly messageNumberOfRetriesField;
|
|
18
|
+
/**
|
|
19
|
+
* Used to know when the message was sent initially so we can have a max retry date and avoid
|
|
20
|
+
* a infinite `retryLater` loop
|
|
21
|
+
*/
|
|
22
|
+
protected readonly messageTimestampField: string;
|
|
13
23
|
protected readonly errorReporter: ErrorReporter;
|
|
14
24
|
readonly logger: Logger;
|
|
15
25
|
protected readonly messageIdField: string;
|
|
16
26
|
protected readonly messageTypeField: string;
|
|
17
|
-
protected readonly messageTimestampField: string;
|
|
18
27
|
protected readonly logMessages: boolean;
|
|
19
28
|
protected readonly creationConfig?: QueueConfiguration;
|
|
20
29
|
protected readonly locatorConfig?: QueueLocatorType;
|
|
@@ -38,7 +47,11 @@ export declare abstract class AbstractQueueService<MessagePayloadSchemas extends
|
|
|
38
47
|
protected handleMessageProcessed(message: MessagePayloadSchemas | null, processingResult: MessageProcessingResult, messageId?: string): void;
|
|
39
48
|
protected processPrehandlersInternal(preHandlers: Prehandler<MessagePayloadSchemas, ExecutionContext, PrehandlerOutput>[], message: MessagePayloadSchemas): Promise<PrehandlerOutput>;
|
|
40
49
|
protected preHandlerBarrierInternal<BarrierOutput>(barrier: BarrierCallback<MessagePayloadSchemas, ExecutionContext, PrehandlerOutput, BarrierOutput> | undefined, message: MessagePayloadSchemas, executionContext: ExecutionContext, preHandlerOutput: PrehandlerOutput): Promise<BarrierResult<BarrierOutput>>;
|
|
41
|
-
|
|
50
|
+
shouldBeRetried(message: MessagePayloadSchemas, maxRetryDuration: number): boolean;
|
|
51
|
+
protected getMessageRetryDelayInSeconds(message: MessagePayloadSchemas): number;
|
|
52
|
+
protected updateInternalProperties(message: MessagePayloadSchemas): MessagePayloadSchemas;
|
|
53
|
+
private tryToExtractTimestamp;
|
|
54
|
+
private tryToExtractNumberOfRetries;
|
|
42
55
|
protected abstract resolveNextFunction(preHandlers: Prehandler<MessagePayloadSchemas, ExecutionContext, PrehandlerOutput>[], message: MessagePayloadSchemas, index: number, preHandlerOutput: PrehandlerOutput, resolve: (value: PrehandlerOutput | PromiseLike<PrehandlerOutput>) => void, reject: (err: Error) => void): (preHandlerResult: PrehandlerResult) => void;
|
|
43
56
|
protected resolveNextPreHandlerFunctionInternal(preHandlers: Prehandler<MessagePayloadSchemas, ExecutionContext, PrehandlerOutput>[], executionContext: ExecutionContext, message: MessagePayloadSchemas, index: number, preHandlerOutput: PrehandlerOutput, resolve: (value: PrehandlerOutput | PromiseLike<PrehandlerOutput>) => void, reject: (err: Error) => void): (preHandlerResult: PrehandlerResult) => void;
|
|
44
57
|
protected abstract processPrehandlers(message: MessagePayloadSchemas, messageType: string): Promise<PrehandlerOutput>;
|
|
@@ -3,14 +3,24 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.AbstractQueueService = void 0;
|
|
4
4
|
const node_util_1 = require("node:util");
|
|
5
5
|
const node_core_1 = require("@lokalise/node-core");
|
|
6
|
+
const dateUtils_1 = require("../utils/dateUtils");
|
|
6
7
|
const toDateProcessor_1 = require("../utils/toDateProcessor");
|
|
7
8
|
const HandlerSpy_1 = require("./HandlerSpy");
|
|
8
9
|
class AbstractQueueService {
|
|
10
|
+
/**
|
|
11
|
+
* Used to keep track of the number of `retryLater` results received for a message to be able to
|
|
12
|
+
* calculate the delay for the next retry
|
|
13
|
+
*/
|
|
14
|
+
messageNumberOfRetriesField = '_internalNumberOfRetries';
|
|
15
|
+
/**
|
|
16
|
+
* Used to know when the message was sent initially so we can have a max retry date and avoid
|
|
17
|
+
* a infinite `retryLater` loop
|
|
18
|
+
*/
|
|
19
|
+
messageTimestampField;
|
|
9
20
|
errorReporter;
|
|
10
21
|
logger;
|
|
11
22
|
messageIdField;
|
|
12
23
|
messageTypeField;
|
|
13
|
-
messageTimestampField;
|
|
14
24
|
logMessages;
|
|
15
25
|
creationConfig;
|
|
16
26
|
locatorConfig;
|
|
@@ -108,6 +118,37 @@ class AbstractQueueService {
|
|
|
108
118
|
// @ts-ignore
|
|
109
119
|
return await barrier(message, executionContext, preHandlerOutput);
|
|
110
120
|
}
|
|
121
|
+
shouldBeRetried(message, maxRetryDuration) {
|
|
122
|
+
const timestamp = this.tryToExtractTimestamp(message) ?? new Date();
|
|
123
|
+
return !(0, dateUtils_1.isRetryDateExceeded)(timestamp, maxRetryDuration);
|
|
124
|
+
}
|
|
125
|
+
getMessageRetryDelayInSeconds(message) {
|
|
126
|
+
// if not defined, this is the first attempt
|
|
127
|
+
const retries = this.tryToExtractNumberOfRetries(message) ?? 0;
|
|
128
|
+
// exponential backoff -> (2 ^ (attempts)) * delay
|
|
129
|
+
// delay = 1 second
|
|
130
|
+
return Math.pow(2, retries);
|
|
131
|
+
}
|
|
132
|
+
updateInternalProperties(message) {
|
|
133
|
+
const messageCopy = { ...message }; // clone the message to avoid mutation
|
|
134
|
+
/**
|
|
135
|
+
* If the message doesn't have a timestamp field -> add it
|
|
136
|
+
* will be used to prevent infinite retries on the same message
|
|
137
|
+
*/
|
|
138
|
+
if (!this.tryToExtractTimestamp(message)) {
|
|
139
|
+
// @ts-ignore
|
|
140
|
+
messageCopy[this.messageTimestampField] = new Date().toISOString();
|
|
141
|
+
this.logger.warn(`${this.messageTimestampField} not defined, adding it automatically`);
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* add/increment the number of retries performed to exponential message delay
|
|
145
|
+
*/
|
|
146
|
+
const numberOfRetries = this.tryToExtractNumberOfRetries(message);
|
|
147
|
+
// @ts-ignore
|
|
148
|
+
messageCopy[this.messageNumberOfRetriesField] =
|
|
149
|
+
numberOfRetries !== undefined ? numberOfRetries + 1 : 0;
|
|
150
|
+
return messageCopy;
|
|
151
|
+
}
|
|
111
152
|
tryToExtractTimestamp(message) {
|
|
112
153
|
// @ts-ignore
|
|
113
154
|
if (this.messageTimestampField in message) {
|
|
@@ -120,6 +161,14 @@ class AbstractQueueService {
|
|
|
120
161
|
}
|
|
121
162
|
return undefined;
|
|
122
163
|
}
|
|
164
|
+
tryToExtractNumberOfRetries(message) {
|
|
165
|
+
if (this.messageNumberOfRetriesField in message &&
|
|
166
|
+
typeof message[this.messageNumberOfRetriesField] === 'number') {
|
|
167
|
+
// @ts-ignore
|
|
168
|
+
return message[this.messageNumberOfRetriesField];
|
|
169
|
+
}
|
|
170
|
+
return undefined;
|
|
171
|
+
}
|
|
123
172
|
// eslint-disable-next-line max-params
|
|
124
173
|
resolveNextPreHandlerFunctionInternal(preHandlers, executionContext, message, index, preHandlerOutput, resolve, reject) {
|
|
125
174
|
return (preHandlerResult) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AbstractQueueService.js","sourceRoot":"","sources":["../../../lib/queues/AbstractQueueService.ts"],"names":[],"mappings":";;;AAAA,yCAAiC;AAGjC,mDAAiE;AAMjE,8DAA6D;AAU7D,6CAAgD;AAYhD,MAAsB,oBAAoB;
|
|
1
|
+
{"version":3,"file":"AbstractQueueService.js","sourceRoot":"","sources":["../../../lib/queues/AbstractQueueService.ts"],"names":[],"mappings":";;;AAAA,yCAAiC;AAGjC,mDAAiE;AAMjE,kDAAwD;AACxD,8DAA6D;AAU7D,6CAAgD;AAYhD,MAAsB,o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GAAG,CAAA;QACZ,CAAC;QAED,OAAO,SAAS,CAAA;IAClB,CAAC;IAEO,2BAA2B,CAAC,OAA8B;QAChE,IACE,IAAI,CAAC,2BAA2B,IAAI,OAAO;YAC3C,OAAO,OAAO,CAAC,IAAI,CAAC,2BAA2B,CAAC,KAAK,QAAQ,EAC7D,CAAC;YACD,aAAa;YACb,OAAO,OAAO,CAAC,IAAI,CAAC,2BAA2B,CAAC,CAAA;QAClD,CAAC;QAED,OAAO,SAAS,CAAA;IAClB,CAAC;IAWD,sCAAsC;IAC5B,qCAAqC,CAC7C,WAAoF,EACpF,gBAAkC,EAClC,OAA8B,EAC9B,KAAa,EACb,gBAAkC,EAClC,OAA0E,EAC1E,MAA4B;QAE5B,OAAO,CAAC,gBAAkC,EAAE,EAAE;YAC5C,IAAI,gBAAgB,CAAC,KAAK,EAAE,CAAC;gBAC3B,MAAM,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAA;YAChC,CAAC;YAED,IAAI,WAAW,CAAC,MAAM,GAAG,KAAK,GAAG,CAAC,EAAE,CAAC;gBACnC,OAAO,CAAC,gBAAgB,CAAC,CAAA;YAC3B,CAAC;iBAAM,CAAC;gBACN,6DAA6D;gBAC7D,WAAW,CAAC,KAAK,CAAC,CAChB,OAAO,EACP,gBAAgB;gBAChB,aAAa;gBACb,gBAAgB,EAChB,IAAI,CAAC,qCAAqC,CACxC,WAAW,EACX,gBAAgB,EAChB,OAAO,EACP,KAAK,GAAG,CAAC,EACT,gBAAgB,EAChB,OAAO,EACP,MAAM,CACP,CACF,CAAA;YACH,CAAC;QACH,CAAC,CAAA;IACH,CAAC;CAqBF;AAtTD,oDAsTC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@message-queue-toolkit/core",
|
|
3
|
-
"version": "13.
|
|
3
|
+
"version": "13.4.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "Useful utilities, interfaces and base classes for message queue handling. Supports AMQP and SQS with a common abstraction on top currently",
|