@sera4/essentia 1.0.37 → 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 +49 -17
- package/package.json +1 -1
- package/package.tar.gz +0 -0
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
|
-
|
|
68
|
-
|
|
69
|
-
|
|
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
|
-
|
|
84
|
-
|
|
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 (
|
|
124
|
+
if (criticalFail) {
|
|
95
125
|
res.status(status).json({
|
|
96
126
|
... payload,
|
|
97
|
-
"healthy":
|
|
98
|
-
"message":"
|
|
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 (
|
|
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
package/package.tar.gz
CHANGED
|
Binary file
|