Service health check with queue and Redis status
cURL
curl --request GET \ --url https://api.example.com/api/health
{ "status": "<string>", "timestamp": "<string>", "jobQueue": { "enabled": true, "redis": "<string>" } }
ok
degraded
Show jobQueue properties
connected
disconnected
curl https://api.bioagents.xyz/api/health
{ "status": "ok", "timestamp": "2024-03-15T10:30:45.123Z", "jobQueue": { "enabled": false } }
{ "status": "ok", "timestamp": "2024-03-15T10:30:45.123Z", "jobQueue": { "enabled": true, "redis": "connected" } }
{ "status": "degraded", "timestamp": "2024-03-15T10:30:45.123Z", "jobQueue": { "enabled": true, "redis": "disconnected" } }
livenessProbe: httpGet: path: /api/health port: 3000 initialDelaySeconds: 30 periodSeconds: 10 timeoutSeconds: 5 failureThreshold: 3
services: bioagents: image: bioagents:latest healthcheck: test: ["CMD", "curl", "-f", "http://localhost:3000/api/health"] interval: 30s timeout: 10s retries: 3 start_period: 40s
#!/bin/bash # health-check.sh RESPONSE=$(curl -s https://api.bioagents.xyz/api/health) STATUS=$(echo $RESPONSE | jq -r '.status') if [ "$STATUS" != "ok" ]; then echo "Service degraded: $RESPONSE" # Send alert to monitoring system curl -X POST https://monitoring.example.com/alert \ -H "Content-Type: application/json" \ -d "{\"service\": \"bioagents\", \"status\": \"$STATUS\", \"details\": $RESPONSE}" fi
async function checkHealth() { try { const response = await fetch('https://api.bioagents.xyz/api/health'); const health = await response.json(); if (health.status !== 'ok') { console.warn('Service health degraded:', health); // Check specific subsystems if (health.jobQueue?.enabled && health.jobQueue?.redis === 'disconnected') { console.error('Redis connection lost - queue jobs may fail'); } } return health; } catch (error) { console.error('Health check failed:', error); return { status: 'unreachable', error: error.message }; } } // Poll every 30 seconds setInterval(checkHealth, 30000);
src/index.ts:212-251
.get("/api/health", async () => { const health = { status: "ok", timestamp: new Date().toISOString(), }; if (isJobQueueEnabled()) { try { const redis = getBullMQConnection(); await redis.ping(); health.jobQueue = { enabled: true, redis: "connected", }; } catch (error) { health.jobQueue = { enabled: true, redis: "disconnected", }; health.status = "degraded"; } } else { health.jobQueue = { enabled: false, }; } return health; })
# Prometheus alerting rules groups: - name: bioagents rules: - alert: BioAgentsServiceDown expr: up{job="bioagents"} == 0 for: 2m labels: severity: critical annotations: summary: "BioAgents service is down" - alert: BioAgentsServiceDegraded expr: bioagents_health_status{status="degraded"} == 1 for: 5m labels: severity: warning annotations: summary: "BioAgents service degraded (Redis disconnected)" - alert: BioAgentsSlowHealthCheck expr: bioagents_health_check_duration_seconds > 1 for: 5m labels: severity: warning annotations: summary: "Health check taking longer than 1 second"