@autofleet/rabbit 3.0.0-alpha → 3.0.1-alpha

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
@@ -143,13 +143,13 @@ class RabbitMq {
143
143
  return resolve();
144
144
  }
145
145
  if (this.connection !== null) {
146
- debug('rabbit: connection already exist');
147
146
  if (this.options?.disableReconnect || this.connection?.isConnected()) {
147
+ debug('rabbit: connection - is connected');
148
148
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
149
149
  // @ts-ignore
150
150
  return resolve(this.connection);
151
151
  }
152
- debug('rabbit: connection already exist - reconnecting');
152
+ debug('rabbit: connection - reconnecting');
153
153
  }
154
154
  if (this.creatingConnection) {
155
155
  debug('rabbit: creating connection emi');
@@ -183,7 +183,7 @@ class RabbitMq {
183
183
  });
184
184
  });
185
185
  }
186
- async getNewChannel({ name = '', onClose = null } = {}) {
186
+ async getNewChannel({ name = utils_1.rand().toString(), onClose = null } = {}) {
187
187
  return new Promise(async (resolve, reject) => {
188
188
  const connection = await this.getConnection();
189
189
  const channel = connection.createChannel({});
@@ -319,14 +319,14 @@ class RabbitMq {
319
319
  async consumeFromRabbit(queue, callback, options) {
320
320
  const optionsWithDefaults = { ...consts_1.DEFAULT_OPTIONS, ...options };
321
321
  RabbitMq.validateName('queue', queue);
322
- const { limit, deadMessageTtl, useConsumeWithLock, lockTimeout, } = optionsWithDefaults;
322
+ const { limit, deadMessageTtl, useConsumeWithLock, lockTimeout, auditContext, } = optionsWithDefaults;
323
323
  if (useConsumeWithLock) {
324
324
  if (!this.redisLock) {
325
325
  throw new Error('Usage of consumeWithLock requires RedisInstance');
326
326
  }
327
327
  logger_1.default.info(`rabbit: Consuming with lock from queue ${queue} with lockTimeout: ${lockTimeout}ms`);
328
328
  }
329
- const channel = await this.getNewChannel({ name: `consume-${queue}` });
329
+ const channel = await this.getNewChannel({ name: `consume-queue-${queue}` });
330
330
  return channel.addSetup(async (c) => {
331
331
  await c.assertQueue(queue, options ? { messageTtl: options.messageTtl } : undefined);
332
332
  await c.prefetch(limit, true);
@@ -339,6 +339,9 @@ class RabbitMq {
339
339
  const trace = outbreak_1.newTrace(outbreak_1.traceTypes.RABBIT);
340
340
  trace?.context?.set(consts_1.TRACING_HEADER, traceId);
341
341
  }
342
+ if (auditContext) {
343
+ auditContext(queue);
344
+ }
342
345
  const parsedMessage = RabbitMq.parseMsg(msg);
343
346
  const releaseLock = await this.lockRedisIfNeeded(parsedMessage, optionsWithDefaults);
344
347
  const shouldConsume = await this.shouldConsumeMessageByTimestamp(parsedMessage);
@@ -367,7 +370,7 @@ class RabbitMq {
367
370
  RabbitMq.validateName('exchange', exchange);
368
371
  RabbitMq.validateName('queue', queue);
369
372
  const { limit, deadMessageTtl } = optionsWithDefaults;
370
- const channel = await this.getNewChannel();
373
+ const channel = await this.getNewChannel({ name: `consume-exchange-${exchange}-queue-${queue}` });
371
374
  return channel.addSetup(async (c) => {
372
375
  const assertExchange = await utils_1.assertExchangeFanout(c, exchange);
373
376
  await c.assertQueue(queue);
@@ -10,4 +10,5 @@ export declare const DEFAULT_OPTIONS: {
10
10
  deadMessageTtl: number;
11
11
  lockTimeout: number;
12
12
  useConsumeWithLock: boolean;
13
+ auditContext: null;
13
14
  };
@@ -13,4 +13,5 @@ exports.DEFAULT_OPTIONS = {
13
13
  deadMessageTtl: exports.DEFAULT_DEAD_TTL_TWO_DAYS,
14
14
  lockTimeout: exports.DEFAULT_LOCK_TIMEOUT,
15
15
  useConsumeWithLock: exports.DEFAULT_USE_CONSUME_WITH_LOCK,
16
+ auditContext: null,
16
17
  };
@@ -17,6 +17,7 @@ export interface ConsumeOptions {
17
17
  limit?: number;
18
18
  lockTimeout?: number;
19
19
  useConsumeWithLock?: boolean;
20
+ auditContext?: any;
20
21
  }
21
22
  export declare type CallbackFunction = (msg: ConsumeMessage, ack: any, nack: any) => Promise<any>;
22
23
  export declare type newChannelOpts = {
@@ -2,3 +2,4 @@ import { ChannelWrapper } from 'amqp-connection-manager';
2
2
  import { ConfirmChannel } from 'amqplib';
3
3
  export declare const assertExchangeFanout: (c: ChannelWrapper | ConfirmChannel, exchangeName: string) => Promise<import("amqplib").Replies.AssertExchange>;
4
4
  export declare const wrapSetImmediate: (callback: () => any) => Promise<any>;
5
+ export declare const rand: () => number;
package/dist/lib/utils.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.wrapSetImmediate = exports.assertExchangeFanout = void 0;
3
+ exports.rand = exports.wrapSetImmediate = exports.assertExchangeFanout = void 0;
4
4
  exports.assertExchangeFanout = async (c, exchangeName) => c.assertExchange(exchangeName, 'fanout');
5
5
  exports.wrapSetImmediate = (callback) => new Promise((resolve, reject) => {
6
6
  setImmediate(async () => {
@@ -13,3 +13,4 @@ exports.wrapSetImmediate = (callback) => new Promise((resolve, reject) => {
13
13
  }
14
14
  });
15
15
  });
16
+ exports.rand = () => Math.floor(Math.random() * 100000);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/rabbit",
3
- "version": "3.0.0-alpha",
3
+ "version": "3.0.1-alpha",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
package/src/index.ts CHANGED
@@ -12,7 +12,7 @@ import { getCurrentContext, newTrace, traceTypes } from '@autofleet/outbreak';
12
12
  import logger from './logger';
13
13
  import RabbitError from './lib/rabbitError';
14
14
  import getRedisInstance, { RedisConfig } from './lib/redis';
15
- import { assertExchangeFanout, wrapSetImmediate } from './lib/utils';
15
+ import { assertExchangeFanout, rand, wrapSetImmediate } from './lib/utils';
16
16
  import {
17
17
  CONNECTION_CREATED_CONST,
18
18
  DEFAULT_LOCK_TIMEOUT,
@@ -259,13 +259,13 @@ class RabbitMq implements IAfRabbitMq {
259
259
  return resolve();
260
260
  }
261
261
  if (this.connection !== null) {
262
- debug('rabbit: connection already exist');
263
262
  if (this.options?.disableReconnect || this.connection?.isConnected()) {
263
+ debug('rabbit: connection - is connected');
264
264
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
265
265
  // @ts-ignore
266
266
  return resolve(this.connection);
267
267
  }
268
- debug('rabbit: connection already exist - reconnecting');
268
+ debug('rabbit: connection - reconnecting');
269
269
  }
270
270
  if (this.creatingConnection) {
271
271
  debug('rabbit: creating connection emi');
@@ -299,7 +299,7 @@ class RabbitMq implements IAfRabbitMq {
299
299
  });
300
300
  }
301
301
 
302
- async getNewChannel({ name = '', onClose = null }: newChannelOpts = {}) {
302
+ async getNewChannel({ name = rand().toString(), onClose = null }: newChannelOpts = {}) {
303
303
  return new Promise<ChannelWrapper>(async (resolve, reject) => {
304
304
  const connection: AmqpConnectionManager = await this.getConnection();
305
305
  const channel = connection.createChannel({});
@@ -455,7 +455,7 @@ class RabbitMq implements IAfRabbitMq {
455
455
  const optionsWithDefaults = { ...DEFAULT_OPTIONS, ...options };
456
456
  RabbitMq.validateName('queue', queue);
457
457
  const {
458
- limit, deadMessageTtl, useConsumeWithLock, lockTimeout,
458
+ limit, deadMessageTtl, useConsumeWithLock, lockTimeout, auditContext,
459
459
  } = optionsWithDefaults;
460
460
  if (useConsumeWithLock) {
461
461
  if (!this.redisLock) {
@@ -463,7 +463,7 @@ class RabbitMq implements IAfRabbitMq {
463
463
  }
464
464
  logger.info(`rabbit: Consuming with lock from queue ${queue} with lockTimeout: ${lockTimeout}ms`);
465
465
  }
466
- const channel: ChannelWrapper = await this.getNewChannel({ name: `consume-${queue}` });
466
+ const channel: ChannelWrapper = await this.getNewChannel({ name: `consume-queue-${queue}` });
467
467
  return channel.addSetup(async (c: ConfirmChannel) => {
468
468
  await c.assertQueue(queue, options ? { messageTtl: options.messageTtl } : undefined);
469
469
  await c.prefetch(limit, true);
@@ -480,6 +480,10 @@ class RabbitMq implements IAfRabbitMq {
480
480
  (trace as any)?.context?.set(TRACING_HEADER, traceId);
481
481
  }
482
482
 
483
+ if (auditContext) {
484
+ auditContext(queue);
485
+ }
486
+
483
487
  const parsedMessage = RabbitMq.parseMsg(msg);
484
488
  const releaseLock = await this.lockRedisIfNeeded(parsedMessage, optionsWithDefaults);
485
489
  const shouldConsume = await this.shouldConsumeMessageByTimestamp(parsedMessage);
@@ -512,7 +516,7 @@ class RabbitMq implements IAfRabbitMq {
512
516
  RabbitMq.validateName('exchange', exchange);
513
517
  RabbitMq.validateName('queue', queue);
514
518
  const { limit, deadMessageTtl } = optionsWithDefaults;
515
- const channel: ChannelWrapper = await this.getNewChannel();
519
+ const channel: ChannelWrapper = await this.getNewChannel({ name: `consume-exchange-${exchange}-queue-${queue}` });
516
520
 
517
521
  return channel.addSetup(async (c: ConfirmChannel) => {
518
522
  const assertExchange = await assertExchangeFanout(c, exchange);
package/src/lib/consts.ts CHANGED
@@ -10,4 +10,5 @@ export const DEFAULT_OPTIONS = {
10
10
  deadMessageTtl: DEFAULT_DEAD_TTL_TWO_DAYS,
11
11
  lockTimeout: DEFAULT_LOCK_TIMEOUT,
12
12
  useConsumeWithLock: DEFAULT_USE_CONSUME_WITH_LOCK,
13
+ auditContext: null,
13
14
  };
package/src/lib/types.ts CHANGED
@@ -21,6 +21,7 @@ export interface ConsumeOptions {
21
21
  limit?: number;
22
22
  lockTimeout?: number;
23
23
  useConsumeWithLock?: boolean;
24
+ auditContext?: any;
24
25
  }
25
26
 
26
27
  export type CallbackFunction = (msg: ConsumeMessage, ack: any, nack: any) => Promise<any>
package/src/lib/utils.ts CHANGED
@@ -12,3 +12,4 @@ export const wrapSetImmediate = (callback: () => any) => new Promise<any>((resol
12
12
  }
13
13
  });
14
14
  });
15
+ export const rand = () => Math.floor(Math.random() * 100000);