@go-mailer/vertebra 3.0.0 → 3.1.0
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/lib/constants/queues.js +1 -0
- package/lib/queue/rabbit.js +42 -33
- package/package.json +1 -1
package/lib/constants/queues.js
CHANGED
package/lib/queue/rabbit.js
CHANGED
|
@@ -1,24 +1,25 @@
|
|
|
1
1
|
const rabbit = require("amqplib");
|
|
2
|
+
const EventEmitter = require("events");
|
|
2
3
|
|
|
3
|
-
class
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
4
|
+
class AppEvent extends EventEmitter {}
|
|
5
|
+
const appEvent = new AppEvent();
|
|
6
|
+
const CHANNEL_CLOSED = "channel:closed";
|
|
7
|
+
const CHANNEL_OPENED = "channel:opened";
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
const delay = async (seconds = 1) => {
|
|
10
|
+
return await new Promise((resolve, __) => {
|
|
11
|
+
setTimeout(() => resolve(), seconds * 1000);
|
|
12
|
+
});
|
|
13
|
+
};
|
|
12
14
|
|
|
13
|
-
|
|
15
|
+
class Connector {
|
|
16
|
+
async connect(url) {
|
|
14
17
|
const conn = await rabbit.connect(url);
|
|
15
|
-
conn.
|
|
16
|
-
|
|
17
|
-
});
|
|
18
|
+
const ch = await conn.createChannel();
|
|
19
|
+
appEvent.emit(CHANNEL_OPENED, ch);
|
|
18
20
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
return this.channel;
|
|
21
|
+
ch.on("close", () => appEvent.emit(CHANNEL_CLOSED));
|
|
22
|
+
ch.on("error", () => appEvent.emit(CHANNEL_CLOSED));
|
|
22
23
|
}
|
|
23
24
|
}
|
|
24
25
|
|
|
@@ -26,6 +27,7 @@ class Broker {
|
|
|
26
27
|
constructor() {
|
|
27
28
|
this.channel = null;
|
|
28
29
|
this.connector = null;
|
|
30
|
+
this.connection_config = null;
|
|
29
31
|
}
|
|
30
32
|
|
|
31
33
|
getChannel() {
|
|
@@ -34,31 +36,44 @@ class Broker {
|
|
|
34
36
|
|
|
35
37
|
async connect(config = { host, user, passwd, port, onConnect }) {
|
|
36
38
|
try {
|
|
37
|
-
|
|
39
|
+
if (config) this.connection_config = config;
|
|
40
|
+
const {
|
|
41
|
+
host = "localhost",
|
|
42
|
+
user = "guest",
|
|
43
|
+
passwd = "guest",
|
|
44
|
+
port = 5672,
|
|
45
|
+
onConnect = () => {},
|
|
46
|
+
} = this.connection_config;
|
|
38
47
|
const url = `amqp://${user}:${passwd}@${host}:${port}`;
|
|
39
|
-
|
|
40
|
-
|
|
48
|
+
|
|
49
|
+
appEvent.once(CHANNEL_OPENED, (ch) => {
|
|
50
|
+
this.channel = ch;
|
|
51
|
+
onConnect(this);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
appEvent.once(CHANNEL_CLOSED, () => {
|
|
55
|
+
this.channel = null;
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
await this.connector.connect(url);
|
|
41
59
|
} catch (e) {
|
|
42
60
|
console.log(`RabbitMQ connection Error(): ${e.message}`);
|
|
43
|
-
|
|
61
|
+
await delay(5);
|
|
62
|
+
this.connect();
|
|
44
63
|
}
|
|
45
64
|
}
|
|
46
65
|
|
|
47
|
-
async acknowledge(message = null
|
|
48
|
-
if (retries > 10) return;
|
|
66
|
+
async acknowledge(message = null) {
|
|
49
67
|
try {
|
|
50
|
-
if (!this.channel) await this.connect();
|
|
51
68
|
if (message) this.channel.ack(message);
|
|
52
69
|
else this.channel.ackAll();
|
|
53
70
|
} catch (e) {
|
|
54
71
|
console.log(`RabbitMQ acknowledge(): ${e.message}`);
|
|
55
|
-
this.acknowledge(message, retries + 1);
|
|
56
72
|
}
|
|
57
73
|
}
|
|
58
74
|
|
|
59
75
|
async requeue(message = null) {
|
|
60
76
|
try {
|
|
61
|
-
if (!this.channel) await this.connect();
|
|
62
77
|
if (message) this.channel.nack(message);
|
|
63
78
|
else this.channel.nackAll();
|
|
64
79
|
} catch (e) {
|
|
@@ -66,21 +81,16 @@ class Broker {
|
|
|
66
81
|
}
|
|
67
82
|
}
|
|
68
83
|
|
|
69
|
-
async recover(
|
|
70
|
-
if (retries > 10) return;
|
|
84
|
+
async recover(queue_name) {
|
|
71
85
|
try {
|
|
72
|
-
|
|
73
|
-
await this.channel.recover();
|
|
86
|
+
await this.channel.recover(queue_name);
|
|
74
87
|
} catch (e) {
|
|
75
88
|
console.log(`RabbitMQ recover(): ${e.message}`);
|
|
76
|
-
this.recover(retries + 1);
|
|
77
89
|
}
|
|
78
90
|
}
|
|
79
91
|
|
|
80
|
-
async publish(queue_name, message = {}
|
|
81
|
-
if (retries > 10) return;
|
|
92
|
+
async publish(queue_name, message = {}) {
|
|
82
93
|
try {
|
|
83
|
-
if (!this.channel) await this.connect();
|
|
84
94
|
this.channel.assertQueue(queue_name, {
|
|
85
95
|
durable: true,
|
|
86
96
|
});
|
|
@@ -89,7 +99,6 @@ class Broker {
|
|
|
89
99
|
});
|
|
90
100
|
} catch (e) {
|
|
91
101
|
console.log(`RabbitMQ publish(): ${e.message}`);
|
|
92
|
-
this.publish(queue_name, message, retries + 1);
|
|
93
102
|
}
|
|
94
103
|
}
|
|
95
104
|
|