From 83c9fbaea42afa61d50ef33446c07766820b8eea Mon Sep 17 00:00:00 2001 From: Munkh-Orgil Date: Wed, 24 Jun 2020 15:24:07 +0800 Subject: [PATCH 1/3] add health check status for services --- src/db/connection.ts | 15 +++++++++++++++ src/index.ts | 29 +++++++++++++++++++++++++---- src/messageBroker.ts | 20 +++++++++++++++++++- src/redisClient.ts | 16 ++++++++++++++++ 4 files changed, 75 insertions(+), 5 deletions(-) diff --git a/src/db/connection.ts b/src/db/connection.ts index 63ef718ab..d413e193c 100644 --- a/src/db/connection.ts +++ b/src/db/connection.ts @@ -46,6 +46,21 @@ export function disconnect() { return mongoose.connection.close(); } +/** + * Health check status + */ +export const mongoStatus = () => { + return new Promise((resolve, reject) => { + mongoose.connection.db.admin().ping((err, result) => { + if (err) { + return reject(err); + } + + return resolve(result); + }); + }); +}; + const schema = makeExecutableSchema({ typeDefs, resolvers, diff --git a/src/index.ts b/src/index.ts index 184d8f674..3852a766c 100755 --- a/src/index.ts +++ b/src/index.ts @@ -21,14 +21,14 @@ import { readFileRequest, registerOnboardHistory, } from './data/utils'; -import { connect } from './db/connection'; +import { connect, mongoStatus } from './db/connection'; import { debugBase, debugExternalApi, debugInit } from './debuggers'; import { identifyCustomer, trackCustomEvent, trackViewPageEvent, updateCustomerProperty } from './events'; -import { initConsumer } from './messageBroker'; +import { initConsumer, rabbitMQStatus } from './messageBroker'; import { importer, uploader } from './middlewares/fileMiddleware'; import userMiddleware from './middlewares/userMiddleware'; import widgetsMiddleware from './middlewares/widgetsMiddleware'; -import { initRedis } from './redisClient'; +import { initRedis, redisStatus } from './redisClient'; import init from './startup'; // load environment variables @@ -126,7 +126,28 @@ app.get('/download-template', async (req: any, res) => { }); // for health check -app.get('/status', async (_req, res) => { +app.get('/status', async (_req, res, next) => { + try { + await mongoStatus(); + } catch (e) { + debugBase('MongoDB is not running'); + next(e); + } + + try { + await redisStatus(); + } catch (e) { + debugBase('Redis is not running'); + next(e); + } + + try { + await rabbitMQStatus(); + } catch (e) { + debugBase('RabbitMQ is not running'); + next(e); + } + res.end('ok'); }); diff --git a/src/messageBroker.ts b/src/messageBroker.ts index 719a132b3..e2ef231a8 100644 --- a/src/messageBroker.ts +++ b/src/messageBroker.ts @@ -1,4 +1,4 @@ -import * as amqplib from 'amqplib'; +import amqplib from 'amqplib'; import * as dotenv from 'dotenv'; import * as uuid from 'uuid'; import { @@ -162,3 +162,21 @@ export const initConsumer = async () => { } }); }; + +/** + * Health check rabbitMQ + */ +export const rabbitMQStatus = async () => { + return new Promise((resolve, reject) => { + // tslint:disable-next-line:no-submodule-imports + import('amqplib/callback_api').then(amqp => { + amqp.connect(RABBITMQ_HOST, error => { + if (error) { + return reject(error); + } + + return resolve('ok'); + }); + }); + }); +}; diff --git a/src/redisClient.ts b/src/redisClient.ts index c13d45e77..88d330624 100644 --- a/src/redisClient.ts +++ b/src/redisClient.ts @@ -129,3 +129,19 @@ export const removeFromArray = (setKey: string, setMember: string) => { }); }); }; + +/** + * Health check status + * retryStrategy - get response immediately + */ +export const redisStatus = () => { + return new Promise((resolve, reject) => { + client.ping((error, result) => { + if (error) { + return reject(error); + } + + return resolve(result); + }); + }); +}; From 23729cadfdf021561b653d7a722fe60972080dcc Mon Sep 17 00:00:00 2001 From: Munkh-Orgil Date: Wed, 24 Jun 2020 15:25:47 +0800 Subject: [PATCH 2/3] fix promise --- src/messageBroker.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/messageBroker.ts b/src/messageBroker.ts index e2ef231a8..07572b302 100644 --- a/src/messageBroker.ts +++ b/src/messageBroker.ts @@ -170,13 +170,15 @@ export const rabbitMQStatus = async () => { return new Promise((resolve, reject) => { // tslint:disable-next-line:no-submodule-imports import('amqplib/callback_api').then(amqp => { - amqp.connect(RABBITMQ_HOST, error => { - if (error) { - return reject(error); - } + amqp + .connect(RABBITMQ_HOST, error => { + if (error) { + return reject(error); + } - return resolve('ok'); - }); + return resolve('ok'); + }) + .catch(e => reject(e)); }); }); }; From 25e03132eb534c37ef525216be907d985add10e3 Mon Sep 17 00:00:00 2001 From: Munkh-Orgil Date: Wed, 24 Jun 2020 15:26:43 +0800 Subject: [PATCH 3/3] fix catch in rabbitMQ --- src/messageBroker.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/messageBroker.ts b/src/messageBroker.ts index 07572b302..34be64d9d 100644 --- a/src/messageBroker.ts +++ b/src/messageBroker.ts @@ -169,16 +169,16 @@ export const initConsumer = async () => { export const rabbitMQStatus = async () => { return new Promise((resolve, reject) => { // tslint:disable-next-line:no-submodule-imports - import('amqplib/callback_api').then(amqp => { - amqp - .connect(RABBITMQ_HOST, error => { + import('amqplib/callback_api') + .then(amqp => { + amqp.connect(RABBITMQ_HOST, error => { if (error) { return reject(error); } return resolve('ok'); - }) - .catch(e => reject(e)); - }); + }); + }) + .catch(e => reject(e)); }); };