@autofleet/rabbit 1.0.19 → 1.0.21
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 +1 -0
- package/dist/index.js +17 -7
- package/package.json +1 -1
- package/src/index.ts +19 -7
package/dist/index.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ declare class RabbitMq {
|
|
|
5
5
|
channel: ChannelWrapper | null;
|
|
6
6
|
connection: AmqpConnectionManager | null;
|
|
7
7
|
em: EventEmitter;
|
|
8
|
+
creatingConnection: Boolean;
|
|
8
9
|
constructor();
|
|
9
10
|
ack: (msg: any) => Promise<void>;
|
|
10
11
|
nack: (queue: string) => (msg: any, retries: number) => Promise<void>;
|
package/dist/index.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const amqp_connection_manager_1 = require("amqp-connection-manager");
|
|
4
4
|
const events_1 = require("events");
|
|
5
|
+
const connectionCreatedConst = 'connectionCreated';
|
|
5
6
|
class RabbitMq {
|
|
6
7
|
constructor() {
|
|
7
8
|
this.ack = async (msg) => {
|
|
@@ -26,15 +27,24 @@ class RabbitMq {
|
|
|
26
27
|
this.em = new events_1.EventEmitter;
|
|
27
28
|
this.channel = null;
|
|
28
29
|
this.connection = null;
|
|
30
|
+
this.creatingConnection = false;
|
|
29
31
|
}
|
|
30
32
|
async getConnection() {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
33
|
+
return new Promise(async (resolve) => {
|
|
34
|
+
if (this.creatingConnection) {
|
|
35
|
+
this.em.on(connectionCreatedConst, resolve);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
if (this.connection !== null) {
|
|
39
|
+
return this.connection;
|
|
40
|
+
}
|
|
41
|
+
let host = process.env.RABBITMQ_SERVICE_HOST || '35.240.13.28';
|
|
42
|
+
let connection = await amqp_connection_manager_1.connect([`amqp://${host}`]);
|
|
43
|
+
this.connection = connection;
|
|
44
|
+
this.creatingConnection = false;
|
|
45
|
+
this.em.emit(connectionCreatedConst, connection);
|
|
46
|
+
resolve(connection);
|
|
47
|
+
});
|
|
38
48
|
}
|
|
39
49
|
async assertChannel() {
|
|
40
50
|
return new Promise(async (resolve, reject) => {
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -2,15 +2,18 @@ import { ConfirmChannel } from 'amqplib';
|
|
|
2
2
|
import { connect, AmqpConnectionManager, ChannelWrapper } from 'amqp-connection-manager';
|
|
3
3
|
import { EventEmitter } from 'events';
|
|
4
4
|
|
|
5
|
+
const connectionCreatedConst = 'connectionCreated';
|
|
5
6
|
class RabbitMq {
|
|
6
7
|
channel: ChannelWrapper | null;
|
|
7
8
|
connection: AmqpConnectionManager | null;
|
|
8
9
|
em: EventEmitter;
|
|
10
|
+
creatingConnection: Boolean;
|
|
9
11
|
|
|
10
12
|
constructor() {
|
|
11
13
|
this.em = new EventEmitter;
|
|
12
14
|
this.channel = null;
|
|
13
15
|
this.connection = null;
|
|
16
|
+
this.creatingConnection = false;
|
|
14
17
|
}
|
|
15
18
|
|
|
16
19
|
public ack = async (msg: any) => {
|
|
@@ -34,14 +37,23 @@ class RabbitMq {
|
|
|
34
37
|
}
|
|
35
38
|
|
|
36
39
|
async getConnection() {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
+
return new Promise<AmqpConnectionManager>(async (resolve) => {
|
|
41
|
+
if (this.creatingConnection) {
|
|
42
|
+
this.em.on(connectionCreatedConst, resolve);
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
40
45
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
46
|
+
if (this.connection !== null) {
|
|
47
|
+
return this.connection;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
let host: string = process.env.RABBITMQ_SERVICE_HOST || '35.240.13.28';
|
|
51
|
+
let connection: AmqpConnectionManager = await connect([`amqp://${host}`]);
|
|
52
|
+
this.connection = connection;
|
|
53
|
+
this.creatingConnection = false;
|
|
54
|
+
this.em.emit(connectionCreatedConst, connection);
|
|
55
|
+
resolve(connection);
|
|
56
|
+
})
|
|
45
57
|
}
|
|
46
58
|
|
|
47
59
|
async assertChannel() {
|