#!/usr/bin/env bash
# =============================================================================
# /opt/timmy-tower/health-check.sh
# Run by systemd timer every 5 minutes.
# Restarts timmy-tower if /api/healthz returns non-200.
# =============================================================================
HEALTH_URL="http://localhost:8088/api/healthz"
LOG="/opt/timmy-tower/health.log"

log() { echo "[$(date -u +%FT%TZ)] [health] $*" | tee -a "$LOG"; }

if curl -sf --max-time 10 "$HEALTH_URL" > /dev/null; then
  exit 0
fi

log "Health check FAILED — restarting timmy-tower"
systemctl restart timmy-tower
sleep 5

if curl -sf --max-time 10 "$HEALTH_URL" > /dev/null; then
  log "Service recovered after restart"
else
  log "CRITICAL: service did not recover after restart — manual intervention needed"
fi
