@autofleet/rabbit 1.0.13 → 1.0.14

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,19 +1,23 @@
1
1
  /// <reference types="node" />
2
- import { Channel } from 'amqplib';
2
+ import { Connection, Channel } from 'amqplib';
3
3
  import { EventEmitter } from 'events';
4
4
  declare class RabbitMq {
5
5
  channel: Channel | null;
6
+ connection: Connection | null;
6
7
  em: EventEmitter;
7
8
  creatingChannel: boolean;
9
+ queues: any;
10
+ exchanges: any;
8
11
  static parseMsg(msg: any): any;
9
12
  constructor();
10
13
  ack: (msg: any) => Promise<void>;
11
14
  nack: (queue: string) => (msg: any, retries: number) => Promise<void>;
15
+ getConnection(): Promise<Connection>;
12
16
  assertChannel(): Promise<Channel>;
13
17
  assertExchange(exchange: string, options?: any): Promise<import("amqplib").Replies.AssertExchange>;
14
18
  assertQueue(queue: string, options?: any): Promise<import("amqplib").Replies.AssertQueue>;
15
- consume(queue: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any): Promise<import("amqplib").Replies.Consume>;
16
- consumeFromExchange(queue: string, exchange: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any): Promise<import("amqplib").Replies.Consume>;
19
+ consume(queue: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any, isFirstTime?: boolean): Promise<import("amqplib").Replies.Consume | undefined>;
20
+ consumeFromExchange(queue: string, exchange: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any, isFirstTime?: boolean): Promise<import("amqplib").Replies.Consume | undefined>;
17
21
  publish(exchange: string, content: any): Promise<boolean>;
18
22
  sendToQueue(queue: string, content: any): Promise<boolean>;
19
23
  }
