|
| 1 | +import { prisma } from '@/lib/prisma' |
| 2 | + |
| 3 | +export interface HealthCheckStatus { |
| 4 | + status: 'healthy' | 'unhealthy' |
| 5 | + services?: { |
| 6 | + database?: { |
| 7 | + status: 'healthy' | 'unhealthy' |
| 8 | + error?: string |
| 9 | + } |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +async function checkDatabase(): Promise<{ status: 'healthy' | 'unhealthy'; error?: string }> { |
| 14 | + try { |
| 15 | + // Simple query to test database connectivity |
| 16 | + await prisma.$queryRaw`SELECT 1` |
| 17 | + return { |
| 18 | + status: 'healthy' |
| 19 | + } |
| 20 | + } catch (error) { |
| 21 | + return { |
| 22 | + status: 'unhealthy', |
| 23 | + error: error instanceof Error ? error.message : 'Database connection failed' |
| 24 | + } |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +function createHealthResponse(data: HealthCheckStatus, isHealthy: boolean): Response { |
| 29 | + return new Response(JSON.stringify(data), { |
| 30 | + status: isHealthy ? 200 : 503, |
| 31 | + headers: { |
| 32 | + 'Cache-Control': 'no-cache, no-store, must-revalidate', |
| 33 | + 'Content-Type': 'application/json' |
| 34 | + } |
| 35 | + }) |
| 36 | +} |
| 37 | + |
| 38 | +export async function checkReadiness(): Promise<Response> { |
| 39 | + try { |
| 40 | + const databaseStatus = await checkDatabase() |
| 41 | + |
| 42 | + const services: HealthCheckStatus['services'] = { |
| 43 | + database: databaseStatus |
| 44 | + } |
| 45 | + |
| 46 | + // For readiness: healthy only if all services are healthy |
| 47 | + const isHealthy = databaseStatus.status === 'healthy' |
| 48 | + |
| 49 | + const healthStatus: HealthCheckStatus = { |
| 50 | + status: isHealthy ? 'healthy' : 'unhealthy', |
| 51 | + services |
| 52 | + } |
| 53 | + |
| 54 | + return createHealthResponse(healthStatus, isHealthy) |
| 55 | + } catch (error) { |
| 56 | + const errorStatus: HealthCheckStatus = { |
| 57 | + status: 'unhealthy', |
| 58 | + services: { |
| 59 | + database: { |
| 60 | + status: 'unhealthy', |
| 61 | + error: error instanceof Error ? error.message : 'Readiness check failed' |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return createHealthResponse(errorStatus, false) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +export async function checkLiveness(): Promise<Response> { |
| 71 | + try { |
| 72 | + // Liveness: Only check if the app process is alive |
| 73 | + // No database or external service checks - restarting won't fix those |
| 74 | + const healthStatus: HealthCheckStatus = { |
| 75 | + status: 'healthy' |
| 76 | + // No services reported - we don't check them for liveness |
| 77 | + } |
| 78 | + |
| 79 | + return createHealthResponse(healthStatus, true) // Always 200 for liveness |
| 80 | + } catch (error) { |
| 81 | + // This should rarely happen, but if it does, the app needs restart |
| 82 | + const errorStatus: HealthCheckStatus = { |
| 83 | + status: 'unhealthy' |
| 84 | + } |
| 85 | + |
| 86 | + return createHealthResponse(errorStatus, false) |
| 87 | + } |
| 88 | +} |
0 commit comments