@go-mailer/vertebra 2.0.2 → 3.0.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/queue/rabbit.js +49 -23
- package/package.json +1 -1
package/lib/queue/rabbit.js
CHANGED
|
@@ -1,39 +1,58 @@
|
|
|
1
1
|
const rabbit = require("amqplib");
|
|
2
2
|
|
|
3
|
+
class Connector {
|
|
4
|
+
constructor() {
|
|
5
|
+
this.channel = null;
|
|
6
|
+
this.is_connecting = false;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
async connect(url) {
|
|
10
|
+
if (this.channel) return this.channel;
|
|
11
|
+
if (this.is_connecting) return;
|
|
12
|
+
|
|
13
|
+
this.is_connecting = true;
|
|
14
|
+
const conn = await rabbit.connect(url);
|
|
15
|
+
conn.on("error", async (error) => {
|
|
16
|
+
throw new Error(error.message);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
this.channel = await conn.createChannel();
|
|
20
|
+
this.is_connecting = false;
|
|
21
|
+
return this.channel;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
3
25
|
class Broker {
|
|
4
|
-
constructor(
|
|
5
|
-
this.url = `amqp://${user}:${passwd}@${host}:${port}`;
|
|
26
|
+
constructor() {
|
|
6
27
|
this.channel = null;
|
|
7
|
-
this.
|
|
28
|
+
this.connector = null;
|
|
8
29
|
}
|
|
9
30
|
|
|
10
31
|
getChannel() {
|
|
11
32
|
return this.channel;
|
|
12
33
|
}
|
|
13
34
|
|
|
14
|
-
async connect() {
|
|
35
|
+
async connect(config = { host, user, passwd, port, onConnect }) {
|
|
15
36
|
try {
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
this.channel = await conn.createChannel();
|
|
22
|
-
this.onConnect(this);
|
|
37
|
+
const { host = "localhost", user = "guest", passwd = "guest", port = 5672, onConnect = () => {} } = config;
|
|
38
|
+
const url = `amqp://${user}:${passwd}@${host}:${port}`;
|
|
39
|
+
this.channel = await this.connector.connect(url);
|
|
40
|
+
onConnect(this);
|
|
23
41
|
} catch (e) {
|
|
24
|
-
console.log(`RabbitMQ
|
|
25
|
-
setTimeout(this.connect.bind(this), 5000);
|
|
42
|
+
console.log(`RabbitMQ connection Error(): ${e.message}`);
|
|
43
|
+
setTimeout(this.connect.bind(this, config), 5000);
|
|
26
44
|
}
|
|
27
45
|
}
|
|
28
46
|
|
|
29
|
-
async acknowledge(message = null) {
|
|
47
|
+
async acknowledge(message = null, retries = 0) {
|
|
48
|
+
if (retries > 10) return;
|
|
30
49
|
try {
|
|
31
50
|
if (!this.channel) await this.connect();
|
|
32
51
|
if (message) this.channel.ack(message);
|
|
33
52
|
else this.channel.ackAll();
|
|
34
53
|
} catch (e) {
|
|
35
54
|
console.log(`RabbitMQ acknowledge(): ${e.message}`);
|
|
36
|
-
this.
|
|
55
|
+
this.acknowledge(message, retries + 1);
|
|
37
56
|
}
|
|
38
57
|
}
|
|
39
58
|
|
|
@@ -44,21 +63,22 @@ class Broker {
|
|
|
44
63
|
else this.channel.nackAll();
|
|
45
64
|
} catch (e) {
|
|
46
65
|
console.log(`RabbitMQ requeue(): ${e.message}`);
|
|
47
|
-
this.connect()
|
|
48
66
|
}
|
|
49
67
|
}
|
|
50
68
|
|
|
51
|
-
async recover() {
|
|
69
|
+
async recover(retries = 0) {
|
|
70
|
+
if (retries > 10) return;
|
|
52
71
|
try {
|
|
53
72
|
if (!this.channel) await this.connect();
|
|
54
73
|
await this.channel.recover();
|
|
55
74
|
} catch (e) {
|
|
56
75
|
console.log(`RabbitMQ recover(): ${e.message}`);
|
|
57
|
-
this.
|
|
76
|
+
this.recover(retries + 1);
|
|
58
77
|
}
|
|
59
78
|
}
|
|
60
79
|
|
|
61
|
-
async publish(queue_name, message = {}) {
|
|
80
|
+
async publish(queue_name, message = {}, retries = 0) {
|
|
81
|
+
if (retries > 10) return;
|
|
62
82
|
try {
|
|
63
83
|
if (!this.channel) await this.connect();
|
|
64
84
|
this.channel.assertQueue(queue_name, {
|
|
@@ -69,20 +89,26 @@ class Broker {
|
|
|
69
89
|
});
|
|
70
90
|
} catch (e) {
|
|
71
91
|
console.log(`RabbitMQ publish(): ${e.message}`);
|
|
72
|
-
this.
|
|
92
|
+
this.publish(queue_name, message, retries + 1);
|
|
73
93
|
}
|
|
74
94
|
}
|
|
75
95
|
|
|
96
|
+
setConnector(connector) {
|
|
97
|
+
this.connector = connector;
|
|
98
|
+
}
|
|
99
|
+
|
|
76
100
|
async subscribe(queue_name, handler = console.log) {
|
|
77
101
|
try {
|
|
78
|
-
if (!this.channel)
|
|
102
|
+
if (!this.channel) return;
|
|
79
103
|
this.channel.assertQueue(queue_name, {});
|
|
80
104
|
this.channel.consume(queue_name, handler);
|
|
81
105
|
} catch (e) {
|
|
82
106
|
console.log(`RabbitMQ subscribe(): ${e.message}`);
|
|
83
|
-
this.connect()
|
|
84
107
|
}
|
|
85
108
|
}
|
|
86
109
|
}
|
|
87
110
|
|
|
88
|
-
|
|
111
|
+
const RMQConnector = new Connector();
|
|
112
|
+
const broker = new Broker();
|
|
113
|
+
broker.setConnector(RMQConnector);
|
|
114
|
+
module.exports = broker;
|