@sera4/essentia 1.0.36 → 1.0.37

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sera4/essentia",
3
- "version": "1.0.36",
3
+ "version": "1.0.37",
4
4
  "description": "A library of utilities for Teleporte Web Services",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/package.tar.gz CHANGED
Binary file
package/queue/index.js CHANGED
@@ -29,6 +29,7 @@ class S4Queue {
29
29
  */
30
30
  async testConnection() {
31
31
  if (!this.isConnected()) {
32
+ this.connectionTestChannel = null;
32
33
  throw("queue service not connected")
33
34
  }
34
35
 
@@ -72,12 +73,16 @@ class S4Queue {
72
73
  amqp.connect(options.connectionUrl, socketOptions, (err, conn) => {
73
74
 
74
75
  if (err) {
75
- return reject(err);
76
+ logger.error("Error while connecting", err)
77
+ logger.error("Attempting again in", this.retry, "seconds");
78
+ return setTimeout(() => this.openConnection(options), this.retry * 1000);
76
79
  }
77
80
 
78
81
  this.connection = conn;
79
82
  if (!this.connection) {
80
83
  return reject(new Error("Failed to obtain connection"));
84
+ } else {
85
+ this.updatePubsAndSubsConnectionRef(conn);
81
86
  }
82
87
 
83
88
  this.connection.on("error", (err) => {
@@ -91,8 +96,8 @@ class S4Queue {
91
96
  logger.error("connection closed");
92
97
  this.connection = null;
93
98
  if (err) {
94
- logger.debug("connection closed due to error:", err);
95
- logger.debug("Reconnecting in", this.retry, "seconds");
99
+ logger.error("connection closed due to error:", err);
100
+ logger.error("Reconnecting in", this.retry, "seconds");
96
101
  setTimeout(() => this.openConnection(options), this.retry * 1000);
97
102
  } else {
98
103
  // If there was no error, it is us who closed
@@ -327,6 +332,29 @@ class S4Queue {
327
332
  }
328
333
  });
329
334
  }
335
+
336
+ /**
337
+ * Updates the connection reference to all the previously
338
+ * registered publishers and subscribers.
339
+ * After we successfully establish a connection and register pubs/subs,
340
+ * it's possible for the connection to break. This object will try
341
+ * to re-establish a new connection every 3 seconds (or whatever configuration is set)
342
+ * and if a new connection is established, we need to pass that connection to
343
+ * all our pubs/subs, otherwise they'll be operating with old/stale references
344
+ * @param {Object} connRef a new connection reference to the AMQP service
345
+ */
346
+ updatePubsAndSubsConnectionRef(connRef) {
347
+ Object.keys(this.pubs).forEach(k => {
348
+ logger.debug(`Updating publisher ${k} with a new connection object`);
349
+ const p = this.pubs[k];
350
+ p.init(connRef);
351
+ });
352
+ Object.keys(this.subs).forEach(k => {
353
+ logger.debug(`Updating subscriber ${k} with a new connection object`);
354
+ const s = this.subs[k];
355
+ s.init(connRef);
356
+ });
357
+ }
330
358
  }
331
359
 
332
360
  module.exports = new S4Queue();