Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | /** * Health check endpoint for deployment orchestration * * GET /api/health * * Returns 200 OK when the application is ready to serve traffic: * - Database connection is working * - All critical services are initialized * * Status levels: * - healthy (200): All services operational * - degraded (200): Core services work, but some features limited (e.g., Redis down) * - unhealthy (503): Cannot serve traffic (e.g., database down) * * Used by: * - Red-black deployment scripts to verify new containers are ready * - Docker healthcheck for container orchestration * - Load balancers to determine if traffic should be routed (Traefik) */ import { NextResponse } from 'next/server' import { sql } from 'drizzle-orm' import { db } from '@/db' import { getRedisClient, isRedisAvailable } from '@/lib/redis' export const dynamic = 'force-dynamic' interface HealthCheckResult { status: 'healthy' | 'degraded' | 'unhealthy' timestamp: string /** Unique identifier for this container instance, generated at startup */ instanceId: string /** Seconds since container started */ uptimeSeconds: number checks: { database: { status: 'ok' | 'error' latencyMs?: number error?: string } redis?: { status: 'ok' | 'error' | 'not_configured' latencyMs?: number error?: string /** Features affected when Redis is down */ affectedFeatures?: string[] } } version?: string commit?: string buildTimestamp?: string | null nodeVersion: string /** Pipeline test marker - change this value to verify auto-deploy */ pipelineTest: string } // Generate a unique instance ID at module load time (container startup) const INSTANCE_ID = `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}` const STARTUP_TIME = Date.now() export async function GET(): Promise<NextResponse<HealthCheckResult>> { const result: HealthCheckResult = { status: 'healthy', timestamp: new Date().toISOString(), instanceId: INSTANCE_ID, uptimeSeconds: Math.floor((Date.now() - STARTUP_TIME) / 1000), nodeVersion: process.version, pipelineTest: 'feb01-test-alpha', checks: { database: { status: 'ok' }, }, } // Check database connectivity (critical - app cannot function without it) try { const dbStart = Date.now() await db.run(sql`SELECT 1`) result.checks.database.latencyMs = Date.now() - dbStart } catch (error) { result.status = 'unhealthy' result.checks.database = { status: 'error', error: error instanceof Error ? error.message : 'Unknown database error', } } // Check Redis connectivity (non-critical - app works without it but with reduced functionality) const redisClient = getRedisClient() if (!redisClient) { // Redis not configured - this is fine in development result.checks.redis = { status: 'not_configured', affectedFeatures: ['cross-instance sessions', 'remote camera sync'], } // Only mark as degraded in production where Redis is expected if (process.env.REDIS_URL) { result.status = result.status === 'healthy' ? 'degraded' : result.status } } else if (!isRedisAvailable()) { // Redis configured but not connected result.checks.redis = { status: 'error', error: `Connection status: ${redisClient.status}`, affectedFeatures: ['cross-instance sessions', 'remote camera sync', 'Socket.IO scaling'], } // Degrade status (but don't mark unhealthy - app can still serve traffic) result.status = result.status === 'healthy' ? 'degraded' : result.status } else { // Redis connected - verify with a ping try { const redisStart = Date.now() await redisClient.ping() result.checks.redis = { status: 'ok', latencyMs: Date.now() - redisStart, } } catch (error) { result.checks.redis = { status: 'error', error: error instanceof Error ? error.message : 'Redis ping failed', affectedFeatures: ['cross-instance sessions', 'remote camera sync', 'Socket.IO scaling'], } result.status = result.status === 'healthy' ? 'degraded' : result.status } } // Add version info from environment (set during Docker build) if (process.env.GIT_COMMIT) { result.commit = process.env.GIT_COMMIT } if (process.env.npm_package_version) { result.version = process.env.npm_package_version } result.buildTimestamp = process.env.BUILD_TIMESTAMP ?? null // Return 200 for healthy/degraded (can still serve traffic), 503 for unhealthy const statusCode = result.status === 'unhealthy' ? 503 : 200 return NextResponse.json(result, { status: statusCode }) } |