Skip to content

Commit 7688317

Browse files
authored
Use terminus for liveness/readiness probes & sigterms (#28)
* Use terminus for liveness/readiness & SIGTERMS * WIP
1 parent 83b71c3 commit 7688317

File tree

3 files changed

+46
-11
lines changed

3 files changed

+46
-11
lines changed

app.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import logMaker from './log';
1313

1414
const log = logMaker('app.js', { level: process.env.DEBUG_LEVEL || 'info' });
1515

16+
const { createTerminus } = require('@godaddy/terminus');
17+
1618
function maybeReplaceWithContentsOfFile(obj, field) {
1719
const potentialFile = obj[field];
1820
if (potentialFile != null && fs.existsSync(potentialFile)) {
@@ -54,9 +56,6 @@ app.use(bodyParser.urlencoded({
5456
extended: false,
5557
}));
5658

57-
// The Health Check
58-
app.use('/export/status', require('express-healthcheck')());
59-
6059
app.get('/export/:userid', async (req, res) => {
6160
// Set the timeout for the request. Make it 10 seconds longer than
6261
// our configured timeout to give the service time to cancel the API data
@@ -155,8 +154,29 @@ app.get('/export/:userid', async (req, res) => {
155154
}
156155
});
157156

157+
function beforeShutdown() {
158+
return new Promise((resolve) => {
159+
// Ensure that the export request can time out
160+
// without being forcefully killed
161+
setTimeout(resolve, config.exportTimeout + 10000);
162+
});
163+
}
164+
165+
function healthCheck() {
166+
return Promise.resolve();
167+
}
168+
169+
const options = {
170+
healthChecks: {
171+
'/export/status': healthCheck,
172+
},
173+
beforeShutdown,
174+
};
175+
158176
if (config.httpPort) {
159-
app.server = http.createServer(app).listen(config.httpPort, () => {
177+
const server = http.createServer(app);
178+
createTerminus(server, options);
179+
server.listen(config.httpPort, () => {
160180
log.info(`Listening for HTTP on ${config.httpPort}`);
161181
});
162182
}
@@ -166,7 +186,9 @@ if (config.httpsPort) {
166186
log.error('SSL endpoint is enabled, but no valid config was found. Exiting.');
167187
process.exit(1);
168188
} else {
169-
https.createServer(config.httpsConfig, app).listen(config.httpsPort, () => {
189+
const server = https.createServer(config.httpsConfig, app);
190+
createTerminus(server, options);
191+
server.listen(config.httpsPort, () => {
170192
log.info(`Listening for HTTPS on ${config.httpsPort}`);
171193
});
172194
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
"author": "Lennart Goedhart <lennart@tidepool.org>",
1717
"private": false,
1818
"dependencies": {
19+
"@godaddy/terminus": "^4.4.1",
1920
"@tidepool/data-tools": "2.4.0",
2021
"axios": "0.19.2",
2122
"body-parser": "1.19.0",
2223
"bunyan": "1.8.12",
2324
"esm": "3.2.25",
2425
"express": "4.17.1",
25-
"express-healthcheck": "0.1.0",
2626
"lodash": "4.17.15",
2727
"query-string": "6.13.1"
2828
},

yarn.lock

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,14 @@
125125
lodash "^4.17.13"
126126
to-fast-properties "^2.0.0"
127127

128+
"@godaddy/terminus@^4.4.1":
129+
version "4.4.1"
130+
resolved "https://registry.yarnpkg.com/@godaddy/terminus/-/terminus-4.4.1.tgz#0a54ba1363452a8199f46aed3f7931d55287c6b0"
131+
integrity sha512-ZPwsG7xN+B2bvnEu6+o0rBPAFbXmm6Bk/RAR4b/nhjNOSJtbVFYTd+JSaJYL/jyESqcTcAyzlWO0dmbT4YsckQ==
132+
dependencies:
133+
es6-promisify "^6.0.2"
134+
stoppable "^1.1.0"
135+
128136
"@tidepool/data-tools@2.4.0":
129137
version "2.4.0"
130138
resolved "https://registry.npmjs.org/@tidepool/data-tools/-/data-tools-2.4.0.tgz#029242563445e72dd844e71f63b4fe41ec846de2"
@@ -788,6 +796,11 @@ es-to-primitive@^1.2.1:
788796
is-date-object "^1.0.1"
789797
is-symbol "^1.0.2"
790798

799+
es6-promisify@^6.0.2:
800+
version "6.1.1"
801+
resolved "https://registry.yarnpkg.com/es6-promisify/-/es6-promisify-6.1.1.tgz#46837651b7b06bf6fff893d03f29393668d01621"
802+
integrity sha512-HBL8I3mIki5C1Cc9QjKUenHtnG0A5/xA8Q/AllRcfiwl2CZFXGK7ddBiCoRwAix4i2KxcQfjtIVcrVbB3vbmwg==
803+
791804
escape-html@~1.0.3:
792805
version "1.0.3"
793806
resolved "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz#0258eae4d3d0c0974de1c169188ef0051d1d1988"
@@ -1059,11 +1072,6 @@ exceljs@3.8.0:
10591072
unzipper "^0.9.12"
10601073
uuid "^3.3.3"
10611074

1062-
express-healthcheck@0.1.0:
1063-
version "0.1.0"
1064-
resolved "https://registry.npmjs.org/express-healthcheck/-/express-healthcheck-0.1.0.tgz#cabec78129c4cb90cd7fb894dfae21b82e27cb07"
1065-
integrity sha1-yr7HgSnEy5DNf7iU364huC4nywc=
1066-
10671075
express@4.17.1:
10681076
version "4.17.1"
10691077
resolved "https://registry.npmjs.org/express/-/express-4.17.1.tgz#4491fc38605cf51f8629d39c2b5d026f98a4c134"
@@ -2461,6 +2469,11 @@ sprintf-js@~1.0.2:
24612469
resolved "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz#161c7dac177659fd9811f43771fa99381478628c"
24622470
integrity sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow=
24632471

2472+
stoppable@^1.1.0:
2473+
version "1.1.0"
2474+
resolved "https://registry.yarnpkg.com/stoppable/-/stoppable-1.1.0.tgz#32da568e83ea488b08e4d7ea2c3bcc9d75015d5b"
2475+
integrity sha512-KXDYZ9dszj6bzvnEMRYvxgeTHU74QBFL54XKtP3nyMuJ81CFYtABZ3bAzL2EdFUaEwJOBOgENyFj3R7oTzDyyw==
2476+
24642477
stream-combiner@~0.0.4:
24652478
version "0.0.4"
24662479
resolved "https://registry.npmjs.org/stream-combiner/-/stream-combiner-0.0.4.tgz#4d5e433c185261dde623ca3f44c586bcf5c4ad14"

0 commit comments

Comments
 (0)