-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode-rag-server.sh
More file actions
executable file
·201 lines (175 loc) · 5.58 KB
/
Copy pathcode-rag-server.sh
File metadata and controls
executable file
·201 lines (175 loc) · 5.58 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/bin/bash
# Launcher for the persistent code-rag HTTP server.
# Starts the server if not running, verifies health.
# Safe to call multiple times (idempotent).
#
# Usage: ./code-rag-server.sh [start|stop|status]
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SERVER_DIR="$HOME/.code-rag"
PID_FILE="$SERVER_DIR/server.pid"
WATCHDOG_PID_FILE="$SERVER_DIR/watchdog.pid"
LOG_FILE="$SERVER_DIR/server.log"
PYTHON="$SCRIPT_DIR/venv/bin/python"
PORT="${CODE_RAG_PORT:-7101}"
HEALTH_URL="http://127.0.0.1:$PORT/health"
MAX_WAIT=30
WATCHDOG_INTERVAL=30
WATCHDOG_MAX_FAILURES=3
mkdir -p "$SERVER_DIR"
is_running() {
if [ -f "$PID_FILE" ]; then
local pid
pid=$(cat "$PID_FILE")
if kill -0 "$pid" 2>/dev/null; then
if curl -sf --max-time 2 "$HEALTH_URL" >/dev/null 2>&1; then
return 0
fi
return 1
else
rm -f "$PID_FILE"
fi
fi
return 1
}
stop_watchdog() {
if [ -f "$WATCHDOG_PID_FILE" ]; then
local wpid
wpid=$(cat "$WATCHDOG_PID_FILE")
if kill -0 "$wpid" 2>/dev/null; then
kill "$wpid" 2>/dev/null || true
fi
rm -f "$WATCHDOG_PID_FILE"
fi
}
start_watchdog() {
stop_watchdog
(
set +e # Don't let unexpected failures kill the watchdog
failures=0
while true; do
sleep "$WATCHDOG_INTERVAL"
if curl -sf --max-time 5 "$HEALTH_URL" >/dev/null 2>&1; then
failures=0
else
failures=$((failures + 1))
echo "[code-rag-watchdog] Health check failed ($failures/$WATCHDOG_MAX_FAILURES)" >> "$LOG_FILE"
fi
if [ "$failures" -ge "$WATCHDOG_MAX_FAILURES" ]; then
echo "[code-rag-watchdog] Server unresponsive — restarting..." >> "$LOG_FILE"
# Kill old server if still alive
if [ -f "$PID_FILE" ]; then
old_pid=$(cat "$PID_FILE")
kill "$old_pid" 2>/dev/null || true
sleep 2
kill -9 "$old_pid" 2>/dev/null || true
rm -f "$PID_FILE"
fi
# Kill stale port holders
stale_pids=$(lsof -ti :"$PORT" 2>/dev/null || true)
if [ -n "$stale_pids" ]; then
echo "$stale_pids" | xargs kill 2>/dev/null || true
sleep 1
fi
# Restart the server
export PYTHONPATH="$SCRIPT_DIR"
nohup "$PYTHON" -u "$SCRIPT_DIR/http_server.py" >> "$LOG_FILE" 2>&1 &
# Wait for it to be healthy
waited=0
while [ $waited -lt $MAX_WAIT ]; do
sleep 1
waited=$((waited + 1))
if curl -sf --max-time 2 "$HEALTH_URL" >/dev/null 2>&1; then
echo "[code-rag-watchdog] Server restarted successfully (${waited}s)" >> "$LOG_FILE"
break
fi
done
failures=0
fi
done
) &
echo $! > "$WATCHDOG_PID_FILE"
echo "[code-rag] Watchdog started (PID $!)" >&2
}
do_start() {
if is_running; then
echo "[code-rag] Already running (PID $(cat "$PID_FILE"))" >&2
exit 0
fi
rm -f "$PID_FILE"
# Kill any stale process holding our port
local stale_pids
stale_pids=$(lsof -ti :"$PORT" 2>/dev/null || true)
if [ -n "$stale_pids" ]; then
echo "[code-rag] Killing stale process(es) on port $PORT: $stale_pids" >&2
echo "$stale_pids" | xargs kill 2>/dev/null || true
sleep 1
fi
echo "[code-rag] Starting HTTP server on port $PORT..." >&2
export PYTHONPATH="$SCRIPT_DIR"
nohup "$PYTHON" -u "$SCRIPT_DIR/http_server.py" >> "$LOG_FILE" 2>&1 &
local server_pid=$!
local waited=0
while [ $waited -lt $MAX_WAIT ]; do
sleep 1
waited=$((waited + 1))
if ! kill -0 $server_pid 2>/dev/null; then
echo "[code-rag] Server process died. Check $LOG_FILE" >&2
exit 1
fi
if curl -sf --max-time 2 "$HEALTH_URL" >/dev/null 2>&1; then
echo "[code-rag] Server ready (PID $server_pid, ${waited}s)" >&2
start_watchdog
exit 0
fi
done
echo "[code-rag] Server failed to become healthy after ${MAX_WAIT}s. Check $LOG_FILE" >&2
exit 1
}
do_stop() {
stop_watchdog
if [ -f "$PID_FILE" ]; then
local pid
pid=$(cat "$PID_FILE")
if kill -0 "$pid" 2>/dev/null; then
echo "[code-rag] Stopping server (PID $pid)..." >&2
kill "$pid"
local waited=0
while [ $waited -lt 10 ]; do
if ! kill -0 "$pid" 2>/dev/null; then
break
fi
sleep 1
waited=$((waited + 1))
done
if kill -0 "$pid" 2>/dev/null; then
kill -9 "$pid" 2>/dev/null || true
fi
fi
rm -f "$PID_FILE"
echo "[code-rag] Stopped." >&2
else
echo "[code-rag] Not running." >&2
fi
}
do_status() {
if is_running; then
echo "[code-rag] Running (PID $(cat "$PID_FILE"))" >&2
else
echo "[code-rag] Not running." >&2
exit 1
fi
}
case "${1:-start}" in
start) do_start ;;
stop) do_stop ;;
status) do_status ;;
restart)
do_stop
do_start
;;
*)
echo "Usage: $0 {start|stop|status|restart}" >&2
exit 1
;;
esac