@autofleet/rabbit 1.0.12 → 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,13 +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
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));
86
+ });
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');
96
+ this.channel = null;
97
+ await this.assertChannel();
98
+ });
62
99
  this.em.emit(channelCreatedConst, channel);
63
100
  resolve(channel);
64
101
  }
@@ -75,23 +112,29 @@ class RabbitMq {
75
112
  const channel = await this.assertChannel();
76
113
  return channel.assertQueue(queue);
77
114
  }
78
- 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 };
79
117
  const { limit } = options ? options : 1;
80
118
  const channel = await this.assertChannel();
81
119
  await this.assertQueue(queue);
82
- channel.prefetch(limit, false);
83
- 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
+ }
84
124
  }
85
- 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 };
86
127
  const { limit } = options ? options : 1;
87
128
  const channel = await this.assertChannel();
88
129
  await Promise.all([
89
130
  this.assertExchange(exchange),
90
131
  this.assertQueue(queue),
91
132
  ]);
92
- channel.prefetch(limit, false);
93
- await channel.bindQueue(queue, exchange, '');
94
- 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
+ }
95
138
  }
96
139
  async publish(exchange, content) {
97
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.12",
3
+ "version": "1.0.14",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
package/src/index.ts CHANGED
@@ -1,12 +1,14 @@
1
1
  import { connect, Connection, Channel } from 'amqplib';
2
2
  import { EventEmitter } from 'events';
3
- import { reject } from 'bluebird';
4
3
 
5
4
  const channelCreatedConst: string = 'channelCreated';
6
5
  class RabbitMq {
7
6
  channel: Channel | null;
7
+ connection: Connection | null;
8
8
  em: EventEmitter;
9
9
  creatingChannel: boolean;
10
+ queues: any;
11
+ exchanges: any;
10
12
 
11
13
  static parseMsg(msg: any) {
12
14
  let { content } = msg;
@@ -26,7 +28,9 @@ class RabbitMq {
26
28
  this.creatingChannel = false;
27
29
  this.em = new EventEmitter;
28
30
  this.channel = null;
29
-
31
+ this.connection = null;
32
+ this.queues = {};
33
+ this.exchanges = {};
30
34
  process.once('SIGINT', () => {
31
35
  console.log('Removing channel');
32
36
  if(this.channel) {
@@ -55,6 +59,13 @@ class RabbitMq {
55
59
  }
56
60
  }
57
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
+
58
69
  async assertChannel() {
59
70
  return new Promise<Channel>(async (resolve, reject) => {
60
71
  if(this.channel) {
@@ -64,14 +75,42 @@ class RabbitMq {
64
75
  }
65
76
 
66
77
  this.creatingChannel = true;
67
- let connection: Connection, channel: Channel;
68
- let host: string = process.env.RABBITMQ_SERVICE_HOST || '35.240.13.28';
78
+ let connection: Connection;
79
+ let channel: Channel | null = this.channel;
69
80
 
70
81
  try {
71
- 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();
72
92
  channel = await connection.createChannel();
73
93
  this.channel = channel;
74
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));
101
+ });
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();
113
+ });
75
114
  this.em.emit(channelCreatedConst, channel);
76
115
  resolve(channel);
77
116
  } catch (e) {
@@ -90,24 +129,30 @@ class RabbitMq {
90
129
  return channel.assertQueue(queue);
91
130
  }
92
131
 
93
- 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 };
94
134
  const { limit } = options ? options : 1;
95
135
  const channel: Channel = await this.assertChannel();
96
136
  await this.assertQueue(queue);
97
- channel.prefetch(limit, false);
98
- 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
+ }
99
141
  }
100
142
 
101
- 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 }
102
145
  const { limit } = options ? options : 1;
103
146
  const channel: Channel = await this.assertChannel();
104
147
  await Promise.all([
105
148
  this.assertExchange(exchange),
106
149
  this.assertQueue(queue),
107
150
  ]);
108
- channel.prefetch(limit, false);
109
- await channel.bindQueue(queue, exchange, '');
110
- 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
+ }
111
156
  }
112
157
 
113
158
  async publish(exchange: string, content: any) {