@seidor-cloud-produtos/orbit-backend-lib 1.101.49 → 1.101.51

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.
@@ -4,6 +4,12 @@ import QueueConnection from '../../../application/queue/queue-connection';
4
4
  import DomainEvent from '../../../domain/events/domain-event';
5
5
  import QueueController from '../queue-controller';
6
6
  import { AmqpQueueGetResult } from './types';
7
+ interface SocketOptions {
8
+ retry?: {
9
+ maxCount: number;
10
+ intervalMs: number;
11
+ };
12
+ }
7
13
  export default class AmqpQueue implements QueueConnection {
8
14
  private uri;
9
15
  private static instance;
@@ -12,10 +18,10 @@ export default class AmqpQueue implements QueueConnection {
12
18
  private messagesInMemory;
13
19
  private controller;
14
20
  private channels;
15
- protected socketOptions?: any;
16
- constructor(uri?: string, socketOptions?: any);
21
+ protected socketOptions?: SocketOptions;
22
+ constructor(socketOptions?: SocketOptions, uri?: string);
17
23
  private setSocketOptions;
18
- connect(vHost: string): Promise<AmqpQueue>;
24
+ connect(vHost: string, retryCount?: number): Promise<AmqpQueue>;
19
25
  close(): Promise<void>;
20
26
  get(queueName: string, options?: amqp.Options.Get | undefined): Promise<AmqpQueueGetResult | null>;
21
27
  ack(channel: amqp.Channel, msg: any): Promise<void>;
@@ -24,7 +30,7 @@ export default class AmqpQueue implements QueueConnection {
24
30
  publish(exchangeName: string, domainEvent: DomainEvent, configs?: Record<string, any>): Promise<void>;
25
31
  createConsumers(queueName: string, configs: CreateConsumers): Promise<void>;
26
32
  setController(controller?: QueueController): this;
27
- static getInstance(vHost: string): Promise<AmqpQueue>;
33
+ static getInstance(vHost: string, socketOptions?: SocketOptions, uri?: string): Promise<AmqpQueue>;
28
34
  private treatReconnection;
29
35
  private reconnectionChannels;
30
36
  private publishMessagesOfMemory;
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
4
  const amqplib_1 = tslib_1.__importDefault(require("amqplib"));
5
+ const timeout_1 = require("../../../shared/timeout");
5
6
  class AmqpQueue {
6
7
  uri;
7
8
  static instance;
@@ -11,7 +12,7 @@ class AmqpQueue {
11
12
  controller;
12
13
  channels = new Map();
13
14
  socketOptions;
14
- constructor(uri = process.env.BROKER_AMQP, socketOptions) {
15
+ constructor(socketOptions, uri = process.env.BROKER_AMQP) {
15
16
  this.uri = uri;
16
17
  this.setSocketOptions(socketOptions);
17
18
  }
@@ -19,16 +20,26 @@ class AmqpQueue {
19
20
  this.socketOptions = socketOptions;
20
21
  return this;
21
22
  }
22
- async connect(vHost) {
23
- const urlConnection = `${this.uri}/${vHost}`;
24
- this.connection = await amqplib_1.default.connect(urlConnection, this.socketOptions);
25
- this.vHost = vHost;
26
- await this.treatReconnection(vHost);
27
- if (process.env.NODE_ENV !== 'test') {
28
- console.log(`⚙️ PROCESS ${this.vHost} PID: `, process.pid);
29
- console.log(`🐝 Queue connected on vHost: ${this.vHost}`);
23
+ async connect(vHost, retryCount = 0) {
24
+ try {
25
+ const urlConnection = `${this.uri}/${vHost}`;
26
+ this.connection = await amqplib_1.default.connect(urlConnection, this.socketOptions);
27
+ this.vHost = vHost;
28
+ await this.treatReconnection(vHost);
29
+ if (process.env.NODE_ENV !== 'test') {
30
+ console.log(`⚙️ PROCESS ${this.vHost} PID: `, process.pid);
31
+ console.log(`🐝 Queue connected on vHost: ${this.vHost}`);
32
+ }
33
+ return this;
34
+ }
35
+ catch (e) {
36
+ if (!this.socketOptions?.retry?.maxCount ||
37
+ retryCount >= this.socketOptions?.retry?.maxCount) {
38
+ throw e;
39
+ }
40
+ await (0, timeout_1.sleep)(this.socketOptions.retry.intervalMs);
41
+ return await this.connect(vHost, retryCount++);
30
42
  }
31
- return this;
32
43
  }
33
44
  async close() {
34
45
  await this.connection.close();
@@ -160,11 +171,11 @@ class AmqpQueue {
160
171
  }
161
172
  return this;
162
173
  }
163
- static async getInstance(vHost) {
174
+ static async getInstance(vHost, socketOptions, uri) {
164
175
  if (AmqpQueue.instance && AmqpQueue.instance.vHost === vHost) {
165
176
  return AmqpQueue.instance;
166
177
  }
167
- const queue = new AmqpQueue();
178
+ const queue = new AmqpQueue(socketOptions, uri);
168
179
  AmqpQueue.instance = await queue.connect(vHost);
169
180
  return AmqpQueue.instance;
170
181
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seidor-cloud-produtos/orbit-backend-lib",
3
- "version": "1.101.49",
3
+ "version": "1.101.51",
4
4
  "description": "Internal lib for backend components",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",