@sera4/essentia 1.0.36 → 1.0.38

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/health/index.js CHANGED
@@ -15,10 +15,12 @@ class HealthCheckError extends Error {
15
15
  * const router = require('express').Router();
16
16
  *
17
17
  * ....
18
+ *
19
+ * const checkRedis = async () => { await cache.ping(); }
20
+ * hc.setupCheck({ name: "redis", checkResult: false, critical: true, healthFunction: checkRedis });
18
21
  * app.use(hc.defaultPath(serverConfigs.api_prefix), hc.routes(router));
19
22
  *
20
23
  */
21
-
22
24
  class HealthCheck {
23
25
  constructor(appendData = {}) {
24
26
  this.defaultResponse = {
@@ -60,15 +62,35 @@ class HealthCheck {
60
62
  return router
61
63
  }
62
64
 
65
+ // v2 version of health check
66
+ setupCheck(config) {
67
+ this.healthChecks.push(config);
68
+ }
69
+
63
70
  /* performs health checks and returns 200 if true and 500 if not */
64
71
  async checkHealth(req, res) {
65
72
  const checkFormat = pathHelper.extname(req.baseUrl);
66
73
 
67
- try {
68
- const result = await Promise.all(this.healthChecks.map(async healthCallback => {
69
- return await healthCallback();
70
- }));
74
+ const result = await Promise.all(this.healthChecks.map(async hc => {
75
+ if (typeof(hc) === "function") {
76
+ try {
77
+ return await hc();
78
+ return { "result": "success", critical: false };
79
+ } catch(e) {
80
+ return { name: "unknown", result: 'fail', status: e.status }
81
+ }
82
+ } else {
83
+ let res = { name: hc.name, critical: hc.critical }
84
+ try {
85
+ return await hc.healthFunction();
86
+ return { ...res, "result": "success" };
87
+ } catch(e) {
88
+ return { ...res, "result": "fail", status: e.status }
89
+ }
90
+ }
91
+ }));
71
92
 
93
+ try {
72
94
  let callId = ++this.lastCallId;
73
95
 
74
96
  // start the counter over
@@ -78,10 +100,18 @@ class HealthCheck {
78
100
 
79
101
  let status = this.defaultResponse.status;
80
102
 
103
+ let nonCriticalFail = false;
104
+ let criticalFail = false;
105
+
81
106
  result.some(e => {
82
107
  if (e.result.match(/fail/)) {
83
- status = 500;
84
- return true; // break
108
+ if (e.critical == false) {
109
+ nonCriticalFail = true;
110
+ } else {
111
+ criticalFail = true;
112
+ status = 500;
113
+ return true; // break
114
+ }
85
115
  }
86
116
  })
87
117
 
@@ -89,29 +119,31 @@ class HealthCheck {
89
119
  res.setHeader('check', callId)
90
120
 
91
121
  if (checkFormat === ".json") {
92
- const payload = { "check": callId }
122
+ const payload = { "check": callId, healthy: "true", message: "success" }
93
123
 
94
- if (status === 200) {
124
+ if (criticalFail) {
95
125
  res.status(status).json({
96
126
  ... payload,
97
- "healthy":true,
98
- "message":"success"
127
+ "healthy": false,
128
+ "message": "fail",
129
+ "services": [result]
99
130
  });
100
- } else {
131
+ } else if (status === 200) {
132
+ res.status(status).json(payload);
133
+ } else if (nonCriticalFailure) {
101
134
  res.status(status).json({
102
135
  ... payload,
103
- "healthy": false,
104
136
  "message": "fail",
105
137
  "services": [result]
106
138
  });
107
139
  }
108
140
  } else {
109
141
  let status_message;
110
- if (status === 200) {
111
- status_message = "success"
112
- } else {
142
+ if (criticalFail) {
113
143
  status_message = "fail"
114
- }
144
+ } else if ((status === 200) || nonCriticalFail) {
145
+ status_message = "success"
146
+ }
115
147
 
116
148
  res.status(status).send(status_message);
117
149
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sera4/essentia",
3
- "version": "1.0.36",
3
+ "version": "1.0.38",
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();