@autofleet/rabbit 3.2.13 → 3.2.14-beta.1

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 (2) hide show
  1. package/package.json +2 -2
  2. package/src/index.ts +25 -40
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/rabbit",
3
- "version": "3.2.13",
3
+ "version": "3.2.14-beta.1",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -44,4 +44,4 @@
44
44
  },
45
45
  "author": "",
46
46
  "license": "ISC"
47
- }
47
+ }
package/src/index.ts CHANGED
@@ -1,5 +1,3 @@
1
- /* eslint-disable no-empty,consistent-return,no-async-promise-executor,@typescript-eslint/no-unused-vars */
2
-
3
1
  import { EventEmitter } from 'events';
4
2
  import { promisify } from 'util';
5
3
  import moment from 'moment';
@@ -350,38 +348,30 @@ class RabbitMq implements IAfRabbitMq {
350
348
 
351
349
  async getNewChannel({ name = rand().toString(), onClose = null, options = {} }: newChannelOpts = {}) {
352
350
  return new Promise<ChannelWrapper>(async (resolve, reject) => {
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({
351
+ const connection: AmqpConnectionManager = await this.getConnection();
352
+ const channel = connection.createChannel({
361
353
  ...options,
362
354
  });
363
355
 
364
356
  let isResolved = false;
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`);
357
+ channel.on('error', (err) => {
358
+ logger.error(`rabbit: channel error ${name} error`, { err });
359
+ if (!isResolved) {
381
360
  isResolved = true;
382
- resolve(channel);
383
- });
384
- }
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
+ });
385
375
  });
386
376
  }
387
377
 
@@ -472,19 +462,16 @@ class RabbitMq implements IAfRabbitMq {
472
462
  }
473
463
 
474
464
  private saveConsumer(queue: string, callback: CallbackFunction, options: ConsumeOptions | undefined) {
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
- }
465
+ this.consumers.push({
466
+ queue,
467
+ callback,
468
+ options,
469
+ });
484
470
  }
485
471
 
486
472
  async consume(queue: string, callback: CallbackFunction, options?: ConsumeOptions): Promise<any> {
487
473
  await this.consumeFromRabbit(queue, callback, options);
474
+ return this.saveConsumer(queue, callback, options);
488
475
  }
489
476
 
490
477
  private async lockRedisIfNeeded(msg: any, options: any) {
@@ -510,7 +497,6 @@ class RabbitMq implements IAfRabbitMq {
510
497
  private async consumeFromRabbit(queue: string, callback: CallbackFunction, options?: ConsumeOptions): Promise<any> {
511
498
  const optionsWithDefaults = { ...DEFAULT_OPTIONS, ...options };
512
499
  RabbitMq.validateName('queue', queue);
513
- this.saveConsumer(queue, callback, options);
514
500
  const uniqueId = v4();
515
501
  const {
516
502
  limit, deadMessageTtl, useConsumeWithLock, lockTimeout, auditContext, enableRabbitTrace,
@@ -613,7 +599,6 @@ class RabbitMq implements IAfRabbitMq {
613
599
  RabbitMq.validateName('exchange', exchange);
614
600
  RabbitMq.validateName('queue', queue);
615
601
  const { limit, deadMessageTtl } = optionsWithDefaults;
616
- await this.saveConsumer(queue, callback, options);
617
602
  const channel: ChannelWrapper = await this.getNewChannel({ name: `consume-exchange-${exchange}-queue-${queue}` });
618
603
 
619
604
  return channel.addSetup(async (c: ConfirmChannel) => {