-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathstart.sh
More file actions
executable file
·100 lines (86 loc) · 3.06 KB
/
start.sh
File metadata and controls
executable file
·100 lines (86 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/bin/bash
# sovereign_guardian.sh
# Self-healing boot loop with hang detection and clean port recovery.
PORT=${SOV_PORT:-8002}
HEALTH_URL="http://127.0.0.1:${PORT}/api/health"
echo "======================================================="
echo " [SOVEREIGN] Guardian Boot Sequence Initiated"
echo " Protocol: Kill → Verify → Launch → Watch"
echo "======================================================="
cd "$(dirname "$0")" || exit 1
nuke_port() {
local pid
pid=$(ss -tlnp | grep ":${PORT}" | grep -oP 'pid=\K[0-9]+' | head -1)
if [ -n "$pid" ]; then
echo "[GUARDIAN] Force-killing zombie PID ${pid} on port ${PORT}..."
kill -9 "$pid" 2>/dev/null
fi
sleep 1
}
wait_healthy() {
# Poll health endpoint for up to 10s after spawn
local attempts=0
while [ $attempts -lt 20 ]; do
if curl -sf --max-time 1 "${HEALTH_URL}" > /dev/null 2>&1; then
echo "[GUARDIAN] Backend healthy ✓"
return 0
fi
sleep 0.5
attempts=$((attempts + 1))
done
echo "[GUARDIAN] Backend failed health check after 10s — killing and restarting."
return 1
}
watch_health() {
# Background watchdog: if health endpoint stops responding, kill uvicorn so loop restarts it
local uvicorn_pid=$1
while kill -0 "$uvicorn_pid" 2>/dev/null; do
sleep 20
if ! curl -sf --max-time 15 "${HEALTH_URL}" > /dev/null 2>&1; then
echo "[GUARDIAN] !! Health check FAILED — backend hung. Killing PID ${uvicorn_pid}..."
kill -9 "$uvicorn_pid" 2>/dev/null
return
fi
done
}
while true; do
nuke_port
# Ensure we use the isolated venv if it exists
if [ -f ".venv/bin/python3" ]; then
PYTHON_CMD=".venv/bin/python3"
else
PYTHON_CMD="python3"
fi
echo "[GUARDIAN] Launching backend on port ${PORT}..."
$PYTHON_CMD -m uvicorn main:app --host 0.0.0.0 --port ${PORT} --reload &
UVICORN_PID=$!
# Start background health watchdog
watch_health $UVICORN_PID &
WATCHDOG_PID=$!
# Wait for it to come up healthy
if ! wait_healthy; then
kill -9 $UVICORN_PID 2>/dev/null
kill $WATCHDOG_PID 2>/dev/null
echo "[GUARDIAN] Retrying in 2s..."
sleep 2
continue
fi
echo "[GUARDIAN] Spawning native Sovereign UI..."
if [ -z "${SOV_ELECTRON}" ]; then
if command -v google-chrome > /dev/null 2>&1; then
google-chrome --app="http://127.0.0.1:${PORT}" --class="SovereignEngine" --window-size=1400,900 --no-first-run --no-default-browser-check &
else
echo "[GUARDIAN] Please open browser manually to: http://127.0.0.1:${PORT}"
fi
else
echo "[GUARDIAN] Electron mode — skipping browser spawn."
fi
# Block until uvicorn exits (crashed, killed by watchdog, etc.)
wait $UVICORN_PID
EXIT_CODE=$?
kill $WATCHDOG_PID 2>/dev/null
echo ""
echo "[GUARDIAN] !! Backend exited (code: ${EXIT_CODE}) — resurrecting in 2s..."
echo "======================================================="
sleep 2
done