@go-mailer/vertebra 1.1.6 → 2.0.1
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/_manager.js +4 -0
- package/lib/queue/rabbit.js +49 -26
- package/package.json +1 -1
package/lib/queue/_manager.js
CHANGED
package/lib/queue/rabbit.js
CHANGED
|
@@ -1,64 +1,87 @@
|
|
|
1
1
|
const rabbit = require("amqplib");
|
|
2
2
|
|
|
3
3
|
class Broker {
|
|
4
|
-
constructor({ host = "localhost", user = "guest", passwd = "guest", port = 5672, onConnect = () => {}
|
|
4
|
+
constructor({ host = "localhost", user = "guest", passwd = "guest", port = 5672, onConnect = () => {}}) {
|
|
5
5
|
this.url = `amqp://${user}:${passwd}@${host}:${port}`;
|
|
6
6
|
this.channel = null;
|
|
7
|
-
this.
|
|
7
|
+
this.onConnect = onConnect;
|
|
8
8
|
}
|
|
9
9
|
|
|
10
10
|
getChannel() {
|
|
11
11
|
return this.channel;
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
async connect(
|
|
14
|
+
async connect() {
|
|
15
15
|
try {
|
|
16
16
|
const conn = await rabbit.connect(this.url);
|
|
17
17
|
conn.on("error", async (error) => {
|
|
18
|
-
|
|
18
|
+
throw new Error(error.message)
|
|
19
19
|
});
|
|
20
20
|
|
|
21
21
|
this.channel = await conn.createChannel();
|
|
22
|
-
this.
|
|
23
|
-
onConnect(this);
|
|
22
|
+
this.onConnect(this);
|
|
24
23
|
} catch (e) {
|
|
25
24
|
console.log(`RabbitMQ connect(): ${e.message}`);
|
|
26
|
-
setTimeout(this.connect.
|
|
27
|
-
this.retries += 1;
|
|
25
|
+
setTimeout(this.connect.call(this), 5000);
|
|
28
26
|
}
|
|
29
27
|
}
|
|
30
28
|
|
|
31
29
|
async acknowledge(message = null) {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
30
|
+
try {
|
|
31
|
+
if (!this.channel) await this.connect();
|
|
32
|
+
if (message) this.channel.ack(message);
|
|
33
|
+
else this.channel.ackAll();
|
|
34
|
+
} catch (e) {
|
|
35
|
+
console.log(`RabbitMQ acknowledge(): ${e.message}`);
|
|
36
|
+
this.connect()
|
|
37
|
+
}
|
|
35
38
|
}
|
|
36
39
|
|
|
37
40
|
async requeue(message = null) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
+
try {
|
|
42
|
+
if (!this.channel) await this.connect();
|
|
43
|
+
if (message) this.channel.nack(message);
|
|
44
|
+
else this.channel.nackAll();
|
|
45
|
+
} catch (e) {
|
|
46
|
+
console.log(`RabbitMQ requeue(): ${e.message}`);
|
|
47
|
+
this.connect()
|
|
48
|
+
}
|
|
41
49
|
}
|
|
42
50
|
|
|
43
51
|
async recover() {
|
|
44
|
-
|
|
45
|
-
|
|
52
|
+
try {
|
|
53
|
+
if (!this.channel) await this.connect();
|
|
54
|
+
await this.channel.recover();
|
|
55
|
+
} catch (e) {
|
|
56
|
+
console.log(`RabbitMQ recover(): ${e.message}`);
|
|
57
|
+
this.connect()
|
|
58
|
+
}
|
|
46
59
|
}
|
|
47
60
|
|
|
48
61
|
async publish(queue_name, message = {}) {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
62
|
+
try {
|
|
63
|
+
if (!this.channel) await this.connect();
|
|
64
|
+
this.channel.assertQueue(queue_name, {
|
|
65
|
+
durable: true,
|
|
66
|
+
});
|
|
67
|
+
this.channel.sendToQueue(queue_name, Buffer.from(message), {
|
|
68
|
+
persistent: true,
|
|
69
|
+
});
|
|
70
|
+
} catch (e) {
|
|
71
|
+
console.log(`RabbitMQ publish(): ${e.message}`);
|
|
72
|
+
this.connect()
|
|
73
|
+
}
|
|
56
74
|
}
|
|
57
75
|
|
|
58
76
|
async subscribe(queue_name, handler = console.log) {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
77
|
+
try {
|
|
78
|
+
if (!this.channel) await this.connect();
|
|
79
|
+
this.channel.assertQueue(queue_name, {});
|
|
80
|
+
this.channel.consume(queue_name, handler);
|
|
81
|
+
} catch (e) {
|
|
82
|
+
console.log(`RabbitMQ subscribe(): ${e.message}`);
|
|
83
|
+
this.connect()
|
|
84
|
+
}
|
|
62
85
|
}
|
|
63
86
|
}
|
|
64
87
|
|