Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added db connection test to health check #131

Merged
merged 2 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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'",
Expand Down
2 changes: 1 addition & 1 deletion src/server/bootstrap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
}
16 changes: 12 additions & 4 deletions src/server/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.sendStatus(200);
};
Loading