From bab372ff4dbfb511cbd995e81373f7a252bef167 Mon Sep 17 00:00:00 2001 From: Amin Latifi Date: Mon, 4 Nov 2024 11:56:58 +0330 Subject: [PATCH 1/2] Added db connection test to health check --- package.json | 1 + src/server/bootstrap.ts | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index f0efcc5c7..9cd290d0e 100644 --- a/package.json +++ b/package.json @@ -186,6 +186,7 @@ "test:bootstrap": "NODE_ENV=test mocha ./test/pre-test-scripts.ts ./src/server/bootstrap.test.ts", "test:utils": "NODE_ENV=test mocha ./src/utils/utils.test.ts", "test:inverterScript": "NODE_ENV=test mocha ./test/pre-test-scripts.ts ./src/scripts/syncDataWithInverter.test.ts", + "test:healthCheck": "NODE_ENV=test mocha ./test/pre-test-scripts.ts ./src/server/bootstrap.test.ts", "start": "NODE_ENV=development ts-node-dev --project ./tsconfig.json --respawn ./src/index.ts", "start:test": "NODE_ENV=development ts-node-dev --project ./tsconfig.json --respawn ./test.ts", "serve": "pm2 startOrRestart ecosystem.config.js --node-args='--max-old-space-size=8192'", diff --git a/src/server/bootstrap.ts b/src/server/bootstrap.ts index e65be7806..4add41107 100644 --- a/src/server/bootstrap.ts +++ b/src/server/bootstrap.ts @@ -7,7 +7,7 @@ import { ApolloServer } from '@apollo/server'; import { expressMiddleware } from '@apollo/server/express4'; import { ApolloServerPluginSchemaReporting } from '@apollo/server/plugin/schemaReporting'; import { ApolloServerPluginLandingPageGraphQLPlayground } from '@apollo/server-plugin-landing-page-graphql-playground'; -import express, { json, Request } from 'express'; +import express, { json, Request, RequestHandler } from 'express'; import { Container } from 'typedi'; import { Resource } from '@adminjs/typeorm'; import { validate } from 'class-validator'; @@ -424,9 +424,7 @@ export async function bootstrap() { bodyParser.raw({ type: 'application/json' }), handleStripeWebhook, ); - app.get('/health', (_req, res) => { - res.send('Hi every thing seems ok'); - }); + app.get('/health', healthCheck); app.post('/transak_webhook', webhookHandler); const httpServer = http.createServer(app); @@ -472,3 +470,13 @@ async function setPgTrgmParameters(ds: DataSource) { `SET pg_trgm.word_similarity_threshold TO ${similarityThreshold};`, ); } + +const healthCheck: RequestHandler = async (_req, res) => { + const ds = AppDataSource.getDataSource(); + const result = (await ds.query('select version()')) || []; + const version = result[0]?.version || ''; + if (!version.startsWith('PostgreSQL')) { + throw new Error('Unexpected db result'); + } + res.send('Hi every thing seems ok'); +}; From 3361ab98d34b9261bc5974be2dc4c149dae473b7 Mon Sep 17 00:00:00 2001 From: Amin Latifi Date: Mon, 4 Nov 2024 14:04:09 +0330 Subject: [PATCH 2/2] Changed heal status return value to Okay --- src/server/bootstrap.test.ts | 2 +- src/server/bootstrap.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/server/bootstrap.test.ts b/src/server/bootstrap.test.ts index 3b86b50be..8188e1b1c 100644 --- a/src/server/bootstrap.test.ts +++ b/src/server/bootstrap.test.ts @@ -6,6 +6,6 @@ describe('health check test case', healthCheckTestCases); function healthCheckTestCases() { it('should return empty array for now startTimestamp', async () => { const result = await axios.get('http://localhost:4000/health'); - assert.equal(result.data, 'Hi every thing seems ok'); + assert.equal(result.data, 'OK'); }); } diff --git a/src/server/bootstrap.ts b/src/server/bootstrap.ts index 4add41107..a86a54cd9 100644 --- a/src/server/bootstrap.ts +++ b/src/server/bootstrap.ts @@ -478,5 +478,5 @@ const healthCheck: RequestHandler = async (_req, res) => { if (!version.startsWith('PostgreSQL')) { throw new Error('Unexpected db result'); } - res.send('Hi every thing seems ok'); + res.sendStatus(200); };