@autofleet/rabbit 3.2.11 → 3.2.13-beta

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 CHANGED
@@ -25,7 +25,6 @@ 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 = [];
29
28
  this.shouldConsumeMessageByTimestamp = async (msg) => {
30
29
  if (msg) {
31
30
  const { properties: { headers } } = msg;
@@ -88,7 +87,7 @@ class RabbitMq {
88
87
  this.creatingConnection = false;
89
88
  this.exchanges = {};
90
89
  this.queues = {};
91
- this.consumers = [];
90
+ this.consumers = new Set();
92
91
  this.options = options;
93
92
  this.redisClient = redisConfig && redis_1.default(redisConfig);
94
93
  if (this.redisClient) {
@@ -217,7 +216,14 @@ class RabbitMq {
217
216
  }
218
217
  async getNewChannel({ name = utils_1.rand().toString(), onClose = null, options = {} } = {}) {
219
218
  return new Promise(async (resolve, reject) => {
220
- const connection = await this.getConnection();
219
+ let connection;
220
+ try {
221
+ connection = await this.getConnection();
222
+ }
223
+ catch (e) {
224
+ logger_1.default.error(`rabbit: error on get connection for new channel ${name} `, { e });
225
+ reject(e);
226
+ }
221
227
  const channel = connection.createChannel({
222
228
  ...options,
223
229
  });
@@ -323,7 +329,7 @@ class RabbitMq {
323
329
  return queue;
324
330
  }
325
331
  saveConsumer(queue, callback, options) {
326
- this.consumers.push({
332
+ this.consumers.add({
327
333
  queue,
328
334
  callback,
329
335
  options,
@@ -438,6 +444,7 @@ class RabbitMq {
438
444
  RabbitMq.validateName('exchange', exchange);
439
445
  RabbitMq.validateName('queue', queue);
440
446
  const { limit, deadMessageTtl } = optionsWithDefaults;
447
+ await this.saveConsumer(queue, callback, options);
441
448
  const channel = await this.getNewChannel({ name: `consume-exchange-${exchange}-queue-${queue}` });
442
449
  return channel.addSetup(async (c) => {
443
450
  const assertExchange = await utils_1.assertExchangeFanout(c, exchange);
@@ -489,7 +496,7 @@ class RabbitMq {
489
496
  try {
490
497
  await Promise.all([
491
498
  channel.waitForConnect(),
492
- ...this.consumers.map((c) => channel.checkQueue(c.queue)),
499
+ ...Array.from(this.consumers, (c) => channel.checkQueue(c.queue)),
493
500
  ]);
494
501
  }
495
502
  catch (e) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/rabbit",
3
- "version": "3.2.11",
3
+ "version": "3.2.13-beta",
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.7",
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: Array<AfConsumer> = [];
165
+ private consumers: Set<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 = new Set();
175
175
  this.options = options;
176
176
  this.redisClient = redisConfig && getRedisInstance(redisConfig);
177
177
  if (this.redisClient) {
@@ -350,7 +350,13 @@ class RabbitMq implements IAfRabbitMq {
350
350
 
351
351
  async getNewChannel({ name = rand().toString(), onClose = null, options = {} }: newChannelOpts = {}) {
352
352
  return new Promise<ChannelWrapper>(async (resolve, reject) => {
353
- const connection: AmqpConnectionManager = await this.getConnection();
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
+ }
354
360
  const channel = connection.createChannel({
355
361
  ...options,
356
362
  });
@@ -464,7 +470,7 @@ class RabbitMq implements IAfRabbitMq {
464
470
  }
465
471
 
466
472
  private saveConsumer(queue: string, callback: CallbackFunction, options: ConsumeOptions | undefined) {
467
- this.consumers.push({
473
+ this.consumers.add({
468
474
  queue,
469
475
  callback,
470
476
  options,
@@ -601,6 +607,7 @@ class RabbitMq implements IAfRabbitMq {
601
607
  RabbitMq.validateName('exchange', exchange);
602
608
  RabbitMq.validateName('queue', queue);
603
609
  const { limit, deadMessageTtl } = optionsWithDefaults;
610
+ await this.saveConsumer(queue, callback, options);
604
611
  const channel: ChannelWrapper = await this.getNewChannel({ name: `consume-exchange-${exchange}-queue-${queue}` });
605
612
 
606
613
  return channel.addSetup(async (c: ConfirmChannel) => {
@@ -669,7 +676,7 @@ class RabbitMq implements IAfRabbitMq {
669
676
  try {
670
677
  await Promise.all([
671
678
  channel.waitForConnect(),
672
- ...this.consumers.map((c: AfConsumer) => channel.checkQueue(c.queue)),
679
+ ...Array.from(this.consumers, (c) => channel.checkQueue(c.queue)),
673
680
  ]);
674
681
  } catch (e) {
675
682
  logger.error('rabbit: isConnected - false');