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