From bab372ff4dbfb511cbd995e81373f7a252bef167 Mon Sep 17 00:00:00 2001 From: Amin Latifi Date: Mon, 4 Nov 2024 11:56:58 +0330 Subject: [PATCH 1/4] 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 dea77e062431280a7f291d54ec658f23b06b638a Mon Sep 17 00:00:00 2001 From: geleeroyale Date: Mon, 4 Nov 2024 11:14:18 +0100 Subject: [PATCH 2/4] trigger staging-pipeline --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index a66c8ab8e..5f8c1b3fe 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,6 @@ - Production - [![CI/CD](https://github.com/Giveth/impact-graph/actions/workflows/master-pipeline.yml/badge.svg)](https://github.com/Giveth/impact-graph/actions/workflows/master-pipeline.yml) --- - ImpactQL is a GraphQL server, that enables rapid development of serverless impact project applications. It does this by taking care of the persistance of impact project data. [Installation](#installation) From 3361ab98d34b9261bc5974be2dc4c149dae473b7 Mon Sep 17 00:00:00 2001 From: Amin Latifi Date: Mon, 4 Nov 2024 14:04:09 +0330 Subject: [PATCH 3/4] 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); }; From d3c104d3f5a4aa21a74c0065724bfb0cd782d81e Mon Sep 17 00:00:00 2001 From: Amin Latifi Date: Mon, 4 Nov 2024 18:22:35 +0330 Subject: [PATCH 4/4] Run migrations only by the job server --- src/config.ts | 3 +++ src/orm.ts | 3 ++- src/server/bootstrap.ts | 5 +---- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/config.ts b/src/config.ts index 2e7e6a98c..b02eb2835 100644 --- a/src/config.ts +++ b/src/config.ts @@ -121,4 +121,7 @@ class Config { const config = new Config(process.env); +export const isGraphQlMode = config.get('GRAPHQL_MODE') === 'true'; +export const isJobMode = config.get('JOB_MODE') === 'true'; + export default config; diff --git a/src/orm.ts b/src/orm.ts index 9625e97fd..c23c44dbd 100644 --- a/src/orm.ts +++ b/src/orm.ts @@ -1,6 +1,6 @@ import { DataSource } from 'typeorm'; import { PostgresConnectionCredentialsOptions } from 'typeorm/driver/postgres/PostgresConnectionCredentialsOptions'; -import config from './config'; +import config, { isJobMode } from './config'; import { CronJob } from './entities/CronJob'; import { getEntities } from './entities/entities'; import { redisConfig } from './redis'; @@ -57,6 +57,7 @@ export class AppDataSource { }, }, poolSize, + migrationsRun: isJobMode, extra: { maxWaitingClients: 10, evictionRunIntervalMillis: 500, diff --git a/src/server/bootstrap.ts b/src/server/bootstrap.ts index a86a54cd9..a99d77d2c 100644 --- a/src/server/bootstrap.ts +++ b/src/server/bootstrap.ts @@ -18,7 +18,7 @@ import bodyParser from 'body-parser'; import graphqlUploadExpress from 'graphql-upload/graphqlUploadExpress.js'; import { ApolloServerPluginLandingPageDisabled } from '@apollo/server/plugin/disabled'; import { ApolloServerErrorCode } from '@apollo/server/errors'; -import config from '../config'; +import config, { isGraphQlMode, isJobMode } from '../config'; import { handleStripeWebhook } from '../utils/stripe'; import createSchema from './createSchema'; import SentryLogger from '../sentryLogger'; @@ -83,9 +83,6 @@ export async function bootstrap() { try { logger.debug('bootstrap() has been called', new Date()); - const isGraphQlMode = config.get('GRAPHQL_MODE') === 'true'; - const isJobMode = config.get('JOB_MODE') === 'true'; - logger.info('isGraphQlMode: ', isGraphQlMode); logger.info('isJobMode: ', isJobMode);