Skip to content

Commit

Permalink
add redis healthcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
SepehrGanji committed Feb 25, 2024
1 parent 11d04de commit 1d23b62
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions src/health-check.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
import { redisClient } from "./caching";
import { DatabaseContext } from "./context/database-context";

export const checkHealth = async(dataContext: DatabaseContext): Promise<boolean> => {
export const checkHealth = async(dataContext: DatabaseContext): Promise<void> => {
/**
* 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");
};

0 comments on commit 1d23b62

Please sign in to comment.