-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
11d04de
commit 1d23b62
Showing
1 changed file
with
24 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}; |