#!/usr/bin/env bash
# ============================================================================
# Reindex scheduler healthcheck — two independent claims:
#
#   1. THE LOOP IS ALIVE. A heartbeat newer than 180s. The loop refreshes it
#      every 60s while sleeping, so a wedged or dead scheduler shows up within
#      one interval instead of looking fine until someone notices, months later,
#      that the corpus stopped changing.
#
#   2. THE LAST COMPLETED RUN SUCCEEDED. A failed nightly reindex has NO runtime
#      symptom — every retriever fails open, so the assistant keeps answering,
#      just from an index nobody is updating any more. Reporting unhealthy makes
#      `docker compose ps` the one surface where that is visible.
#
# Nothing depends_on this service, so unhealthy here is an alarm, not an outage.
# ============================================================================
set -uo pipefail

STATE_DIR=/var/run/trakop
HEARTBEAT="$STATE_DIR/reindex-scheduler.heartbeat"
STATUS="$STATE_DIR/reindex-scheduler.status"
MAX_AGE="${RAG_REINDEX_HEARTBEAT_MAX_AGE:-180}"

fail() { printf 'UNHEALTHY: %s\n' "$*" >&2; exit 1; }

[ -f "$HEARTBEAT" ] || fail "no heartbeat yet — the scheduler loop has not started"

now="$(date +%s)"
beat="$(date -r "$HEARTBEAT" +%s 2>/dev/null || echo 0)"
age=$((now - beat))
[ "$age" -le "$MAX_AGE" ] \
    || fail "heartbeat is ${age}s old (max ${MAX_AGE}s) — the scheduler loop is wedged or dead"

# 'pending' = up, correctly waiting for its first window. Not a failure.
state="$(cut -d' ' -f1 "$STATUS" 2>/dev/null || echo pending)"
case "$state" in
    ok|pending) : ;;
    fail) fail "the last reindex FAILED ($(cat "$STATUS" 2>/dev/null)). The previous index is still in place, so retrieval still works — but it is STALE: new tables are invisible to query planning and approved Q&A is not being learned. Inspect: docker compose logs reindex-scheduler" ;;
    *) fail "unrecognised scheduler status '${state}'" ;;
esac

printf 'healthy: loop alive (heartbeat %ss old), last run: %s\n' "$age" "$(cat "$STATUS" 2>/dev/null)"
exit 0
