@autofleet/rabbit 3.2.13-beta → 3.2.13-beta.10
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.js +36 -26
- package/package.json +2 -2
- package/src/index.ts +35 -27
package/dist/index.js
CHANGED
|
@@ -25,6 +25,7 @@ class RabbitMq {
|
|
|
25
25
|
constructor(options, redisConfig) {
|
|
26
26
|
this.DISCONNECT_MSG = 'rabbit: connection disconnect';
|
|
27
27
|
this.RECONNECT_MSG = 'rabbit: connection disconnect - reconnecting';
|
|
28
|
+
this.consumers = [];
|
|
28
29
|
this.shouldConsumeMessageByTimestamp = async (msg) => {
|
|
29
30
|
if (msg) {
|
|
30
31
|
const { properties: { headers } } = msg;
|
|
@@ -87,7 +88,7 @@ class RabbitMq {
|
|
|
87
88
|
this.creatingConnection = false;
|
|
88
89
|
this.exchanges = {};
|
|
89
90
|
this.queues = {};
|
|
90
|
-
this.consumers =
|
|
91
|
+
this.consumers = [];
|
|
91
92
|
this.options = options;
|
|
92
93
|
this.redisClient = redisConfig && redis_1.default(redisConfig);
|
|
93
94
|
if (this.redisClient) {
|
|
@@ -224,28 +225,33 @@ class RabbitMq {
|
|
|
224
225
|
logger_1.default.error(`rabbit: error on get connection for new channel ${name} `, { e });
|
|
225
226
|
reject(e);
|
|
226
227
|
}
|
|
227
|
-
const channel = connection
|
|
228
|
+
const channel = connection?.createChannel({
|
|
228
229
|
...options,
|
|
229
230
|
});
|
|
230
231
|
let isResolved = false;
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
232
|
+
if (channel) {
|
|
233
|
+
channel.on('error', (err) => {
|
|
234
|
+
logger_1.default.error(`rabbit: channel error ${name} error`, { err });
|
|
235
|
+
if (!isResolved) {
|
|
236
|
+
isResolved = true;
|
|
237
|
+
reject(err);
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
channel.on('close', (...args) => {
|
|
241
|
+
logger_1.default.error(`rabbit: channel ${name} closed`, { args });
|
|
242
|
+
if (onClose) {
|
|
243
|
+
onClose(args);
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
channel.once('connect', () => {
|
|
247
|
+
debug(`rabbit: channel ${name} CONNECTED`);
|
|
234
248
|
isResolved = true;
|
|
235
|
-
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
onClose(args);
|
|
242
|
-
}
|
|
243
|
-
});
|
|
244
|
-
channel.once('connect', () => {
|
|
245
|
-
debug(`rabbit: channel ${name} CONNECTED`);
|
|
246
|
-
isResolved = true;
|
|
247
|
-
resolve(channel);
|
|
248
|
-
});
|
|
249
|
+
resolve(channel);
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
else {
|
|
253
|
+
debug(`rabbit: channel ${name} not connected and therefore no listener was registered`);
|
|
254
|
+
}
|
|
249
255
|
});
|
|
250
256
|
}
|
|
251
257
|
async assertChannel({ force = false } = {}) {
|
|
@@ -329,15 +335,18 @@ class RabbitMq {
|
|
|
329
335
|
return queue;
|
|
330
336
|
}
|
|
331
337
|
saveConsumer(queue, callback, options) {
|
|
332
|
-
this.consumers.
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
338
|
+
const isConsumerExist = this.consumers.some((consumer) => consumer.queue === queue);
|
|
339
|
+
if (!isConsumerExist) {
|
|
340
|
+
logger_1.default.info(`rabbit: consumer: ${queue} saved in consumer array`);
|
|
341
|
+
this.consumers.push({
|
|
342
|
+
queue,
|
|
343
|
+
callback,
|
|
344
|
+
options,
|
|
345
|
+
});
|
|
346
|
+
}
|
|
337
347
|
}
|
|
338
348
|
async consume(queue, callback, options) {
|
|
339
349
|
await this.consumeFromRabbit(queue, callback, options);
|
|
340
|
-
return this.saveConsumer(queue, callback, options);
|
|
341
350
|
}
|
|
342
351
|
async lockRedisIfNeeded(msg, options) {
|
|
343
352
|
const { properties: { headers } } = msg;
|
|
@@ -356,6 +365,7 @@ class RabbitMq {
|
|
|
356
365
|
async consumeFromRabbit(queue, callback, options) {
|
|
357
366
|
const optionsWithDefaults = { ...consts_1.DEFAULT_OPTIONS, ...options };
|
|
358
367
|
RabbitMq.validateName('queue', queue);
|
|
368
|
+
this.saveConsumer(queue, callback, options);
|
|
359
369
|
const uniqueId = uuid_1.v4();
|
|
360
370
|
const { limit, deadMessageTtl, useConsumeWithLock, lockTimeout, auditContext, enableRabbitTrace, } = optionsWithDefaults;
|
|
361
371
|
if (useConsumeWithLock) {
|
|
@@ -496,7 +506,7 @@ class RabbitMq {
|
|
|
496
506
|
try {
|
|
497
507
|
await Promise.all([
|
|
498
508
|
channel.waitForConnect(),
|
|
499
|
-
...
|
|
509
|
+
...this.consumers.map((c) => channel.checkQueue(c.queue)),
|
|
500
510
|
]);
|
|
501
511
|
}
|
|
502
512
|
catch (e) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autofleet/rabbit",
|
|
3
|
-
"version": "3.2.13-beta",
|
|
3
|
+
"version": "3.2.13-beta.10",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
18
18
|
"@autofleet/logger": "^1.2.8",
|
|
19
|
-
"@autofleet/zehut": "3.0.
|
|
19
|
+
"@autofleet/zehut": "^3.0.6",
|
|
20
20
|
"@types/amqplib": "0.8.2",
|
|
21
21
|
"@types/uuid": "^8.3.3",
|
|
22
22
|
"amqp-connection-manager": "4.1.9",
|
package/src/index.ts
CHANGED
|
@@ -162,7 +162,7 @@ class RabbitMq implements IAfRabbitMq {
|
|
|
162
162
|
/** Array of consumers tags used for canceling consumption */
|
|
163
163
|
consumersTags: Array<[ConfirmChannel, string]>;
|
|
164
164
|
|
|
165
|
-
private consumers:
|
|
165
|
+
private consumers: Array<AfConsumer> = [];
|
|
166
166
|
|
|
167
167
|
constructor(options?: AfRabbitOptions, redisConfig?: RedisConfig) {
|
|
168
168
|
this.em = new EventEmitter();
|
|
@@ -171,7 +171,7 @@ class RabbitMq implements IAfRabbitMq {
|
|
|
171
171
|
this.creatingConnection = false;
|
|
172
172
|
this.exchanges = {};
|
|
173
173
|
this.queues = {};
|
|
174
|
-
this.consumers =
|
|
174
|
+
this.consumers = [];
|
|
175
175
|
this.options = options;
|
|
176
176
|
this.redisClient = redisConfig && getRedisInstance(redisConfig);
|
|
177
177
|
if (this.redisClient) {
|
|
@@ -357,29 +357,33 @@ class RabbitMq implements IAfRabbitMq {
|
|
|
357
357
|
logger.error(`rabbit: error on get connection for new channel ${name} `, { e });
|
|
358
358
|
reject(e);
|
|
359
359
|
}
|
|
360
|
-
const channel = connection
|
|
360
|
+
const channel = connection?.createChannel({
|
|
361
361
|
...options,
|
|
362
362
|
});
|
|
363
363
|
|
|
364
364
|
let isResolved = false;
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
365
|
+
if (channel) {
|
|
366
|
+
channel.on('error', (err) => {
|
|
367
|
+
logger.error(`rabbit: channel error ${name} error`, { err });
|
|
368
|
+
if (!isResolved) {
|
|
369
|
+
isResolved = true;
|
|
370
|
+
reject(err);
|
|
371
|
+
}
|
|
372
|
+
});
|
|
373
|
+
channel.on('close', (...args) => {
|
|
374
|
+
logger.error(`rabbit: channel ${name} closed`, { args });
|
|
375
|
+
if (onClose) {
|
|
376
|
+
onClose(args);
|
|
377
|
+
}
|
|
378
|
+
});
|
|
379
|
+
channel.once('connect', () => {
|
|
380
|
+
debug(`rabbit: channel ${name} CONNECTED`);
|
|
368
381
|
isResolved = true;
|
|
369
|
-
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
if (onClose) {
|
|
375
|
-
onClose(args);
|
|
376
|
-
}
|
|
377
|
-
});
|
|
378
|
-
channel.once('connect', () => {
|
|
379
|
-
debug(`rabbit: channel ${name} CONNECTED`);
|
|
380
|
-
isResolved = true;
|
|
381
|
-
resolve(channel);
|
|
382
|
-
});
|
|
382
|
+
resolve(channel);
|
|
383
|
+
});
|
|
384
|
+
} else {
|
|
385
|
+
debug(`rabbit: channel ${name} not connected and therefore no listener was registered`);
|
|
386
|
+
}
|
|
383
387
|
});
|
|
384
388
|
}
|
|
385
389
|
|
|
@@ -470,16 +474,19 @@ class RabbitMq implements IAfRabbitMq {
|
|
|
470
474
|
}
|
|
471
475
|
|
|
472
476
|
private saveConsumer(queue: string, callback: CallbackFunction, options: ConsumeOptions | undefined) {
|
|
473
|
-
this.consumers.
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
477
|
+
const isConsumerExist :boolean = this.consumers.some((consumer) => consumer.queue === queue);
|
|
478
|
+
if (!isConsumerExist) {
|
|
479
|
+
logger.info(`rabbit: consumer: ${queue} saved in consumer array`);
|
|
480
|
+
this.consumers.push({
|
|
481
|
+
queue,
|
|
482
|
+
callback,
|
|
483
|
+
options,
|
|
484
|
+
});
|
|
485
|
+
}
|
|
478
486
|
}
|
|
479
487
|
|
|
480
488
|
async consume(queue: string, callback: CallbackFunction, options?: ConsumeOptions): Promise<any> {
|
|
481
489
|
await this.consumeFromRabbit(queue, callback, options);
|
|
482
|
-
return this.saveConsumer(queue, callback, options);
|
|
483
490
|
}
|
|
484
491
|
|
|
485
492
|
private async lockRedisIfNeeded(msg: any, options: any) {
|
|
@@ -505,6 +512,7 @@ class RabbitMq implements IAfRabbitMq {
|
|
|
505
512
|
private async consumeFromRabbit(queue: string, callback: CallbackFunction, options?: ConsumeOptions): Promise<any> {
|
|
506
513
|
const optionsWithDefaults = { ...DEFAULT_OPTIONS, ...options };
|
|
507
514
|
RabbitMq.validateName('queue', queue);
|
|
515
|
+
this.saveConsumer(queue, callback, options);
|
|
508
516
|
const uniqueId = v4();
|
|
509
517
|
const {
|
|
510
518
|
limit, deadMessageTtl, useConsumeWithLock, lockTimeout, auditContext, enableRabbitTrace,
|
|
@@ -676,7 +684,7 @@ class RabbitMq implements IAfRabbitMq {
|
|
|
676
684
|
try {
|
|
677
685
|
await Promise.all([
|
|
678
686
|
channel.waitForConnect(),
|
|
679
|
-
...
|
|
687
|
+
...this.consumers.map((c: AfConsumer) => channel.checkQueue(c.queue)),
|
|
680
688
|
]);
|
|
681
689
|
} catch (e) {
|
|
682
690
|
logger.error('rabbit: isConnected - false');
|