mirror of
https://github.com/spliit-app/spliit.git
synced 2026-03-05 12:16:13 +01:00
This commit is contained in:
@@ -10,28 +10,35 @@ export interface HealthCheckStatus {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function checkDatabase(): Promise<{ status: 'healthy' | 'unhealthy'; error?: string }> {
|
async function checkDatabase(): Promise<{
|
||||||
|
status: 'healthy' | 'unhealthy'
|
||||||
|
error?: string
|
||||||
|
}> {
|
||||||
try {
|
try {
|
||||||
// Simple query to test database connectivity
|
// Simple query to test database connectivity
|
||||||
await prisma.$queryRaw`SELECT 1`
|
await prisma.$queryRaw`SELECT 1`
|
||||||
return {
|
return {
|
||||||
status: 'healthy'
|
status: 'healthy',
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return {
|
return {
|
||||||
status: 'unhealthy',
|
status: 'unhealthy',
|
||||||
error: error instanceof Error ? error.message : 'Database connection failed'
|
error:
|
||||||
|
error instanceof Error ? error.message : 'Database connection failed',
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function createHealthResponse(data: HealthCheckStatus, isHealthy: boolean): Response {
|
function createHealthResponse(
|
||||||
|
data: HealthCheckStatus,
|
||||||
|
isHealthy: boolean,
|
||||||
|
): Response {
|
||||||
return new Response(JSON.stringify(data), {
|
return new Response(JSON.stringify(data), {
|
||||||
status: isHealthy ? 200 : 503,
|
status: isHealthy ? 200 : 503,
|
||||||
headers: {
|
headers: {
|
||||||
'Cache-Control': 'no-cache, no-store, must-revalidate',
|
'Cache-Control': 'no-cache, no-store, must-revalidate',
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json',
|
||||||
}
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,7 +47,7 @@ export async function checkReadiness(): Promise<Response> {
|
|||||||
const databaseStatus = await checkDatabase()
|
const databaseStatus = await checkDatabase()
|
||||||
|
|
||||||
const services: HealthCheckStatus['services'] = {
|
const services: HealthCheckStatus['services'] = {
|
||||||
database: databaseStatus
|
database: databaseStatus,
|
||||||
}
|
}
|
||||||
|
|
||||||
// For readiness: healthy only if all services are healthy
|
// For readiness: healthy only if all services are healthy
|
||||||
@@ -48,7 +55,7 @@ export async function checkReadiness(): Promise<Response> {
|
|||||||
|
|
||||||
const healthStatus: HealthCheckStatus = {
|
const healthStatus: HealthCheckStatus = {
|
||||||
status: isHealthy ? 'healthy' : 'unhealthy',
|
status: isHealthy ? 'healthy' : 'unhealthy',
|
||||||
services
|
services,
|
||||||
}
|
}
|
||||||
|
|
||||||
return createHealthResponse(healthStatus, isHealthy)
|
return createHealthResponse(healthStatus, isHealthy)
|
||||||
@@ -58,9 +65,10 @@ export async function checkReadiness(): Promise<Response> {
|
|||||||
services: {
|
services: {
|
||||||
database: {
|
database: {
|
||||||
status: 'unhealthy',
|
status: 'unhealthy',
|
||||||
error: error instanceof Error ? error.message : 'Readiness check failed'
|
error:
|
||||||
}
|
error instanceof Error ? error.message : 'Readiness check failed',
|
||||||
}
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
return createHealthResponse(errorStatus, false)
|
return createHealthResponse(errorStatus, false)
|
||||||
@@ -72,7 +80,7 @@ export async function checkLiveness(): Promise<Response> {
|
|||||||
// Liveness: Only check if the app process is alive
|
// Liveness: Only check if the app process is alive
|
||||||
// No database or external service checks - restarting won't fix those
|
// No database or external service checks - restarting won't fix those
|
||||||
const healthStatus: HealthCheckStatus = {
|
const healthStatus: HealthCheckStatus = {
|
||||||
status: 'healthy'
|
status: 'healthy',
|
||||||
// No services reported - we don't check them for liveness
|
// No services reported - we don't check them for liveness
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,7 +88,7 @@ export async function checkLiveness(): Promise<Response> {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
// This should rarely happen, but if it does, the app needs restart
|
// This should rarely happen, but if it does, the app needs restart
|
||||||
const errorStatus: HealthCheckStatus = {
|
const errorStatus: HealthCheckStatus = {
|
||||||
status: 'unhealthy'
|
status: 'unhealthy',
|
||||||
}
|
}
|
||||||
|
|
||||||
return createHealthResponse(errorStatus, false)
|
return createHealthResponse(errorStatus, false)
|
||||||
|
|||||||
Reference in New Issue
Block a user