package/dist/index.js CHANGED
@@ -27,6 +27,9 @@ class RabbitMq {
27
27
  this.creatingChannel = false;
28
28
  this.em = new events_1.EventEmitter;
29
29
  this.channel = null;
30
+ this.connection = null;
31
+ this.queues = {};
32
+ this.exchanges = {};
30
33
  process.once('SIGINT', () => {
31
34
  console.log('Removing channel');
32
35
  if (this.channel) {
@@ -43,6 +46,12 @@ class RabbitMq {
43
46
  catch (e) { }
44
47
  return Object.assign({}, msg, { content });
45
48
  }
49
+ async getConnection() {
50
+ let host = process.env.RABBITMQ_SERVICE_HOST || '35.240.13.28';
51
+ let connection = this.connection || await amqplib_1.connect(`amqp://${host}`);
52
+ this.connection = connection;
53
+ return connection;
54
+ }
46
55
  async assertChannel() {
47
56
  return new Promise(async (resolve, reject) => {
48
57
  if (this.channel) {
@@ -52,21 +61,41 @@ class RabbitMq {
52
61
  return this.em.on(channelCreatedConst, resolve);
53
62
  }
54
63
  this.creatingChannel = true;
55
- let connection, channel;
56
- let host = process.env.RABBITMQ_SERVICE_HOST || '35.240.13.28';
64
+ let connection;
65
+ let channel = this.channel;
57
66
  try {
58
- connection = await amqplib_1.connect(`amqp://${host}`);
67
+ if (!this.connection) {
68
+ connection = await this.getConnection();
69
+ connection.on('error', async () => {
70
+ console.log('closed connection');
71
+ this.channel = null;
72
+ this.connection = null;
73
+ await this.assertChannel();
74
+ });
75
+ }
76
+ connection = await this.getConnection();
59
77
  channel = await connection.createChannel();
60
78
  this.channel = channel;
61
- channel.on('close', async () => {
62
- this.channel = null;
63
- await this.assertChannel();
79
+ this.creatingChannel = false;
80
+ const queueJobs = [];
81
+ const exchangeJobs = [];
82
+ const queueKeys = Object.keys(this.queues);
83
+ queueKeys.forEach(queue => {
84
+ const { callback, options } = this.queues[queue];
85
+ queueJobs.push(this.consume(queue, callback, options, false));
64
86
  });
65
- connection.on('close', async () => {
87
+ const exchangeKeys = Object.keys(this.exchanges);
88
+ exchangeKeys.forEach(queue => {
89
+ const { exchange, callback, options } = this.exchanges[queue];
90
+ exchangeJobs.push(this.consumeFromExchange(queue, exchange, callback, options, false));
91
+ });
92
+ await Promise.all(queueJobs);
93
+ await Promise.all(exchangeJobs);
94
+ channel.on('error', async () => {
95
+ console.log('closed channel');
66
96
  this.channel = null;
67
97
  await this.assertChannel();
68
98
  });
69
- this.creatingChannel = false;
70
99
  this.em.emit(channelCreatedConst, channel);
71
100
  resolve(channel);
72
101
  }
@@ -83,23 +112,29 @@ class RabbitMq {
83
112
  const channel = await this.assertChannel();
84
113
  return channel.assertQueue(queue);
85
114
  }
86
- async consume(queue, callback, options) {
115
+ async consume(queue, callback, options, isFirstTime = true) {
116
+ this.queues[queue] = this.queues[queue] ? this.queues[queue] : { callback, options };
87
117
  const { limit } = options ? options : 1;
88
118
  const channel = await this.assertChannel();
89
119
  await this.assertQueue(queue);
90
- channel.prefetch(limit, false);
91
- return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue)));
120
+ if (!isFirstTime) {
121
+ channel.prefetch(limit, false);
122
+ return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue)));
123
+ }
92
124
  }
93
- async consumeFromExchange(queue, exchange, callback, options) {
125
+ async consumeFromExchange(queue, exchange, callback, options, isFirstTime = true) {
126
+ this.exchanges[queue] = this.exchanges[queue] ? this.exchanges[queue] : { exchange, callback, options };
94
127
  const { limit } = options ? options : 1;
95
128
  const channel = await this.assertChannel();
96
129
  await Promise.all([
97
130
  this.assertExchange(exchange),
98
131
  this.assertQueue(queue),
99
132
  ]);
100
- channel.prefetch(limit, false);
101
- await channel.bindQueue(queue, exchange, '');
102
- return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue)));
133
+ if (!isFirstTime) {
134
+ channel.prefetch(limit, false);
135
+ await channel.bindQueue(queue, exchange, '');
136
+ return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue)));
137
+ }
103
138
  }
104
139
  async publish(exchange, content) {
105
140
  const channel = await this.assertChannel();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/rabbit",
3
- "version": "1.0.13",
3
+ "version": "1.0.14",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
package/src/index.ts CHANGED
@@ -4,8 +4,11 @@ import { EventEmitter } from 'events';
4
4
  const channelCreatedConst: string = 'channelCreated';
5
5
  class RabbitMq {
6
6
  channel: Channel | null;
7
+ connection: Connection | null;
7
8
  em: EventEmitter;
8
9
  creatingChannel: boolean;
10
+ queues: any;
11
+ exchanges: any;
9
12
 
10
13
  static parseMsg(msg: any) {
11
14
  let { content } = msg;
@@ -25,7 +28,9 @@ class RabbitMq {
25
28
  this.creatingChannel = false;
26
29
  this.em = new EventEmitter;
27
30
  this.channel = null;
28
-
31
+ this.connection = null;
32
+ this.queues = {};
33
+ this.exchanges = {};
29
34
  process.once('SIGINT', () => {
30
35
  console.log('Removing channel');
31
36
  if(this.channel) {
@@ -54,6 +59,13 @@ class RabbitMq {
54
59
  }
55
60
  }
56
61
 
62
+ async getConnection() {
63
+ let host: string = process.env.RABBITMQ_SERVICE_HOST || '35.240.13.28';
64
+ let connection: Connection = this.connection || await connect(`amqp://${host}`);
65
+ this.connection = connection;
66
+ return connection;
67
+ }
68
+
57
69
  async assertChannel() {
58
70
  return new Promise<Channel>(async (resolve, reject) => {
59
71
  if(this.channel) {
@@ -63,22 +75,42 @@ class RabbitMq {
63
75
  }
64
76
 
65
77
  this.creatingChannel = true;
66
- let connection: Connection, channel: Channel;
67
- let host: string = process.env.RABBITMQ_SERVICE_HOST || '35.240.13.28';
78
+ let connection: Connection;
79
+ let channel: Channel | null = this.channel;
68
80
 
69
81
  try {
70
- connection = await connect(`amqp://${host}`);
82
+ if(!this.connection) {
83
+ connection = await this.getConnection();
84
+ connection.on('error', async () => {
85
+ console.log('closed connection');
86
+ this.channel = null;
87
+ this.connection = null;
88
+ await this.assertChannel();
89
+ });
90
+ }
91
+ connection = await this.getConnection();
71
92
  channel = await connection.createChannel();
72
93
  this.channel = channel;
73
- channel.on('close', async () => {
74
- this.channel = null;
75
- await this.assertChannel();
94
+ this.creatingChannel = false;
95
+ const queueJobs : Array<any> = [];
96
+ const exchangeJobs : Array<any> = [];
97
+ const queueKeys = Object.keys(this.queues);
98
+ queueKeys.forEach(queue => {
99
+ const { callback, options } = this.queues[queue];
100
+ queueJobs.push(this.consume(queue, callback, options, false));
76
101
  });
77
- connection.on('close', async () => {
78
- this.channel = null;
79
- await this.assertChannel();
102
+ const exchangeKeys = Object.keys(this.exchanges);
103
+ exchangeKeys.forEach(queue => {
104
+ const { exchange, callback, options } = this.exchanges[queue];
105
+ exchangeJobs.push(this.consumeFromExchange(queue, exchange, callback, options, false));
106
+ });
107
+ await Promise.all(queueJobs);
108
+ await Promise.all(exchangeJobs);
109
+ channel.on('error', async () => {
110
+ console.log('closed channel');
111
+ this.channel = null;
112
+ await this.assertChannel();
80
113
  });
81
- this.creatingChannel = false;
82
114
  this.em.emit(channelCreatedConst, channel);
83
115
  resolve(channel);
84
116
  } catch (e) {
@@ -97,24 +129,30 @@ class RabbitMq {
97
129
  return channel.assertQueue(queue);
98
130
  }
99
131
 
100
- async consume(queue: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any) {
132
+ async consume(queue: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any, isFirstTime: boolean = true) {
133
+ this.queues[queue] = this.queues[queue] ? this.queues[queue] : { callback, options };
101
134
  const { limit } = options ? options : 1;
102
135
  const channel: Channel = await this.assertChannel();
103
136
  await this.assertQueue(queue);
104
- channel.prefetch(limit, false);
105
- return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue)));
137
+ if (!isFirstTime) {
138
+ channel.prefetch(limit, false);
139
+ return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue)));
140
+ }
106
141
  }
107
142
 
108
- async consumeFromExchange(queue: string, exchange: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any) {
143
+ async consumeFromExchange(queue: string, exchange: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any, isFirstTime: boolean = true) {
144
+ this.exchanges[queue] = this.exchanges[queue] ? this.exchanges[queue] : { exchange, callback, options }
109
145
  const { limit } = options ? options : 1;
110
146
  const channel: Channel = await this.assertChannel();
111
147
  await Promise.all([
112
148
  this.assertExchange(exchange),
113
149
  this.assertQueue(queue),
114
150
  ]);
115
- channel.prefetch(limit, false);
116
- await channel.bindQueue(queue, exchange, '');
117
- return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue)));
151
+ if (!isFirstTime) {
152
+ channel.prefetch(limit, false);
153
+ await channel.bindQueue(queue, exchange, '');
154
+ return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue)));
155
+ }
118
156
  }
119
157
 
120
158
  async publish(exchange: string, content: any) {