-
Notifications
You must be signed in to change notification settings - Fork 234
Expand file tree
/
Copy pathadmin-health.ts
More file actions
55 lines (49 loc) · 1.28 KB
/
Copy pathadmin-health.ts
File metadata and controls
55 lines (49 loc) · 1.28 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
import { getCacheClient } from '../cache/client'
import { getMasterDbClient } from '../database/client'
export interface AdminDependencyHealth {
ok: boolean
}
export interface AdminHealthSnapshot {
status: 'ok' | 'degraded'
uptimeSeconds: number
worker: {
type: string
index?: string
}
database: AdminDependencyHealth
redis: AdminDependencyHealth
}
export const collectAdminHealthSnapshot = async (): Promise<AdminHealthSnapshot> => {
const database = await pingDatabase()
const redis = await pingRedis()
return {
status: database.ok && redis.ok ? 'ok' : 'degraded',
uptimeSeconds: Math.floor(process.uptime()),
worker: {
type: process.env.WORKER_TYPE ?? 'primary',
...(process.env.WORKER_INDEX ? { index: process.env.WORKER_INDEX } : {}),
},
database,
redis,
}
}
const pingDatabase = async (): Promise<AdminDependencyHealth> => {
try {
await getMasterDbClient().raw('SELECT 1')
return { ok: true }
} catch {
return { ok: false }
}
}
const pingRedis = async (): Promise<AdminDependencyHealth> => {
try {
const client = getCacheClient()
if (!client.isOpen) {
await client.connect()
}
const pong = await client.ping()
return { ok: pong === 'PONG' }
} catch {
return { ok: false }
}
}