50 lines
1.5 KiB
Bash
50 lines
1.5 KiB
Bash
|
|
#!/bin/sh
|
||
|
|
# Watchdog Context Continuity — verifie API (8765) et MCP (8766),
|
||
|
|
# relance le service via son start_*.sh s'il ne repond pas sur /health.
|
||
|
|
# Idempotent : couvre le boot (rien ne tourne) ET le crash.
|
||
|
|
# Lance par tache planifiee DSM toutes les 5 min.
|
||
|
|
|
||
|
|
BASE=/volume1/homes/Master/App/Context_continuity
|
||
|
|
LOG="$BASE/watchdog.log"
|
||
|
|
TS=$(date '+%Y-%m-%d %H:%M:%S')
|
||
|
|
|
||
|
|
# Verifie /health avec plusieurs essais. Retourne 0 si OK, 1 sinon.
|
||
|
|
wait_health() {
|
||
|
|
URL="$1"
|
||
|
|
TRIES="$2"
|
||
|
|
i=0
|
||
|
|
while [ "$i" -lt "$TRIES" ]; do
|
||
|
|
if curl -s -f -m 5 "$URL" > /dev/null 2>&1; then
|
||
|
|
return 0
|
||
|
|
fi
|
||
|
|
i=$((i + 1))
|
||
|
|
sleep 3
|
||
|
|
done
|
||
|
|
return 1
|
||
|
|
}
|
||
|
|
|
||
|
|
check_and_restart() {
|
||
|
|
NAME="$1" # libelle (api / mcp)
|
||
|
|
URL="$2" # url health
|
||
|
|
STARTER="$3" # script de demarrage
|
||
|
|
|
||
|
|
# Premier check rapide (1 essai) : service deja vivant ?
|
||
|
|
if curl -s -f -m 5 "$URL" > /dev/null 2>&1; then
|
||
|
|
return 0
|
||
|
|
fi
|
||
|
|
|
||
|
|
# Service muet : on relance
|
||
|
|
echo "$TS [$NAME] KO sur $URL — relance via $STARTER" >> "$LOG"
|
||
|
|
sh "$BASE/$STARTER"
|
||
|
|
|
||
|
|
# Verification avec patience (jusqu'a 5 essais = ~15s, le demarrage uvicorn peut etre lent)
|
||
|
|
if wait_health "$URL" 5; then
|
||
|
|
echo "$TS [$NAME] relance OK" >> "$LOG"
|
||
|
|
else
|
||
|
|
echo "$TS [$NAME] ECHEC relance — toujours muet apres ~15s" >> "$LOG"
|
||
|
|
fi
|
||
|
|
}
|
||
|
|
|
||
|
|
check_and_restart "api" "http://127.0.0.1:8765/api/health" "start_api.sh"
|
||
|
|
check_and_restart "mcp" "http://127.0.0.1:8766/health" "start_mcp.sh"
|