@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.
Files changed (2) hide show
  1. package/lib/queue/rabbit.js +60 -34
  2. package/package.json +1 -1
@@ -1,49 +1,75 @@
1
- const rabbit = require('amqplib')
1
+ const rabbit = require("amqplib");
2
2
 
3
- const Broker = async ({
4
- host = 'localhost',
5
- user = 'guest',
6
- passwd = 'guest',
7
- port = 5672,
8
- onError = () => {}
9
- }) => {
10
- const url = `amqp://${user}:${passwd}@${host}:${port}`
11
- const conn = await rabbit.connect(url)
12
- let channel = await conn.createChannel()
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
- conn.on('error', async (error) => {
15
- onError(error)
16
- })
16
+ this.onError = onError;
17
+ this.onConnect = onConnect;
18
+ }
17
19
 
18
- const acknowledge = (message = null) => {
19
- if (message) channel.ack(message)
20
- else channel.ackAll()
20
+ getChannel() {
21
+ return this.channel;
21
22
  }
22
23
 
23
- const requeue = (message = null) => {
24
- if (message) channel.nack(message)
25
- else channel.nackAll()
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
- const recover = async () => {
29
- await channel.recover()
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
- const publish = async (queue_name, message = {}) => {
33
- channel.assertQueue(queue_name, {
34
- durable: true
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
- const subscribe = async (queue_name, handler = console.log) => {
42
- channel.assertQueue(queue_name, {})
43
- channel.consume(queue_name, handler)
53
+ async recover() {
54
+ if (!this.channel) await this.connect();
55
+ await this.channel.recover();
44
56
  }
45
57
 
46
- return { acknowledge, publish, recover, requeue, subscribe, raw_instance: channel }
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();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@go-mailer/vertebra",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
4
4
  "description": "Go-Mailer vertebra",
5
5
  "main": "index.js",
6
6
  "scripts": {