-
Notifications
You must be signed in to change notification settings - Fork 182
Expand file tree
/
Copy pathhealth-checks.ts
More file actions
71 lines (60 loc) · 1.53 KB
/
health-checks.ts
File metadata and controls
71 lines (60 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* Health Checks Recipe
*
* Add health check endpoints for monitoring and orchestration.
*
* Note: db and redis are placeholders. Replace with your actual
* database and cache clients.
*/
import { db, redis } from '../common';
import Router from '../router-module-loader';
import type { RouterContext } from '../router-module-loader';
const router = new Router();
type HealthStatus = {
status: 'ok' | 'degraded';
timestamp: string;
uptime: number;
checks: Record<string, string>;
};
router.get('/health', async (ctx: RouterContext) => {
const health: HealthStatus = {
status: 'ok',
timestamp: new Date().toISOString(),
uptime: process.uptime(),
checks: {}
};
try {
await db.authenticate();
health.checks.database = 'ok';
} catch (err) {
health.checks.database = 'error';
health.status = 'degraded';
}
try {
await redis.ping();
health.checks.redis = 'ok';
} catch (err) {
health.checks.redis = 'error';
health.status = 'degraded';
}
ctx.status = health.status === 'ok' ? 200 : 503;
ctx.body = health;
});
router.get('/ready', async (ctx: RouterContext) => {
const isReady = await checkReadiness();
ctx.status = isReady ? 200 : 503;
ctx.body = { ready: isReady };
});
router.get('/live', async (ctx: RouterContext) => {
ctx.body = { alive: true };
});
async function checkReadiness(): Promise<boolean> {
try {
await db.authenticate();
// Check other critical services
// await redis.ping();
return true;
} catch (err) {
return false;
}
}