@autofleet/rabbit 2.2.8 → 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 +14 -12
- package/package.json +2 -2
- package/src/index.ts +18 -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() {
|
|
@@ -136,6 +134,7 @@ class RabbitMq {
|
|
|
136
134
|
return;
|
|
137
135
|
}
|
|
138
136
|
this.creatingConnection = true;
|
|
137
|
+
logger_1.default.info('rabbit: env username', { userName: (process.env.RABBITMQ_USERNAME || 'doesnt exist') });
|
|
139
138
|
const username = process.env.RABBITMQ_USERNAME || 'guest';
|
|
140
139
|
const password = process.env.RABBITMQ_PASSWORD || 'guest';
|
|
141
140
|
const host = process.env.RABBITMQ_SERVICE_HOST || '';
|
|
@@ -238,10 +237,12 @@ class RabbitMq {
|
|
|
238
237
|
const lockTimeout = (options === null || options === void 0 ? void 0 : options.lockTimeout) || DEFAULT_LOCK_TIMEOUT;
|
|
239
238
|
logger_1.default.info(`Consuming with lock from queue ${queue} with lockTimeout: ${lockTimeout}ms`);
|
|
240
239
|
return this.consumeFromRabbit(queue, (msg, ack, nack) => __awaiter(this, void 0, void 0, function* () {
|
|
240
|
+
logger_1.default.info('Logs ');
|
|
241
241
|
if (!msg) {
|
|
242
242
|
return null;
|
|
243
243
|
}
|
|
244
|
-
const { properties: {
|
|
244
|
+
const { properties: { headers } } = msg;
|
|
245
|
+
const timestamp = headers === null || headers === void 0 ? void 0 : headers.creationTimestamp;
|
|
245
246
|
let releaseLock = null;
|
|
246
247
|
try {
|
|
247
248
|
if (timestamp && (headers === null || headers === void 0 ? void 0 : headers.redisTimestampValidationKey)) {
|
|
@@ -324,6 +325,7 @@ class RabbitMq {
|
|
|
324
325
|
}
|
|
325
326
|
sendToQueue(queue, content, options, customHeaders) {
|
|
326
327
|
return __awaiter(this, void 0, void 0, function* () {
|
|
328
|
+
logger_1.default.info('ss', RabbitMq.getPublishOptions(customHeaders));
|
|
327
329
|
return wrapSetImmediate(() => __awaiter(this, void 0, void 0, function* () {
|
|
328
330
|
RabbitMq.validateName('queue', queue);
|
|
329
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,
|
|
@@ -215,6 +224,7 @@ class RabbitMq implements IAfRabbitMq {
|
|
|
215
224
|
return;
|
|
216
225
|
}
|
|
217
226
|
this.creatingConnection = true;
|
|
227
|
+
logger.info('rabbit: env username', { userName: (process.env.RABBITMQ_USERNAME || 'doesnt exist') });
|
|
218
228
|
const username: string = process.env.RABBITMQ_USERNAME || 'guest';
|
|
219
229
|
const password: string = process.env.RABBITMQ_PASSWORD || 'guest';
|
|
220
230
|
const host: string = process.env.RABBITMQ_SERVICE_HOST || '';
|
|
@@ -308,11 +318,14 @@ class RabbitMq implements IAfRabbitMq {
|
|
|
308
318
|
const lockTimeout = options?.lockTimeout || DEFAULT_LOCK_TIMEOUT;
|
|
309
319
|
logger.info(`Consuming with lock from queue ${queue} with lockTimeout: ${lockTimeout}ms`);
|
|
310
320
|
return this.consumeFromRabbit(queue, async (msg, ack, nack) => {
|
|
321
|
+
logger.info('Logs ');
|
|
311
322
|
if (!msg) {
|
|
312
323
|
return null;
|
|
313
324
|
}
|
|
314
|
-
const { properties: {
|
|
325
|
+
const { properties: { headers } } = msg;
|
|
326
|
+
const timestamp = headers?.creationTimestamp;
|
|
315
327
|
let releaseLock = null;
|
|
328
|
+
|
|
316
329
|
try {
|
|
317
330
|
if (timestamp && headers?.redisTimestampValidationKey) {
|
|
318
331
|
if (this.redisLock) {
|
|
@@ -405,6 +418,7 @@ class RabbitMq implements IAfRabbitMq {
|
|
|
405
418
|
}
|
|
406
419
|
|
|
407
420
|
async sendToQueue(queue: string, content: any, options?: any, customHeaders?: any) : Promise<boolean> {
|
|
421
|
+
logger.info('ss', RabbitMq.getPublishOptions(customHeaders));
|
|
408
422
|
return wrapSetImmediate(async () => {
|
|
409
423
|
RabbitMq.validateName('queue', queue);
|
|
410
424
|
const channel: ChannelWrapper = await this.assertChannel();
|