From 1d23b62a9a3567ca6ca39ecc649dafab706a7fc0 Mon Sep 17 00:00:00 2001 From: SepehrGanji Date: Sat, 24 Feb 2024 17:49:09 -0700 Subject: [PATCH] add redis healthcheck --- src/health-check.ts | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/src/health-check.ts b/src/health-check.ts index 211ec86..940986e 100644 --- a/src/health-check.ts +++ b/src/health-check.ts @@ -1,18 +1,35 @@ +import { redisClient } from "./caching"; import { DatabaseContext } from "./context/database-context"; -export const checkHealth = async(dataContext: DatabaseContext): Promise => { +export const checkHealth = async(dataContext: DatabaseContext): Promise => { /** * Things to check: - * - Database connection - * - Redis connection + * - Database connection (Done) + * - Redis connection (Done) * - Node connection * - Block watcher */ + const checks = { - db: dataContext.checkConnection + db: () => dataContext.checkConnection, + redis: async() => (await redisClient.mget("test")).length === 1, }; - // const healthy = Object.values(checks).every(() => true); - // There's no feature in apollo-server-express to return custom error messages, so we leave it empty - throw new Error(""); + const results = await Promise.all( + Object.entries(checks).map(async ([key, func]) => ({ + [key]: await func(), + })) + ); + + let isAnyFailed = false; + for(const result of results) { + for(const [key, value] of Object.entries(result)) { + if(!value) { + console.error(`🚫 Health check failed for ${key}`); + isAnyFailed = true; + } + } + } + + if(isAnyFailed) throw new Error("Health check failed"); };