@go-mailer/vertebra 3.0.0 → 3.1.0

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.
@@ -18,6 +18,7 @@ module.exports = {
18
18
  CAMPAIGNS_TO_PROCESS: "campaigns_to_process",
19
19
  TRANSACTIONAL_SENT: "transactional_sent",
20
20
  PAUSED_CAMPAIGNS: 'paused_campaigns',
21
+ BOUNCED_EMAILS: 'emails_that_bounced',
21
22
 
22
23
  NOTIFICATION: "new_notification",
23
24
 
@@ -1,24 +1,25 @@
1
1
  const rabbit = require("amqplib");
2
+ const EventEmitter = require("events");
2
3
 
3
- class Connector {
4
- constructor() {
5
- this.channel = null;
6
- this.is_connecting = false;
7
- }
4
+ class AppEvent extends EventEmitter {}
5
+ const appEvent = new AppEvent();
6
+ const CHANNEL_CLOSED = "channel:closed";
7
+ const CHANNEL_OPENED = "channel:opened";
8
8
 
9
- async connect(url) {
10
- if (this.channel) return this.channel;
11
- if (this.is_connecting) return;
9
+ const delay = async (seconds = 1) => {
10
+ return await new Promise((resolve, __) => {
11
+ setTimeout(() => resolve(), seconds * 1000);
12
+ });
13
+ };
12
14
 
13
- this.is_connecting = true;
15
+ class Connector {
16
+ async connect(url) {
14
17
  const conn = await rabbit.connect(url);
15
- conn.on("error", async (error) => {
16
- throw new Error(error.message);
17
- });
18
+ const ch = await conn.createChannel();
19
+ appEvent.emit(CHANNEL_OPENED, ch);
18
20
 
19
- this.channel = await conn.createChannel();
20
- this.is_connecting = false;
21
- return this.channel;
21
+ ch.on("close", () => appEvent.emit(CHANNEL_CLOSED));
22
+ ch.on("error", () => appEvent.emit(CHANNEL_CLOSED));
22
23
  }
23
24
  }
24
25
 
@@ -26,6 +27,7 @@ class Broker {
26
27
  constructor() {
27
28
  this.channel = null;
28
29
  this.connector = null;
30
+ this.connection_config = null;
29
31
  }
30
32
 
31
33
  getChannel() {
@@ -34,31 +36,44 @@ class Broker {
34
36
 
35
37
  async connect(config = { host, user, passwd, port, onConnect }) {
36
38
  try {
37
- const { host = "localhost", user = "guest", passwd = "guest", port = 5672, onConnect = () => {} } = config;
39
+ if (config) this.connection_config = config;
40
+ const {
41
+ host = "localhost",
42
+ user = "guest",
43
+ passwd = "guest",
44
+ port = 5672,
45
+ onConnect = () => {},
46
+ } = this.connection_config;
38
47
  const url = `amqp://${user}:${passwd}@${host}:${port}`;
39
- this.channel = await this.connector.connect(url);
40
- onConnect(this);
48
+
49
+ appEvent.once(CHANNEL_OPENED, (ch) => {
50
+ this.channel = ch;
51
+ onConnect(this);
52
+ });
53
+
54
+ appEvent.once(CHANNEL_CLOSED, () => {
55
+ this.channel = null;
56
+ });
57
+
58
+ await this.connector.connect(url);
41
59
  } catch (e) {
42
60
  console.log(`RabbitMQ connection Error(): ${e.message}`);
43
- setTimeout(this.connect.bind(this, config), 5000);
61
+ await delay(5);
62
+ this.connect();
44
63
  }
45
64
  }
46
65
 
47
- async acknowledge(message = null, retries = 0) {
48
- if (retries > 10) return;
66
+ async acknowledge(message = null) {
49
67
  try {
50
- if (!this.channel) await this.connect();
51
68
  if (message) this.channel.ack(message);
52
69
  else this.channel.ackAll();
53
70
  } catch (e) {
54
71
  console.log(`RabbitMQ acknowledge(): ${e.message}`);
55
- this.acknowledge(message, retries + 1);
56
72
  }
57
73
  }
58
74
 
59
75
  async requeue(message = null) {
60
76
  try {
61
- if (!this.channel) await this.connect();
62
77
  if (message) this.channel.nack(message);
63
78
  else this.channel.nackAll();
64
79
  } catch (e) {
@@ -66,21 +81,16 @@ class Broker {
66
81
  }
67
82
  }
68
83
 
69
- async recover(retries = 0) {
70
- if (retries > 10) return;
84
+ async recover(queue_name) {
71
85
  try {
72
- if (!this.channel) await this.connect();
73
- await this.channel.recover();
86
+ await this.channel.recover(queue_name);
74
87
  } catch (e) {
75
88
  console.log(`RabbitMQ recover(): ${e.message}`);
76
- this.recover(retries + 1);
77
89
  }
78
90
  }
79
91
 
80
- async publish(queue_name, message = {}, retries = 0) {
81
- if (retries > 10) return;
92
+ async publish(queue_name, message = {}) {
82
93
  try {
83
- if (!this.channel) await this.connect();
84
94
  this.channel.assertQueue(queue_name, {
85
95
  durable: true,
86
96
  });
@@ -89,7 +99,6 @@ class Broker {
89
99
  });
90
100
  } catch (e) {
91
101
  console.log(`RabbitMQ publish(): ${e.message}`);
92
- this.publish(queue_name, message, retries + 1);
93
102
  }
94
103
  }
95
104
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@go-mailer/vertebra",
3
- "version": "3.0.0",
3
+ "version": "3.1.0",
4
4
  "description": "Go-Mailer vertebra",
5
5
  "main": "index.js",
6
6
  "scripts": {