-
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
1d23b62
commit 325730d
Showing
2 changed files
with
22 additions
and
17 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
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,35 +1,31 @@ | ||
import { blockWatcher } from "./block-watcher"; | ||
import { redisClient } from "./caching"; | ||
import { DatabaseContext } from "./context/database-context"; | ||
import { nodeService } from "./services"; | ||
|
||
export const checkHealth = async(dataContext: DatabaseContext): Promise<void> => { | ||
/** | ||
* Things to check: | ||
* - Database connection (Done) | ||
* - Redis connection (Done) | ||
* - Node connection | ||
* - Block watcher | ||
*/ | ||
|
||
export const checkHealth = async (dataContext: DatabaseContext): Promise<void> => { | ||
const checks = { | ||
db: () => dataContext.checkConnection, | ||
redis: async() => (await redisClient.mget("test")).length === 1, | ||
db: () => () => dataContext.checkConnection, | ||
redis: async () => (await redisClient.mget("test")).length === 1, | ||
node: async () => (await nodeService.getNodeInfo()).status === 200, | ||
blockWatcher: () => blockWatcher.isHealthy() | ||
}; | ||
|
||
const results = await Promise.all( | ||
Object.entries(checks).map(async ([key, func]) => ({ | ||
[key]: await 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}`); | ||
for (const result of results) { | ||
for (const [key, value] of Object.entries(result)) { | ||
if (!value) { | ||
console.error(`🚫 ${key} is not healthy.`); | ||
isAnyFailed = true; | ||
} | ||
} | ||
} | ||
|
||
if(isAnyFailed) throw new Error("Health check failed"); | ||
if (isAnyFailed) throw new Error("Service is unhealthy."); | ||
}; |