@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.
@@ -28,6 +28,10 @@ class QueueManager {
28
28
  max_size
29
29
  })
30
30
  }
31
+
32
+ size (queue_name) {
33
+ return this.queues.get(queue_name).current_size
34
+ }
31
35
  }
32
36
 
33
37
  module.exports = new QueueManager()
@@ -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.retries = 0;
7
+ this.onConnect = onConnect;
8
8
  }
9
9
 
10
10
  getChannel() {
11
11
  return this.channel;
12
12
  }
13
13
 
14
- async connect({ onConnect = () => {}, onError = () => {} }) {
14
+ async connect() {
15
15
  try {
16
16
  const conn = await rabbit.connect(this.url);
17
17
  conn.on("error", async (error) => {
18
- onError(error);
18
+ throw new Error(error.message)
19
19
  });
20
20
 
21
21
  this.channel = await conn.createChannel();
22
- this.retries = 0;
23
- onConnect(this);
22
+ this.onConnect(this);
24
23
  } catch (e) {
25
24
  console.log(`RabbitMQ connect(): ${e.message}`);
26
- setTimeout(this.connect.bind(this), this.retries * 1000);
27
- this.retries += 1;
25
+ setTimeout(this.connect.call(this), 5000);
28
26
  }
29
27
  }
30
28
 
31
29
  async acknowledge(message = null) {
32
- if (!this.channel) await this.connect();
33
- if (message) this.channel.ack(message);
34
- else this.channel.ackAll();
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
- if (!this.channel) await this.connect();
39
- if (message) this.channel.nack(message);
40
- else this.channel.nackAll();
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
- if (!this.channel) await this.connect();
45
- await this.channel.recover();
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
- if (!this.channel) await this.connect();
50
- this.channel.assertQueue(queue_name, {
51
- durable: true,
52
- });
53
- this.channel.sendToQueue(queue_name, Buffer.from(message), {
54
- persistent: true,
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
- if (!this.channel) await this.connect();
60
- this.channel.assertQueue(queue_name, {});
61
- this.channel.consume(queue_name, handler);
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
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@go-mailer/vertebra",
3
- "version": "1.1.6",
3
+ "version": "2.0.1",
4
4
  "description": "Go-Mailer vertebra",
5
5
  "main": "index.js",
6
6
  "scripts": {