Skip to content

Health Checks

@app.route('/health')
def health():
return {'status': 'healthy'}, 200
app.get('/health', (req, res) => {
res.json({ status: 'healthy' });
});
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"status":"healthy"}`))
})
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD curl -f http://localhost:8000/health || exit 1
services:
snapcode:
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
interval: 30s
timeout: 3s
retries: 3
start_period: 5s
Terminal window
# Check health status
docker inspect --format='{{.State.Health.Status}}' snapcode
location /health {
access_log off;
return 200 "healthy\n";
add_header Content-Type text/plain;
}
backend snapcode_backend
option httpchk GET /health
http-check expect status 200
server app1 localhost:8000 check
upstream backend {
server localhost:8000 max_fails=3 fail_timeout=30s;
server localhost:8001 max_fails=3 fail_timeout=30s;
}
health-check.sh
#!/bin/bash
URL="http://localhost:8000/health"
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" $URL)
if [ $RESPONSE -ne 200 ]; then
echo "Service unhealthy: $RESPONSE"
# Send alert
systemctl restart snapcode
fi

Cron:

Terminal window
* * * * * /path/to/health-check.sh
@app.route('/health')
def health():
checks = {
'database': check_db(),
'cache': check_redis(),
'disk': check_disk_space(),
}
status = 'healthy' if all(checks.values()) else 'unhealthy'
code = 200 if status == 'healthy' else 503
return {'status': status, 'checks': checks}, code