forked from clubdrei/docker-localhost-tunnel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
healthcheck.js
39 lines (34 loc) · 1.27 KB
/
healthcheck.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const { Console } = require('console');
const fs = require('fs');
const { buildConfig } = require('./lib/config');
// Write health check console output to docker logs (https://stackoverflow.com/a/68929795)
const out = fs.createWriteStream('/proc/1/fd/1');
const console = new Console(out);
if (!process.env.LOCALTUNNEL_HEALTHCHECK_PATH) {
// No healthcheck path given, stop healthcheck
process.exit(0);
}
const config = buildConfig();
const healthcheckUrl = `${config.clientUrl}${process.env.LOCALTUNNEL_HEALTHCHECK_PATH}`;
fetch(healthcheckUrl)
.then((response) => {
if (!response.ok) {
console.error(`[healthcheck.js] Failed with status code ${response.status}. Url: ${healthcheckUrl}`);
stopContainer();
process.exit(1);
}
console.debug(`[healthcheck.js] Success. Url: ${healthcheckUrl}`);
process.exit(0);
})
.catch((error) => {
console.error(`[healthcheck.js] Failed with exception. Url: ${healthcheckUrl}`, error);
stopContainer();
process.exit(1);
});
/**
* Docker doesn't restart unhealthy containers, so we just kill the current container & let docker handle the restart
* https://github.com/moby/moby/issues/28400
*/
function stopContainer() {
process.kill(1);
}