@sera4/essentia 1.0.35 → 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.35",
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
@@ -289,6 +294,28 @@ class S4Queue {
289
294
  return await p.publish({key: topic, content});
290
295
  }
291
296
 
297
+ /**
298
+ * Binds an exchange to a queue
299
+ * @param {String} exchange the name of the exchange.
300
+ * @param {String} queue the name of the queue.
301
+ * @param {pattern} (optional) pattern for routing key
302
+ */
303
+ async bindQueueToExchange(exchange, queue, pattern) {
304
+ const p = this.pubs[exchange];
305
+
306
+ if (!p) {
307
+ throw(new Error(`No PUB for ${exchange} found`));
308
+ }
309
+
310
+ if (!queue) {
311
+ throw(new Error(`Binding a queue requires a queue name`));
312
+ }
313
+
314
+ return await p.bindQueue(queue, pattern)
315
+ }
316
+
317
+
318
+
292
319
  /**
293
320
  * Adds a listener for a specific key.
294
321
  * If a subscriber that was regitered was found
@@ -305,6 +332,29 @@ class S4Queue {
305
332
  }
306
333
  });
307
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
+ }
308
358
  }
309
359
 
310
360
  module.exports = new S4Queue();
@@ -80,8 +80,12 @@ class Publisher {
80
80
 
81
81
  return parcel.tracking_id;
82
82
  }
83
+
84
+ async bindQueue(queue, pattern, args) {
85
+ await this.channel.bindQueue(queue, this.exchange, pattern, args)
86
+ }
83
87
  }
84
88
 
85
89
  module.exports = {
86
90
  Publisher
87
- }
91
+ }