@sera4/essentia 1.1.58 → 1.1.60
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 +35 -15
- package/package.json +6 -5
- package/package.tar.gz +0 -0
package/health/index.js
CHANGED
|
@@ -2,12 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
import pathHelper from "path";
|
|
4
4
|
import logger from "./health-logger.js";
|
|
5
|
+
import morgan from "morgan";
|
|
5
6
|
|
|
6
|
-
class HealthCheckError extends Error {
|
|
7
|
-
constructor(m) {
|
|
8
|
-
super(m);
|
|
9
|
-
}
|
|
10
|
-
}
|
|
7
|
+
class HealthCheckError extends Error {}
|
|
11
8
|
|
|
12
9
|
/* Health Checking router + controller for use in all of our modules
|
|
13
10
|
* Example use: Place the following code in your routes file
|
|
@@ -36,6 +33,7 @@ class HealthCheck {
|
|
|
36
33
|
this.defaultPathRoute = `health_check\(.*\)?`
|
|
37
34
|
this.suppressLogs = false;
|
|
38
35
|
this.lastCallId = 0; // default start point/integer
|
|
36
|
+
this.pathPrefix = "";
|
|
39
37
|
this.healthChecks = [];
|
|
40
38
|
}
|
|
41
39
|
|
|
@@ -50,7 +48,14 @@ class HealthCheck {
|
|
|
50
48
|
this.healthChecks.push(fcn);
|
|
51
49
|
}
|
|
52
50
|
|
|
51
|
+
setPrefix(prefix) {
|
|
52
|
+
this.pathPrefix = prefix;
|
|
53
|
+
}
|
|
54
|
+
|
|
53
55
|
defaultPath(prefix = "") {
|
|
56
|
+
if (prefix === "") {
|
|
57
|
+
prefix = this.pathPrefix; // maybe setup manually
|
|
58
|
+
}
|
|
54
59
|
return `${prefix}/${this.defaultPathRoute}`
|
|
55
60
|
}
|
|
56
61
|
|
|
@@ -67,6 +72,14 @@ class HealthCheck {
|
|
|
67
72
|
return router
|
|
68
73
|
}
|
|
69
74
|
|
|
75
|
+
suppressHealthCheckLog(app) {
|
|
76
|
+
// Custom morgan skip function to ignore health check logging
|
|
77
|
+
console.log("skip: ", this.defaultPath())
|
|
78
|
+
app.use(morgan('combined', {
|
|
79
|
+
skip: (req) => req.path === defaultPath()
|
|
80
|
+
}));
|
|
81
|
+
}
|
|
82
|
+
|
|
70
83
|
// v2 version of health check, replaced addCheck
|
|
71
84
|
// example:
|
|
72
85
|
// const checkRedisFcn = async () => { await cache.ping().then(() => true) }
|
|
@@ -75,7 +88,7 @@ class HealthCheck {
|
|
|
75
88
|
this.healthChecks.push(config);
|
|
76
89
|
}
|
|
77
90
|
|
|
78
|
-
/* performs health checks and returns 200 if true and 500 if not */
|
|
91
|
+
/* performs health checks and returns 200 if true and 500 if not */
|
|
79
92
|
async checkHealth(req, res) {
|
|
80
93
|
const checkFormat = pathHelper.extname(req.baseUrl);
|
|
81
94
|
|
|
@@ -83,19 +96,27 @@ class HealthCheck {
|
|
|
83
96
|
if (typeof(hc) === "function") {
|
|
84
97
|
try {
|
|
85
98
|
return await hc();
|
|
86
|
-
return { "result": "success", critical: false };
|
|
87
99
|
} catch(e) {
|
|
88
100
|
return { name: "unknown", result: 'fail', status: e.status }
|
|
89
101
|
}
|
|
90
102
|
} else {
|
|
91
103
|
let res = { name: hc.name, critical: hc.critical }
|
|
92
104
|
try {
|
|
93
|
-
await hc.healthFunction();
|
|
94
|
-
|
|
105
|
+
const result = await hc.healthFunction();
|
|
106
|
+
if (typeof(result) === "boolean") {
|
|
107
|
+
if (result === false) {
|
|
108
|
+
Object.assign(res, {"result": "fail" })
|
|
109
|
+
} else {
|
|
110
|
+
Object.assign(res, {"result": "success" })
|
|
111
|
+
}
|
|
112
|
+
} else {
|
|
113
|
+
Object.assign(res, {"result": "success" })
|
|
114
|
+
}
|
|
115
|
+
return res;
|
|
95
116
|
} catch(e) {
|
|
96
117
|
const appendStatus = e.status ? ` (${e.status})` : ""
|
|
97
118
|
logger.warn(`${hc.name} service failed${appendStatus}` )
|
|
98
|
-
return { ...res, "result": "fail", status: e.status }
|
|
119
|
+
return { ...res, "result": "fail", status: e.status }
|
|
99
120
|
}
|
|
100
121
|
}
|
|
101
122
|
}));
|
|
@@ -115,7 +136,7 @@ class HealthCheck {
|
|
|
115
136
|
|
|
116
137
|
result.some(e => {
|
|
117
138
|
if (e.result.match(/fail/)) {
|
|
118
|
-
if (e.critical
|
|
139
|
+
if (e.critical === false) {
|
|
119
140
|
nonCriticalFail = true;
|
|
120
141
|
} else {
|
|
121
142
|
criticalFail = true;
|
|
@@ -153,16 +174,15 @@ class HealthCheck {
|
|
|
153
174
|
status_message = "fail"
|
|
154
175
|
} else if ((status === 200) || nonCriticalFail) {
|
|
155
176
|
status_message = "success"
|
|
156
|
-
}
|
|
177
|
+
}
|
|
157
178
|
|
|
158
179
|
res.status(status).send(status_message);
|
|
159
180
|
}
|
|
160
181
|
} catch(error) {
|
|
161
|
-
console.
|
|
182
|
+
console.warn("error: ", error)
|
|
162
183
|
res.status(500).send();
|
|
163
184
|
}
|
|
164
185
|
}
|
|
165
186
|
}
|
|
166
187
|
|
|
167
|
-
|
|
168
|
-
export { HealthCheck };
|
|
188
|
+
export { HealthCheck };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sera4/essentia",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.60",
|
|
4
4
|
"description": "A library of utilities for Teleporte Web Services",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -20,21 +20,22 @@
|
|
|
20
20
|
"devDependencies": {
|
|
21
21
|
"chai": "^4.3.4",
|
|
22
22
|
"express": "^4.17.1",
|
|
23
|
-
"mocha": "^
|
|
24
|
-
"mocha-junit-reporter": "^
|
|
25
|
-
"node-mocks-http": "^1.
|
|
23
|
+
"mocha": "^11.1.0",
|
|
24
|
+
"mocha-junit-reporter": "^2.2.1",
|
|
25
|
+
"node-mocks-http": "^1.16.2",
|
|
26
26
|
"should": "^13.2.3",
|
|
27
27
|
"sinon": "^5.1.1"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
30
|
"amqplib": "^0.8.0",
|
|
31
31
|
"async": "^3.2.2",
|
|
32
|
-
"axios": "^1.
|
|
32
|
+
"axios": "^1.7.7",
|
|
33
33
|
"deep-diff": "^1.0.2",
|
|
34
34
|
"diff": "^5.0.0",
|
|
35
35
|
"fast-safe-stringify": "^2.0.7",
|
|
36
36
|
"git-rev": "^0.2.1",
|
|
37
37
|
"lodash": "^4.17.21",
|
|
38
|
+
"morgan": "^1.10.0",
|
|
38
39
|
"url-parse": "^1.5.10",
|
|
39
40
|
"uuid": "^9.0.0",
|
|
40
41
|
"winston": "^3.3.3",
|
package/package.tar.gz
CHANGED
|
Binary file
|