@autofleet/rabbit 3.2.14-beta.2 → 3.2.14-beta.4

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.
Files changed (3) hide show
  1. package/dist/index.js +40 -25
  2. package/package.json +4 -4
  3. package/src/index.ts +40 -25
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  "use strict";
2
+ /* eslint-disable no-empty,consistent-return,no-async-promise-executor,@typescript-eslint/no-unused-vars */
2
3
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
4
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
5
  };
@@ -216,29 +217,38 @@ class RabbitMq {
216
217
  }
217
218
  async getNewChannel({ name = utils_1.rand().toString(), onClose = null, options = {} } = {}) {
218
219
  return new Promise(async (resolve, reject) => {
219
- const connection = await this.getConnection();
220
- const channel = connection.createChannel({
220
+ let connection;
221
+ try {
222
+ connection = await this.getConnection();
223
+ }
224
+ catch (e) {
225
+ logger_1.default.error(`rabbit: error on get connection for new channel ${name} `, { e });
226
+ reject(e);
227
+ }
228
+ const channel = connection?.createChannel({
221
229
  ...options,
222
230
  });
223
231
  let isResolved = false;
224
- channel.on('error', (err) => {
225
- logger_1.default.error(`rabbit: channel error ${name} error`, { err });
226
- if (!isResolved) {
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`);
227
248
  isResolved = true;
228
- reject(err);
229
- }
230
- });
231
- channel.on('close', (...args) => {
232
- logger_1.default.error(`rabbit: channel ${name} closed`, { args });
233
- if (onClose) {
234
- onClose(args);
235
- }
236
- });
237
- channel.once('connect', () => {
238
- debug(`rabbit: channel ${name} CONNECTED`);
239
- isResolved = true;
240
- resolve(channel);
241
- });
249
+ resolve(channel);
250
+ });
251
+ }
242
252
  });
243
253
  }
244
254
  async assertChannel({ force = false } = {}) {
@@ -322,15 +332,18 @@ class RabbitMq {
322
332
  return queue;
323
333
  }
324
334
  saveConsumer(queue, callback, options) {
325
- this.consumers.push({
326
- queue,
327
- callback,
328
- options,
329
- });
335
+ const isConsumerExist = this.consumers.some((consumer) => consumer.queue === queue);
336
+ if (!isConsumerExist) {
337
+ logger_1.default.info(`rabbit: consumer: ${queue} saved in consumer array`);
338
+ this.consumers.push({
339
+ queue,
340
+ callback,
341
+ options,
342
+ });
343
+ }
330
344
  }
331
345
  async consume(queue, callback, options) {
332
346
  await this.consumeFromRabbit(queue, callback, options);
333
- return this.saveConsumer(queue, callback, options);
334
347
  }
335
348
  async lockRedisIfNeeded(msg, options) {
336
349
  const { properties: { headers } } = msg;
@@ -349,6 +362,7 @@ class RabbitMq {
349
362
  async consumeFromRabbit(queue, callback, options) {
350
363
  const optionsWithDefaults = { ...consts_1.DEFAULT_OPTIONS, ...options };
351
364
  RabbitMq.validateName('queue', queue);
365
+ this.saveConsumer(queue, callback, options);
352
366
  const uniqueId = uuid_1.v4();
353
367
  const { limit, deadMessageTtl, useConsumeWithLock, lockTimeout, auditContext, enableRabbitTrace, } = optionsWithDefaults;
354
368
  if (useConsumeWithLock) {
@@ -437,6 +451,7 @@ class RabbitMq {
437
451
  RabbitMq.validateName('exchange', exchange);
438
452
  RabbitMq.validateName('queue', queue);
439
453
  const { limit, deadMessageTtl } = optionsWithDefaults;
454
+ await this.saveConsumer(queue, callback, options);
440
455
  const channel = await this.getNewChannel({ name: `consume-exchange-${exchange}-queue-${queue}` });
441
456
  return channel.addSetup(async (c) => {
442
457
  const assertExchange = await utils_1.assertExchangeFanout(c, exchange);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/rabbit",
3
- "version": "3.2.14-beta.2",
3
+ "version": "3.2.14-beta.4",
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.6",
19
+ "@autofleet/zehut": "3.0.8",
20
20
  "@types/amqplib": "0.8.2",
21
21
  "@types/uuid": "^8.3.3",
22
22
  "amqp-connection-manager": "4.1.9",
@@ -28,7 +28,7 @@
28
28
  "uuid": "^8.3.2"
29
29
  },
30
30
  "peerDependencies": {
31
- "@autofleet/outbreak": "0.2.1"
31
+ "@autofleet/outbreak": "^1.0.4"
32
32
  },
33
33
  "devDependencies": {
34
34
  "@types/jest": "^26.0.23",
@@ -44,4 +44,4 @@
44
44
  },
45
45
  "author": "",
46
46
  "license": "ISC"
47
- }
47
+ }
package/src/index.ts CHANGED
@@ -1,3 +1,5 @@
1
+ /* eslint-disable no-empty,consistent-return,no-async-promise-executor,@typescript-eslint/no-unused-vars */
2
+
1
3
  import { EventEmitter } from 'events';
2
4
  import { promisify } from 'util';
3
5
  import moment from 'moment';
@@ -348,30 +350,38 @@ class RabbitMq implements IAfRabbitMq {
348
350
 
349
351
  async getNewChannel({ name = rand().toString(), onClose = null, options = {} }: newChannelOpts = {}) {
350
352
  return new Promise<ChannelWrapper>(async (resolve, reject) => {
351
- const connection: AmqpConnectionManager = await this.getConnection();
352
- const channel = connection.createChannel({
353
+ let connection!: AmqpConnectionManager;
354
+ try {
355
+ connection = await this.getConnection();
356
+ } catch (e) {
357
+ logger.error(`rabbit: error on get connection for new channel ${name} `, { e });
358
+ reject(e);
359
+ }
360
+ const channel = connection?.createChannel({
353
361
  ...options,
354
362
  });
355
363
 
356
364
  let isResolved = false;
357
- channel.on('error', (err) => {
358
- logger.error(`rabbit: channel error ${name} error`, { err });
359
- if (!isResolved) {
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`);
360
381
  isResolved = true;
361
- reject(err);
362
- }
363
- });
364
- channel.on('close', (...args) => {
365
- logger.error(`rabbit: channel ${name} closed`, { args });
366
- if (onClose) {
367
- onClose(args);
368
- }
369
- });
370
- channel.once('connect', () => {
371
- debug(`rabbit: channel ${name} CONNECTED`);
372
- isResolved = true;
373
- resolve(channel);
374
- });
382
+ resolve(channel);
383
+ });
384
+ }
375
385
  });
