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