-
Notifications
You must be signed in to change notification settings - Fork 45
feat(health): add multi-service health check endpoint #611
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
KanishJebaMathewM
merged 5 commits into
KanishJebaMathewM:main
from
nyxsky404:feat/580-health-check-endpoint
Jun 18, 2026
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1cd787e
feat(health): add multi-service health check endpoint
nyxsky404 ff9dd42
fix(health): add per-check timeouts, /live liveness probe, and use CM…
nyxsky404 2bdec0a
fix(health): reduce timeout to 400ms, clear timer on resolve, treat n…
nyxsky404 8effd57
fix(health): validate HEALTHCHECK_TIMEOUT_MS before use to guard agai…
nyxsky404 77c5a87
fix(health): resolve merge conflict with main in index.js
KanishJebaMathewM File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import express from 'express'; | ||
| import { supabase, mongoDb, redisClient, firebaseAdmin } from '../config/db.js'; | ||
|
|
||
| const router = express.Router(); | ||
|
|
||
| const DEFAULT_TIMEOUT_MS = 400; | ||
| const _parsedTimeout = Number(process.env.HEALTHCHECK_TIMEOUT_MS); | ||
| const CHECK_TIMEOUT_MS = | ||
| Number.isFinite(_parsedTimeout) && _parsedTimeout > 0 ? _parsedTimeout : DEFAULT_TIMEOUT_MS; | ||
|
|
||
| function withTimeout(promise) { | ||
| let timer; | ||
| return Promise.race([ | ||
| promise, | ||
| new Promise((_, reject) => { | ||
| timer = setTimeout(() => reject(new Error('healthcheck timeout')), CHECK_TIMEOUT_MS); | ||
| }), | ||
| ]).finally(() => clearTimeout(timer)); | ||
| } | ||
|
|
||
| async function checkSupabase() { | ||
| if (!supabase) return 'not_configured'; | ||
| try { | ||
| const { error } = await withTimeout( | ||
| supabase.from('profiles').select('id').limit(1) | ||
| ); | ||
| return error ? 'failed' : 'connected'; | ||
| } catch { | ||
| return 'failed'; | ||
| } | ||
| } | ||
|
|
||
| async function checkMongo() { | ||
| if (!mongoDb) return 'not_configured'; | ||
| try { | ||
| await withTimeout(mongoDb.admin().ping()); | ||
| return 'connected'; | ||
| } catch { | ||
| return 'failed'; | ||
| } | ||
| } | ||
|
|
||
| async function checkRedis() { | ||
| if (!redisClient) return 'not_configured'; | ||
| try { | ||
| const reply = await withTimeout(redisClient.ping()); | ||
| return reply === 'PONG' ? 'connected' : 'failed'; | ||
| } catch { | ||
| return 'failed'; | ||
| } | ||
| } | ||
|
|
||
| function checkFirebase() { | ||
| return firebaseAdmin ? 'configured' : 'not_configured'; | ||
| } | ||
|
|
||
| function checkPolygon() { | ||
| return process.env.POLYGON_RPC_URL ? 'configured' : 'not_configured'; | ||
| } | ||
|
|
||
| const CRITICAL_UNHEALTHY = new Set(['failed', 'not_configured']); | ||
|
|
||
| // GET /api/health — full dependency check; returns 503 when a critical service fails | ||
| router.get('/', async (req, res) => { | ||
| const [supabaseStatus, mongoStatus, redisStatus] = await Promise.all([ | ||
| checkSupabase(), | ||
| checkMongo(), | ||
| checkRedis(), | ||
| ]); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| const services = { | ||
| supabase: supabaseStatus, | ||
| mongodb: mongoStatus, | ||
| redis: redisStatus, | ||
| firebase: checkFirebase(), | ||
| polygon: checkPolygon(), | ||
| }; | ||
|
|
||
| const criticalFailed = | ||
| CRITICAL_UNHEALTHY.has(supabaseStatus) || CRITICAL_UNHEALTHY.has(mongoStatus); | ||
|
|
||
| const status = criticalFailed ? 'degraded' : 'ok'; | ||
| const httpStatus = criticalFailed ? 503 : 200; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| return res.status(httpStatus).json({ | ||
| status, | ||
| services, | ||
| uptime: process.uptime(), | ||
| }); | ||
| }); | ||
|
|
||
| // GET /api/health/live — liveness probe; always 200 as long as the process is up | ||
| router.get('/live', (req, res) => { | ||
| res.json({ status: 'ok', uptime: process.uptime() }); | ||
| }); | ||
|
|
||
| export default router; | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.