@autofleet/rabbit 2.2.9-beta → 2.3.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.
- package/dist/index.d.ts +4 -1
- package/dist/index.js +13 -12
- package/package.json +2 -2
- package/src/index.ts +17 -4
package/dist/index.d.ts
CHANGED
|
@@ -42,7 +42,10 @@ declare class RabbitMq implements IAfRabbitMq {
|
|
|
42
42
|
static validateName(type: string, name: string): void;
|
|
43
43
|
static getPublishOptions(customHeaders?: CustomMessageHeaders): {
|
|
44
44
|
timestamp: number;
|
|
45
|
-
headers:
|
|
45
|
+
headers: {
|
|
46
|
+
redisTimestampValidationKey?: string | undefined;
|
|
47
|
+
creationTimestamp: number;
|
|
48
|
+
};
|
|
46
49
|
};
|
|
47
50
|
PUBLISH_TIMEOUT: number;
|
|
48
51
|
PUBLISH_ERROR_MSG: string;
|
package/dist/index.js
CHANGED
|
@@ -52,7 +52,8 @@ class RabbitMq {
|
|
|
52
52
|
this.RESCONNECT_MSG = 'rabbit: reconnecting';
|
|
53
53
|
this.shouldConsumeMessageByTimestamp = (msg) => __awaiter(this, void 0, void 0, function* () {
|
|
54
54
|
if (msg) {
|
|
55
|
-
const { properties: {
|
|
55
|
+
const { properties: { headers } } = msg;
|
|
56
|
+
const timestamp = headers === null || headers === void 0 ? void 0 : headers.creationTimestamp;
|
|
56
57
|
if (timestamp && (headers === null || headers === void 0 ? void 0 : headers.redisTimestampValidationKey) && this.redisClient) {
|
|
57
58
|
const lastMessageTimestamp = yield this.redisClient.getAsync(headers.redisTimestampValidationKey);
|
|
58
59
|
return !lastMessageTimestamp || (parseInt(lastMessageTimestamp, 10) <= parseInt(timestamp, 10));
|
|
@@ -64,7 +65,8 @@ class RabbitMq {
|
|
|
64
65
|
this.ack = (channel, msg, shouldUpdateRedisTimestamp = false) => (userMsg) => __awaiter(this, void 0, void 0, function* () {
|
|
65
66
|
if (msg) {
|
|
66
67
|
yield channel.ack(msg);
|
|
67
|
-
const { properties: {
|
|
68
|
+
const { properties: { headers } } = msg;
|
|
69
|
+
const timestamp = headers === null || headers === void 0 ? void 0 : headers.creationTimestamp;
|
|
68
70
|
if (shouldUpdateRedisTimestamp && timestamp && (headers === null || headers === void 0 ? void 0 : headers.redisTimestampValidationKey) && this.redisClient) {
|
|
69
71
|
const parsedTimestamp = parseInt(timestamp, 10);
|
|
70
72
|
yield this.redisClient.setAsync(headers.redisTimestampValidationKey, parsedTimestamp, 'EX', 3600);
|
|
@@ -76,19 +78,15 @@ class RabbitMq {
|
|
|
76
78
|
if (!skipRetry
|
|
77
79
|
&& (!msg.properties.headers[RETRY_HEADER]
|
|
78
80
|
|| parseInt(msg.properties.headers[RETRY_HEADER], 10) < options.retries)) {
|
|
79
|
-
yield this.sendToQueue(queue, RabbitMq.parseMsg(msg).content, options, {
|
|
80
|
-
[RETRY_HEADER]: msg.properties.headers[RETRY_HEADER]
|
|
81
|
+
yield this.sendToQueue(queue, RabbitMq.parseMsg(msg).content, options, Object.assign(Object.assign({}, msg.properties.headers), { [RETRY_HEADER]: msg.properties.headers[RETRY_HEADER]
|
|
81
82
|
? msg.properties.headers[RETRY_HEADER] + 1
|
|
82
|
-
: 1
|
|
83
|
-
});
|
|
83
|
+
: 1 }));
|
|
84
84
|
}
|
|
85
85
|
else {
|
|
86
86
|
const deadQueue = `${queue}-dead`;
|
|
87
|
-
yield this.sendToQueue(deadQueue, RabbitMq.parseMsg(msg).content, deadQueueOptions, {
|
|
88
|
-
[RETRY_HEADER]: msg.properties.headers[RETRY_HEADER]
|
|
87
|
+
yield this.sendToQueue(deadQueue, RabbitMq.parseMsg(msg).content, deadQueueOptions, Object.assign(Object.assign({}, msg.properties.headers), { [RETRY_HEADER]: msg.properties.headers[RETRY_HEADER]
|
|
89
88
|
? msg.properties.headers[RETRY_HEADER] + 1
|
|
90
|
-
: 1
|
|
91
|
-
});
|
|
89
|
+
: 1 }));
|
|
92
90
|
}
|
|
93
91
|
yield channel.ack(msg);
|
|
94
92
|
}
|
|
@@ -122,7 +120,7 @@ class RabbitMq {
|
|
|
122
120
|
static getPublishOptions(customHeaders = {}) {
|
|
123
121
|
return {
|
|
124
122
|
timestamp: moment_1.default().unix(),
|
|
125
|
-
headers: customHeaders,
|
|
123
|
+
headers: Object.assign({ creationTimestamp: moment_1.default().valueOf() }, customHeaders),
|
|
126
124
|
};
|
|
127
125
|
}
|
|
128
126
|
getConnection() {
|
|
@@ -239,10 +237,12 @@ class RabbitMq {
|
|
|
239
237
|
const lockTimeout = (options === null || options === void 0 ? void 0 : options.lockTimeout) || DEFAULT_LOCK_TIMEOUT;
|
|
240
238
|
logger_1.default.info(`Consuming with lock from queue ${queue} with lockTimeout: ${lockTimeout}ms`);
|
|
241
239
|
return this.consumeFromRabbit(queue, (msg, ack, nack) => __awaiter(this, void 0, void 0, function* () {
|
|
240
|
+
logger_1.default.info('Logs ');
|
|
242
241
|
if (!msg) {
|
|
243
242
|
return null;
|
|
244
243
|
}
|
|
245
|
-
const { properties: {
|
|
244
|
+
const { properties: { headers } } = msg;
|
|
245
|
+
const timestamp = headers === null || headers === void 0 ? void 0 : headers.creationTimestamp;
|
|
246
246
|
let releaseLock = null;
|
|
247
247
|
try {
|
|
248
248
|
if (timestamp && (headers === null || headers === void 0 ? void 0 : headers.redisTimestampValidationKey)) {
|
|
@@ -325,6 +325,7 @@ class RabbitMq {
|
|
|
325
325
|
}
|
|
326
326
|
sendToQueue(queue, content, options, customHeaders) {
|
|
327
327
|
return __awaiter(this, void 0, void 0, function* () {
|
|
328
|
+
logger_1.default.info('ss', RabbitMq.getPublishOptions(customHeaders));
|
|
328
329
|
return wrapSetImmediate(() => __awaiter(this, void 0, void 0, function* () {
|
|
329
330
|
RabbitMq.validateName('queue', queue);
|
|
330
331
|
const channel = yield this.assertChannel();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autofleet/rabbit",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"build": "tsc",
|
|
11
11
|
"linter": "./node_modules/.bin/eslint .",
|
|
12
12
|
"test": "jest --forceExit",
|
|
13
|
-
"test-local": "RABBITMQ_SERVICE_HOST=
|
|
13
|
+
"test-local": "RABBITMQ_SERVICE_HOST=localhost:5672 jest --forceExit",
|
|
14
14
|
"coverage": "jest --coverage --forceExit && rm -rf ./coverage",
|
|
15
15
|
"dev": "nodemon"
|
|
16
16
|
},
|
package/src/index.ts
CHANGED
|
@@ -101,7 +101,10 @@ class RabbitMq implements IAfRabbitMq {
|
|
|
101
101
|
static getPublishOptions(customHeaders: CustomMessageHeaders = {}) {
|
|
102
102
|
return {
|
|
103
103
|
timestamp: moment().unix(),
|
|
104
|
-
headers:
|
|
104
|
+
headers: {
|
|
105
|
+
creationTimestamp: moment().valueOf(),
|
|
106
|
+
...customHeaders,
|
|
107
|
+
},
|
|
105
108
|
};
|
|
106
109
|
}
|
|
107
110
|
|
|
@@ -147,7 +150,9 @@ class RabbitMq implements IAfRabbitMq {
|
|
|
147
150
|
|
|
148
151
|
private shouldConsumeMessageByTimestamp = async (msg: ConsumeMessageOrNull) => {
|
|
149
152
|
if (msg) {
|
|
150
|
-
const { properties: {
|
|
153
|
+
const { properties: { headers } } = msg;
|
|
154
|
+
const timestamp = headers?.creationTimestamp;
|
|
155
|
+
|
|
151
156
|
if (timestamp && headers?.redisTimestampValidationKey && this.redisClient) {
|
|
152
157
|
const lastMessageTimestamp = await this.redisClient.getAsync(headers.redisTimestampValidationKey);
|
|
153
158
|
return !lastMessageTimestamp || (parseInt(lastMessageTimestamp, 10) <= parseInt(timestamp, 10));
|
|
@@ -160,7 +165,9 @@ class RabbitMq implements IAfRabbitMq {
|
|
|
160
165
|
public ack = (channel: ChannelWrapper, msg: ConsumeMessageOrNull, shouldUpdateRedisTimestamp = false) => async (userMsg: ConsumeMessage) : Promise<any> => {
|
|
161
166
|
if (msg) {
|
|
162
167
|
await channel.ack(msg);
|
|
163
|
-
const { properties: {
|
|
168
|
+
const { properties: { headers } } = msg;
|
|
169
|
+
const timestamp = headers?.creationTimestamp;
|
|
170
|
+
|
|
164
171
|
if (shouldUpdateRedisTimestamp && timestamp && headers?.redisTimestampValidationKey && this.redisClient) {
|
|
165
172
|
const parsedTimestamp = parseInt(timestamp, 10);
|
|
166
173
|
await this.redisClient.setAsync(headers.redisTimestampValidationKey, parsedTimestamp, 'EX', 3600);
|
|
@@ -189,6 +196,7 @@ class RabbitMq implements IAfRabbitMq {
|
|
|
189
196
|
)
|
|
190
197
|
) {
|
|
191
198
|
await this.sendToQueue(queue, RabbitMq.parseMsg(msg).content, options, {
|
|
199
|
+
...msg.properties.headers,
|
|
192
200
|
[RETRY_HEADER]: msg.properties.headers[RETRY_HEADER]
|
|
193
201
|
? msg.properties.headers[RETRY_HEADER] + 1
|
|
194
202
|
: 1,
|
|
@@ -196,6 +204,7 @@ class RabbitMq implements IAfRabbitMq {
|
|
|
196
204
|
} else {
|
|
197
205
|
const deadQueue = `${queue}-dead`;
|
|
198
206
|
await this.sendToQueue(deadQueue, RabbitMq.parseMsg(msg).content, deadQueueOptions, {
|
|
207
|
+
...msg.properties.headers,
|
|
199
208
|
[RETRY_HEADER]: msg.properties.headers[RETRY_HEADER]
|
|
200
209
|
? msg.properties.headers[RETRY_HEADER] + 1
|
|
201
210
|
: 1,
|
|
@@ -309,11 +318,14 @@ class RabbitMq implements IAfRabbitMq {
|
|
|
309
318
|
const lockTimeout = options?.lockTimeout || DEFAULT_LOCK_TIMEOUT;
|
|
310
319
|
logger.info(`Consuming with lock from queue ${queue} with lockTimeout: ${lockTimeout}ms`);
|
|
311
320
|
return this.consumeFromRabbit(queue, async (msg, ack, nack) => {
|
|
321
|
+
logger.info('Logs ');
|
|
312
322
|
if (!msg) {
|
|
313
323
|
return null;
|
|
314
324
|
}
|
|
315
|
-
const { properties: {
|
|
325
|
+
const { properties: { headers } } = msg;
|
|
326
|
+
const timestamp = headers?.creationTimestamp;
|
|
316
327
|
let releaseLock = null;
|
|
328
|
+
|
|
317
329
|
try {
|
|
318
330
|
if (timestamp && headers?.redisTimestampValidationKey) {
|
|
319
331
|
if (this.redisLock) {
|
|
@@ -406,6 +418,7 @@ class RabbitMq implements IAfRabbitMq {
|
|
|
406
418
|
}
|
|
407
419
|
|
|
408
420
|
async sendToQueue(queue: string, content: any, options?: any, customHeaders?: any) : Promise<boolean> {
|
|
421
|
+
logger.info('ss', RabbitMq.getPublishOptions(customHeaders));
|
|
409
422
|
return wrapSetImmediate(async () => {
|
|
410
423
|
RabbitMq.validateName('queue', queue);
|
|
411
424
|
const channel: ChannelWrapper = await this.assertChannel();
|