Repository navigation
Expand file tree
/
Copy pathstart_web_server.sh
More file actions
175 lines (164 loc) · 5.57 KB
/
Copy pathstart_web_server.sh
File metadata and controls
175 lines (164 loc) · 5.57 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
#!/usr/bin/env bash
# OpenTenBase AgentX v5.0.4 - AI Agent Infra v5.0.4 - PostgreSQL Community unified FastAPI/Uvicorn server controller.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
RUNTIME_HELPER="$SCRIPT_DIR/scripts/python_runtime.sh"
if [[ ! -f "$RUNTIME_HELPER" ]]; then
echo "Python runtime helper is missing: $RUNTIME_HELPER" >&2
exit 1
fi
source "$RUNTIME_HELPER"
VENV_PYTHON="${CX_VENV_DIR:-$SCRIPT_DIR/.venv}/bin/python"
if [[ -z "${PYTHON_BIN:-}" && -x "$VENV_PYTHON" ]]; then
PYTHON="$VENV_PYTHON"
elif ! PYTHON="$(cx_resolve_python "${PYTHON_BIN:-}")"; then
echo "Python 3.14+ is required; set PYTHON_BIN to an accessible interpreter." >&2
exit 1
fi
cx_prepare_python_environment "$PYTHON"
YASHAN_CLIENT_LIB="${CX_YASHAN_LIB_DIR:-$SCRIPT_DIR/.runtime/yashandb-client/lib}"
if [[ -d "$YASHAN_CLIENT_LIB" ]]; then
export LD_LIBRARY_PATH="$YASHAN_CLIENT_LIB${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
fi
cd "$SCRIPT_DIR"
export PYTHONPATH="$SCRIPT_DIR/scripts:$SCRIPT_DIR${PYTHONPATH:+:$PYTHONPATH}"
if [[ "${1:-start}" =~ ^(start|restart)$ && -z "${CX_CONFIG_PATH:-}" && ! -f "$SCRIPT_DIR/config.json" ]]; then
"$SCRIPT_DIR/scripts/config_wizard.sh"
fi
# Validate selection before importing any native driver or opening a socket.
DB_DIALECT="$("$PYTHON" -m lib.database_selection --dialect)"
PROFILE="${CX_RUNTIME_PROFILE:-production}"
HOST="${MEMORY_SERVER_HOST:-0.0.0.0}"
PORT="${MEMORY_SERVER_PORT:-8000}"
PID_FILE="${CX_WEB_PID_FILE:-$SCRIPT_DIR/.runtime/web.pid}"
LOG_FILE="${CX_WEB_LOG_FILE:-$SCRIPT_DIR/chuanxu_web.log}"
export CX_DATABASE_DIALECT="$DB_DIALECT"
export CX_REQUIREMENTS_FILE="$("$PYTHON" -m lib.database_selection --requirements)"
export CX_RUNTIME_PROFILE="$PROFILE"
export AI_AGENT_RELEASE_DATE="2026-11-05"
read_config() {
"$PYTHON" - "$1" "$2" <<'PY'
import sys
try:
from lib.config import get_config
cfg = get_config()
section, key = sys.argv[1:3]
value = getattr(getattr(cfg, section), key, "")
print(value if value is not None else "")
except Exception:
print("")
PY
}
if [[ -z "${MEMORY_SERVER_HOST:-}" ]]; then
HOST="$(read_config server host || true)"
HOST="${HOST:-0.0.0.0}"
fi
if [[ -z "${MEMORY_SERVER_PORT:-}" ]]; then
PORT="$(read_config server port || true)"
PORT="${PORT:-8000}"
fi
export MEMORY_SERVER_HOST="$HOST"
export MEMORY_SERVER_PORT="$PORT"
is_running() {
[[ -f "$PID_FILE" ]] || return 1
local pid
pid="$(<"$PID_FILE")"
[[ -n "$pid" ]] && kill -0 "$pid" 2>/dev/null
}
health_ready() {
"$PYTHON" - "$HOST" "$PORT" <<'PY' >/dev/null 2>&1
import sys, urllib.request
host, port = sys.argv[1:]
urllib.request.urlopen(f"http://127.0.0.1:{port}/api/health", timeout=1).read()
PY
}
stop_server() {
if ! is_running; then
rm -f "$PID_FILE"
echo "OpenTenBase AgentX is stopped."
return 0
fi
local pid
pid="$(<"$PID_FILE")"
kill "$pid" 2>/dev/null || true
for _ in {1..20}; do
if ! kill -0 "$pid" 2>/dev/null; then
rm -f "$PID_FILE"
echo "OpenTenBase AgentX stopped."
return 0
fi
sleep 0.25
done
kill -KILL "$pid" 2>/dev/null || true
rm -f "$PID_FILE"
echo "OpenTenBase AgentX stopped after forced termination."
}
start_server() {
mkdir -p "$(dirname "$PID_FILE")"
if is_running; then
echo "OpenTenBase AgentX is already running (PID $(<"$PID_FILE"))."
return 0
fi
rm -f "$PID_FILE"
if health_ready; then
echo "Port $PORT is already serving an untracked process; stop it before starting OpenTenBase AgentX." >&2
return 1
fi
if [[ -x "$SCRIPT_DIR/scripts/config_wizard.sh" && ! -f "$SCRIPT_DIR/config.json" && -z "${CX_CONFIG_PATH:-}" ]]; then
"$SCRIPT_DIR/scripts/config_wizard.sh"
fi
if [[ -f "$SCRIPT_DIR/scripts/verify_deps.py" ]] && ! "$PYTHON" "$SCRIPT_DIR/scripts/verify_deps.py"; then
echo "Offline dependency verification failed; resolve the reported vendor/ and platform errors before starting." >&2
exit 1
fi
if ! "$PYTHON" -c 'import fastapi, uvicorn' >/dev/null 2>&1; then
echo "FastAPI and Uvicorn are not installed for the selected Python. Run scripts/install_offline.sh with the same Python 3.14+ interpreter, then retry." >&2
exit 1
fi
echo "Starting OpenTenBase AgentX v5.0.4 ($DB_DIALECT, $PROFILE) on $HOST:$PORT"
setsid nohup "$PYTHON" -m uvicorn web_app:app --host "$HOST" --port "$PORT" \
--no-proxy-headers --no-access-log >>"$LOG_FILE" 2>&1 &
echo "$!" >"$PID_FILE"
for _ in {1..40}; do
if is_running; then
if health_ready; then
# A different process may own the port while this child is still
# failing startup. Require the launched PID to survive beyond the
# health response before reporting success.
sleep 0.5
if is_running; then
echo "OpenTenBase AgentX started (PID $(<"$PID_FILE"))."
return 0
fi
fi
fi
sleep 0.5
done
echo "OpenTenBase AgentX failed to start; see $LOG_FILE." >&2
stop_server || true
exit 1
}
case "${1:-status}" in
start) start_server ;;
stop) stop_server ;;
restart) stop_server; start_server ;;
status)
if is_running; then
echo "OpenTenBase AgentX is running (PID $(<"$PID_FILE"), $DB_DIALECT, $PROFILE, $HOST:$PORT)."
else
echo "OpenTenBase AgentX is stopped ($DB_DIALECT, $HOST:$PORT)."
exit 1
fi
;;
config)
echo "database=$DB_DIALECT"
echo "profile=$PROFILE"
echo "host=$HOST"
echo "port=$PORT"
echo "python=$PYTHON"
echo "pid=$PID_FILE"
echo "log=$LOG_FILE"
;;
log) tail -50 "$LOG_FILE" 2>/dev/null || true ;;
*) echo "Usage: $0 {start|stop|restart|status|config|log}" >&2; exit 2 ;;
esac