@autofleet/rabbit 3.2.26-beta.201 → 3.2.26-beta.3

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.d.ts CHANGED
@@ -1,10 +1,10 @@
1
1
  /// <reference types="node" />
2
2
  import { EventEmitter } from 'events';
3
- import { AmqpConnectionManager, ChannelWrapper } from 'amqp-connection-manager';
3
+ import { AmqpConnectionManager, ChannelWrapper, CreateChannelOpts } from 'amqp-connection-manager';
4
4
  import { ConfirmChannel, ConsumeMessage, Options, Replies } from 'amqplib';
5
5
  import { RedisConfig } from './lib/redis';
6
6
  import { ConnectionPurpose } from './lib/consts';
7
- import { CallbackFunction, ConsumeMessageOrNull, ConsumeOptions, CustomMessageHeaders, QueuesCache, RedisLockType, ExchangesCache, QueueSetupPromisesDictionary, AssertExchangePromisesDictionary, ConnectionData, assertChannelOpts, newChannelOpts, PublishOptions } from './lib/types';
7
+ import { CallbackFunction, ConsumeMessageOrNull, ConsumeOptions, CustomMessageHeaders, QueuesCache, RedisLockType, ExchangesCache, QueueSetupPromisesDictionary, AssertExchangePromisesDictionary, ConnectionData } from './lib/types';
8
8
  export interface IAfRabbitMq {
9
9
  ack: any;
10
10
  nack: any;
@@ -34,6 +34,17 @@ export interface AfRabbitOptions {
34
34
  dontRetryAssert?: boolean;
35
35
  rabbitHost?: string;
36
36
  }
37
+ type newChannelOpts = {
38
+ name?: string;
39
+ onClose?: null | ((args: any | null) => void);
40
+ options?: CreateChannelOpts | undefined;
41
+ connectionPurpose?: ConnectionPurpose;
42
+ };
43
+ type assertChannelOpts = {
44
+ channelName?: string;
45
+ force?: boolean;
46
+ connectionPurpose?: ConnectionPurpose;
47
+ };
37
48
  declare class RabbitMq implements IAfRabbitMq {
38
49
  static parseMsg(msg: any): any;
39
50
  static validateName(type: string, name: string): void;
@@ -73,7 +84,7 @@ declare class RabbitMq implements IAfRabbitMq {
73
84
  nack: (channel: ConfirmChannel, queue: string, options: any, deadQueueOptions: Options.AssertQueue, msg: ConsumeMessageOrNull, releaseLock: any) => (userMsg: ConsumeMessageOrNull, { skipRetry, }?: NackOptions) => Promise<any>;
74
85
  getConnection(connectionPurpose: ConnectionPurpose): Promise<AmqpConnectionManager | null | undefined>;
75
86
  getNewChannel({ name, onClose, options, connectionPurpose, }: newChannelOpts): Promise<ChannelWrapper>;
76
- assertChannel({ force, connectionPurpose, newChannelOptions }: assertChannelOpts): Promise<ChannelWrapper>;
87
+ assertChannel({ force, connectionPurpose }: assertChannelOpts): Promise<ChannelWrapper>;
77
88
  assertExchange(exchangeName: string, options?: any): Promise<any>;
78
89
  getQueueLength(queue: string, connectionPurpose?: ConnectionPurpose): Promise<Replies.AssertQueue>;
79
90
  private deleteQueue;
@@ -87,7 +98,7 @@ declare class RabbitMq implements IAfRabbitMq {
87
98
  private unlockRedisIfNeeded;
88
99
  private consumeFromRabbit;
89
100
  consumeFromExchange(queue: string, exchange: string, callback: CallbackFunction, options?: ConsumeOptions): Promise<any>;
90
- publish(exchange: string, content: any, customHeaders?: any, options?: PublishOptions): Promise<boolean>;
101
+ publish(exchange: string, content: any, customHeaders?: any): Promise<boolean>;
91
102
  sendToQueue(queue: string, content: any, options?: any, customHeaders?: any): Promise<boolean | undefined>;
92
103
  isConnected(): Promise<boolean>;
93
104
  gracefulShutdown(signal: string): Promise<void>;
package/dist/index.js CHANGED
@@ -7,7 +7,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
7
7
  exports.sendCeleryTaskViaHttp = void 0;
8
8
  const events_1 = require("events");
9
9
  const util_1 = require("util");
10
- const lodash_1 = require("lodash");
11
10
  const moment_1 = __importDefault(require("moment"));
12
11
  const redis_lock_1 = __importDefault(require("redis-lock"));
13
12
  const amqp_connection_manager_1 = require("amqp-connection-manager");
@@ -132,7 +131,6 @@ class RabbitMq {
132
131
  creatingConnection: false,
133
132
  connectionCreatedEventName: 'publishConnectionCreated',
134
133
  connectionFailedEventName: 'publishConnectionFailed',
135
- doesChannelInitialize: false,
136
134
  },
137
135
  };
138
136
  this.exchanges = {};
@@ -261,7 +259,7 @@ class RabbitMq {
261
259
  throw err;
262
260
  }
263
261
  }
264
- async assertChannel({ force = false, connectionPurpose = consts_1.ConnectionPurpose.Consume, newChannelOptions = {} }) {
262
+ async assertChannel({ force = false, connectionPurpose = consts_1.ConnectionPurpose.Consume }) {
265
263
  debug('rabbit: start assert channel', { connectionPurpose, publishPromise: this.publishChannelSetupPromise, channel: this.publishChannel });
266
264
  if (!this.publishChannelSetupPromise || (!this.publishChannel && connectionPurpose === consts_1.ConnectionPurpose.Publish)) {
267
265
  this.publishChannelSetupPromise = new Promise(async (resolve, reject) => {
@@ -269,14 +267,13 @@ class RabbitMq {
269
267
  return resolve(this.publishChannel);
270
268
  }
271
269
  try {
272
- const channel = await this.getNewChannel({ connectionPurpose, options: newChannelOptions });
270
+ const channel = await this.getNewChannel({ connectionPurpose });
273
271
  debug('rabbit: new channel got', { connectionPurpose, channel: this.publishChannel });
274
272
  channel.on('error', (err) => {
275
273
  logger_1.default.error('rabbit: channel error', { err });
276
274
  });
277
275
  if (connectionPurpose === consts_1.ConnectionPurpose.Publish) {
278
276
  this.publishChannel = channel;
279
- this.connectionsMap[connectionPurpose].doesChannelInitialize = true;
280
277
  }
281
278
  resolve(channel);
282
279
  }
@@ -429,9 +426,6 @@ class RabbitMq {
429
426
  return channel.addSetup(async (confirmChannel) => {
430
427
  const q = await this.assertQueue(queue, consts_1.ConnectionPurpose.Consume, optionsWithDefaults);
431
428
  await confirmChannel.prefetch(limit, false);
432
- const channelConsumerOptions = optionsWithDefaults.channelConsumeOptions
433
- ? (0, lodash_1.merge)(types_1.CONSUMER_DEFAULT_OPTIONS, optionsWithDefaults.channelConsumeOptions)
434
- : types_1.CONSUMER_DEFAULT_OPTIONS;
435
429
  const { consumerTag } = await confirmChannel.consume(queue, async (msg) => {
436
430
  if (!msg) {
437
431
  return null;
@@ -496,7 +490,7 @@ class RabbitMq {
496
490
  catch (e) {
497
491
  await localNack(msg);
498
492
  }
499
- }, channelConsumerOptions);
493
+ }, types_1.CONSUMER_DEFAULT_OPTIONS);
500
494
  if (!consumerTag) {
501
495
  logger_1.default.error(`rabbit: failed to consume from queue ${queue}`);
502
496
  }
@@ -524,24 +518,18 @@ class RabbitMq {
524
518
  ]);
525
519
  });
526
520
  }
527
- async publish(exchange, content, customHeaders, options) {
521
+ async publish(exchange, content, customHeaders) {
528
522
  debug('rabbit: start publish msg');
529
523
  return (0, utils_1.wrapSetImmediate)(async () => {
530
524
  RabbitMq.validateName('exchange', exchange);
531
- const channel = await this.assertChannel({
532
- connectionPurpose: consts_1.ConnectionPurpose.Publish,
533
- newChannelOptions: options?.newChannelOptions,
534
- });
525
+ const channel = await this.assertChannel({ connectionPurpose: consts_1.ConnectionPurpose.Publish });
535
526
  await this.assertExchange(exchange, { connectionPurpose: consts_1.ConnectionPurpose.Publish });
536
527
  await channel.publish(exchange, '', Buffer.from(JSON.stringify(content)), RabbitMq.getPublishOptions(customHeaders));
537
528
  });
538
529
  }
539
530
  async sendToQueue(queue, content, options, customHeaders) {
540
531
  try {
541
- await this.assertChannel({
542
- connectionPurpose: consts_1.ConnectionPurpose.Publish,
543
- newChannelOptions: options?.newChannelOptions,
544
- });
532
+ await this.assertChannel({ connectionPurpose: consts_1.ConnectionPurpose.Publish });
545
533
  }
546
534
  catch (e) {
547
535
  logger_1.default.error(`rabbit sendToQueue: failed to send assert channel when sending to queue ${queue}`, { e });
@@ -568,16 +556,7 @@ class RabbitMq {
568
556
  async isConnected() {
569
557
  debug('rabbit: start is connected');
570
558
  const isEachConnectionConnected = await Promise.all(Object.entries(this.connectionsMap).map(async ([connectionPurpose, connectionData]) => {
571
- const { connection, doesChannelInitialize } = connectionData;
572
- if (connectionPurpose === consts_1.ConnectionPurpose.Consume
573
- || (connectionPurpose === consts_1.ConnectionPurpose.Publish
574
- && doesChannelInitialize)) {
575
- debug('rabbit: is connected inside map', { connection, connectionPurpose });
576
- const isConnected = connection?.isConnected();
577
- if (!isConnected) {
578
- logger_1.default.error('rabbit: isConnected - false', { connectionPurpose });
579
- return false;
580
- }
559
+ if (connectionPurpose === consts_1.ConnectionPurpose.Publish) {
581
560
  const channel = await this.assertChannel({ connectionPurpose: connectionPurpose });
582
561
  try {
583
562
  await Promise.all([
@@ -590,6 +569,13 @@ class RabbitMq {
590
569
  return false;
591
570
  }
592
571
  }
572
+ const { connection } = connectionData;
573
+ debug('rabbit: is connected inside map', { connection, connectionPurpose });
574
+ const isConnected = connection?.isConnected();
575
+ if (!isConnected) {
576
+ logger_1.default.error('rabbit: isConnected - false', { connectionPurpose });
577
+ return false;
578
+ }
593
579
  logger_1.default.info('rabbit: isConnected - true');
594
580
  return true;
595
581
  }));
@@ -1,6 +1,5 @@
1
- import { AmqpConnectionManager, CreateChannelOpts } from 'amqp-connection-manager';
1
+ import { AmqpConnectionManager } from 'amqp-connection-manager';
2
2
  import { ConsumeMessage, Options, Replies } from 'amqplib';
3
- import { ConnectionPurpose } from './consts';
4
3
  export interface ExchangesCache {
5
4
  [key: string]: any;
6
5
  }
@@ -27,34 +26,25 @@ export interface ConsumeOptions {
27
26
  useConsumeWithLock?: boolean;
28
27
  auditContext?: any;
29
28
  enableRabbitTrace?: boolean;
30
- channelConsumeOptions?: Options.Consume;
31
29
  }
32
30
  export type CallbackFunction = (msg: ConsumeMessage, ack: any, nack: any) => Promise<any>;
33
31
  export type newChannelOpts = {
34
32
  name?: string;
35
33
  onClose?: null | ((args: any | null) => void);
36
- options?: CreateChannelOpts | undefined;
37
- connectionPurpose?: ConnectionPurpose;
38
34
  };
39
35
  export type assertChannelOpts = {
40
36
  channelName?: string;
41
37
  force?: boolean;
42
- newChannelOptions?: newChannelOpts;
43
- connectionPurpose?: ConnectionPurpose;
44
38
  };
45
39
  export type AfConsumer = {
46
40
  queue: string;
47
41
  callback: CallbackFunction;
48
42
  options: ConsumeOptions | undefined;
49
43
  };
50
- export interface PublishOptions {
51
- newChannelOptions?: newChannelOpts;
52
- }
53
44
  export declare const CONSUMER_DEFAULT_OPTIONS: Options.Consume;
54
45
  export type ConnectionData = {
55
46
  connection: AmqpConnectionManager | null;
56
47
  creatingConnection: boolean;
57
48
  connectionCreatedEventName: string;
58
49
  connectionFailedEventName: string;
59
- doesChannelInitialize?: boolean;
60
50
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/rabbit",
3
- "version": "3.2.26-beta.201",
3
+ "version": "3.2.26-beta.3",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "engines": {
@@ -19,11 +19,9 @@
19
19
  },
20
20
  "dependencies": {
21
21
  "@autofleet/zehut": "^3.1.2",
22
- "@types/lodash": "^4.17.16",
23
22
  "amqp-connection-manager": "4.1.9",
24
23
  "amqplib": "0.10.3",
25
24
  "bluebird": "^3.7.2",
26
- "lodash": "^4.17.21",
27
25
  "moment": "^2.29.1",
28
26
  "redis": "^3.1.2",
29
27
  "redis-lock": "^0.1.4"
package/src/index.ts CHANGED
@@ -2,11 +2,10 @@
2
2
 
3
3
  import { EventEmitter, once } from 'events';
4
4
  import { promisify } from 'util';
5
- import { merge } from 'lodash';
6
5
  import moment from 'moment';
7
6
  import RedisLock from 'redis-lock';
8
7
  import {
9
- AmqpConnectionManager, ChannelWrapper, connect,
8
+ AmqpConnectionManager, ChannelWrapper, connect, CreateChannelOpts,
10
9
  } from 'amqp-connection-manager';
11
10
  import {
12
11
  ConfirmChannel, ConsumeMessage, Options, Replies,
@@ -41,9 +40,6 @@ import {
41
40
  QueueSetupPromisesDictionary,
42
41
  AssertExchangePromisesDictionary,
43
42
  ConnectionData,
44
- assertChannelOpts,
45
- newChannelOpts,
46
- PublishOptions,
47
43
  } from './lib/types';
48
44
 
49
45
  // const debug = nodeDebug('af-rabbitmq')
@@ -85,6 +81,19 @@ export interface AfRabbitOptions {
85
81
  rabbitHost?: string;
86
82
  }
87
83
 
84
+ type newChannelOpts = {
85
+ name?: string;
86
+ onClose?: null | ((args: any | null) => void);
87
+ options?: CreateChannelOpts | undefined;
88
+ connectionPurpose?: ConnectionPurpose,
89
+ };
90
+
91
+ type assertChannelOpts = {
92
+ channelName?: string;
93
+ force?: boolean;
94
+ connectionPurpose?: ConnectionPurpose;
95
+ }
96
+
88
97
  type AfConsumer = {
89
98
  queue: string;
90
99
  callback: CallbackFunction;
@@ -183,7 +192,6 @@ class RabbitMq implements IAfRabbitMq {
183
192
  creatingConnection: false,
184
193
  connectionCreatedEventName: 'publishConnectionCreated',
185
194
  connectionFailedEventName: 'publishConnectionFailed',
186
- doesChannelInitialize: false,
187
195
  },
188
196
  };
189
197
  this.exchanges = {};
@@ -402,7 +410,7 @@ class RabbitMq implements IAfRabbitMq {
402
410
  }
403
411
  }
404
412
 
405
- async assertChannel({ force = false, connectionPurpose = ConnectionPurpose.Consume, newChannelOptions = {} }: assertChannelOpts): Promise<ChannelWrapper> {
413
+ async assertChannel({ force = false, connectionPurpose = ConnectionPurpose.Consume }: assertChannelOpts): Promise<ChannelWrapper> {
406
414
  debug('rabbit: start assert channel', { connectionPurpose, publishPromise: this.publishChannelSetupPromise, channel: this.publishChannel });
407
415
  if (!this.publishChannelSetupPromise || (!this.publishChannel && connectionPurpose === ConnectionPurpose.Publish)) {
408
416
  this.publishChannelSetupPromise = new Promise<ChannelWrapper>(async (resolve, reject) => {
@@ -411,14 +419,13 @@ class RabbitMq implements IAfRabbitMq {
411
419
  }
412
420
 
413
421
  try {
414
- const channel = await this.getNewChannel({ connectionPurpose, options: newChannelOptions });
422
+ const channel = await this.getNewChannel({ connectionPurpose });
415
423
  debug('rabbit: new channel got', { connectionPurpose, channel: this.publishChannel });
416
424
  channel.on('error', (err) => {
417
425
  logger.error('rabbit: channel error', { err });
418
426
  });
419
427
  if (connectionPurpose === ConnectionPurpose.Publish) {
420
428
  this.publishChannel = channel;
421
- this.connectionsMap[connectionPurpose].doesChannelInitialize = true;
422
429
  }
423
430
  resolve(channel);
424
431
  } catch (e) {
@@ -595,9 +602,6 @@ class RabbitMq implements IAfRabbitMq {
595
602
  return channel.addSetup(async (confirmChannel: ConfirmChannel) => {
596
603
  const q = await this.assertQueue(queue, ConnectionPurpose.Consume, optionsWithDefaults);
597
604
  await confirmChannel.prefetch(limit, false);
598
- const channelConsumerOptions = optionsWithDefaults.channelConsumeOptions
599
- ? merge(CONSUMER_DEFAULT_OPTIONS, optionsWithDefaults.channelConsumeOptions)
600
- : CONSUMER_DEFAULT_OPTIONS;
601
605
  const { consumerTag } = await confirmChannel.consume(
602
606
  queue,
603
607
  async (msg: ConsumeMessageOrNull) => {
@@ -673,7 +677,7 @@ class RabbitMq implements IAfRabbitMq {
673
677
  } catch (e) {
674
678
  await localNack(msg);
675
679
  }
676
- }, channelConsumerOptions,
680
+ }, CONSUMER_DEFAULT_OPTIONS,
677
681
  );
678
682
  if (!consumerTag) {
679
683
  logger.error(`rabbit: failed to consume from queue ${queue}`);
@@ -708,14 +712,11 @@ class RabbitMq implements IAfRabbitMq {
708
712
  });
709
713
  }
710
714
 
711
- async publish(exchange: string, content: any, customHeaders?: any, options?: PublishOptions): Promise<boolean> {
715
+ async publish(exchange: string, content: any, customHeaders?: any): Promise<boolean> {
712
716
  debug('rabbit: start publish msg');
713
717
  return wrapSetImmediate(async () => {
714
718
  RabbitMq.validateName('exchange', exchange);
715
- const channel: ChannelWrapper = await this.assertChannel({
716
- connectionPurpose: ConnectionPurpose.Publish,
717
- newChannelOptions: options?.newChannelOptions,
718
- });
719
+ const channel: ChannelWrapper = await this.assertChannel({ connectionPurpose: ConnectionPurpose.Publish });
719
720
  await this.assertExchange(exchange, { connectionPurpose: ConnectionPurpose.Publish });
720
721
  await channel.publish(exchange, '',
721
722
  Buffer.from(JSON.stringify(content)),
@@ -730,10 +731,7 @@ class RabbitMq implements IAfRabbitMq {
730
731
  customHeaders?: any,
731
732
  ): Promise<boolean | undefined> {
732
733
  try {
733
- await this.assertChannel({
734
- connectionPurpose: ConnectionPurpose.Publish,
735
- newChannelOptions: options?.newChannelOptions,
736
- });
734
+ await this.assertChannel({ connectionPurpose: ConnectionPurpose.Publish });
737
735
  } catch (e) {
738
736
  logger.error(`rabbit sendToQueue: failed to send assert channel when sending to queue ${queue}`, { e });
739
737
  throw e;
@@ -763,18 +761,7 @@ class RabbitMq implements IAfRabbitMq {
763
761
  debug('rabbit: start is connected');
764
762
  const isEachConnectionConnected = await Promise.all(
765
763
  Object.entries(this.connectionsMap).map(async ([connectionPurpose, connectionData]) => {
766
- const { connection, doesChannelInitialize } = connectionData;
767
- if (
768
- connectionPurpose === ConnectionPurpose.Consume
769
- || (connectionPurpose === ConnectionPurpose.Publish
770
- && doesChannelInitialize
771
- )) {
772
- debug('rabbit: is connected inside map', { connection, connectionPurpose });
773
- const isConnected = connection?.isConnected();
774
- if (!isConnected) {
775
- logger.error('rabbit: isConnected - false', { connectionPurpose });
776
- return false;
777
- }
764
+ if (connectionPurpose === ConnectionPurpose.Publish) {
778
765
  const channel: any = await this.assertChannel({ connectionPurpose: connectionPurpose as ConnectionPurpose });
779
766
  try {
780
767
  await Promise.all([
@@ -786,6 +773,13 @@ class RabbitMq implements IAfRabbitMq {
786
773
  return false;
787
774
  }
788
775
  }
776
+ const { connection } = connectionData;
777
+ debug('rabbit: is connected inside map', { connection, connectionPurpose });
778
+ const isConnected = connection?.isConnected();
779
+ if (!isConnected) {
780
+ logger.error('rabbit: isConnected - false', { connectionPurpose });
781
+ return false;
782
+ }
789
783
  logger.info('rabbit: isConnected - true');
790
784
  return true;
791
785
  }),
package/src/lib/types.ts CHANGED
@@ -1,6 +1,5 @@
1
- import { AmqpConnectionManager, CreateChannelOpts } from 'amqp-connection-manager';
1
+ import { AmqpConnectionManager } from 'amqp-connection-manager';
2
2
  import { ConsumeMessage, Options, Replies } from 'amqplib';
3
- import { ConnectionPurpose } from './consts';
4
3
 
5
4
  export interface ExchangesCache {
6
5
  [key: string]: any
@@ -33,7 +32,6 @@ export interface ConsumeOptions {
33
32
  useConsumeWithLock?: boolean;
34
33
  auditContext?: any;
35
34
  enableRabbitTrace?: boolean;
36
- channelConsumeOptions?: Options.Consume;
37
35
  }
38
36
 
39
37
  export type CallbackFunction = (msg: ConsumeMessage, ack: any, nack: any) => Promise<any>
@@ -41,14 +39,10 @@ export type CallbackFunction = (msg: ConsumeMessage, ack: any, nack: any) => Pro
41
39
  export type newChannelOpts = {
42
40
  name?: string;
43
41
  onClose?: null | ((args: any | null) => void);
44
- options?: CreateChannelOpts | undefined;
45
- connectionPurpose?: ConnectionPurpose;
46
42
  };
47
43
  export type assertChannelOpts = {
48
44
  channelName?: string;
49
45
  force?: boolean;
50
- newChannelOptions?: newChannelOpts;
51
- connectionPurpose?: ConnectionPurpose;
52
46
  }
53
47
  export type AfConsumer = {
54
48
  queue: string;
@@ -56,10 +50,6 @@ export type AfConsumer = {
56
50
  options: ConsumeOptions | undefined;
57
51
  }
58
52
 
59
- export interface PublishOptions {
60
- newChannelOptions?: newChannelOpts;
61
- }
62
-
63
53
  const HA_PROMOTE_ON_FAILURE = 'ha-promote-on-failure';
64
54
 
65
55
  const HA_PROMOTE_ON_SHUTDOWN = 'ha-promote-on-shutdown';
@@ -76,5 +66,4 @@ export type ConnectionData = {
76
66
  creatingConnection: boolean;
77
67
  connectionCreatedEventName: string;
78
68
  connectionFailedEventName: string;
79
- doesChannelInitialize?: boolean;
80
69
  };