From b1bf8f3357bbd620e63c35c34013262b65b77cdd Mon Sep 17 00:00:00 2001 From: John Rassa Date: Tue, 13 Feb 2024 13:59:12 -0500 Subject: [PATCH] feat: add healthcheck script for docker use --- src/healthcheck.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/healthcheck.ts diff --git a/src/healthcheck.ts b/src/healthcheck.ts new file mode 100644 index 00000000..07e2867f --- /dev/null +++ b/src/healthcheck.ts @@ -0,0 +1,42 @@ +'use strict'; + +/* eslint-disable no-console */ +import http from 'node:http'; + +import config from 'config'; + +const options = { + hostname: 'localhost', + port: config.get('port'), + path: '/actuator/health', + method: 'GET' +}; + +http + .request(options, (res) => { + let body = ''; + + res.on('data', (chunk) => { + body += chunk; + }); + + res.on('end', () => { + try { + const response = JSON.parse(body); + if (response.status === 'UP') { + process.exit(0); + } + + console.log('Unhealthy response received: ', body); + process.exit(1); + } catch (err) { + console.log('Error parsing JSON response body: ', err); + process.exit(1); + } + }); + }) + .on('error', (err) => { + console.log('Error: ', err); + process.exit(1); + }) + .end();