376
386
  }
377
387
 
@@ -462,16 +472,19 @@ class RabbitMq implements IAfRabbitMq {
462
472
  }
463
473
 
464
474
  private saveConsumer(queue: string, callback: CallbackFunction, options: ConsumeOptions | undefined) {
465
- this.consumers.push({
466
- queue,
467
- callback,
468
- options,
469
- });
475
+ const isConsumerExist :boolean = this.consumers.some((consumer) => consumer.queue === queue);
476
+ if (!isConsumerExist) {
477
+ logger.info(`rabbit: consumer: ${queue} saved in consumer array`);
478
+ this.consumers.push({
479
+ queue,
480
+ callback,
481
+ options,
482
+ });
483
+ }
470
484
  }
471
485
 
472
486
  async consume(queue: string, callback: CallbackFunction, options?: ConsumeOptions): Promise<any> {
473
487
  await this.consumeFromRabbit(queue, callback, options);
474
- return this.saveConsumer(queue, callback, options);
475
488
  }
476
489
 
477
490
  private async lockRedisIfNeeded(msg: any, options: any) {
@@ -497,6 +510,7 @@ class RabbitMq implements IAfRabbitMq {
497
510
  private async consumeFromRabbit(queue: string, callback: CallbackFunction, options?: ConsumeOptions): Promise<any> {
498
511
  const optionsWithDefaults = { ...DEFAULT_OPTIONS, ...options };
499
512
  RabbitMq.validateName('queue', queue);
513
+ this.saveConsumer(queue, callback, options);
500
514
  const uniqueId = v4();
501
515
  const {
502
516
  limit, deadMessageTtl, useConsumeWithLock, lockTimeout, auditContext, enableRabbitTrace,
@@ -599,6 +613,7 @@ class RabbitMq implements IAfRabbitMq {
599
613
  RabbitMq.validateName('exchange', exchange);
600
614
  RabbitMq.validateName('queue', queue);
601
615
  const { limit, deadMessageTtl } = optionsWithDefaults;
616
+ await this.saveConsumer(queue, callback, options);
602
617
  const channel: ChannelWrapper = await this.getNewChannel({ name: `consume-exchange-${exchange}-queue-${queue}` });
603
618
 
604
619
  return channel.addSetup(async (c: ConfirmChannel) => {