-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrun_all_linux.sh
More file actions
116 lines (94 loc) · 3.24 KB
/
run_all_linux.sh
File metadata and controls
116 lines (94 loc) · 3.24 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
#!/bin/bash
set -euo pipefail
VENV_PATH=".venv/bin/activate"
if [ ! -f "$VENV_PATH" ]; then
echo "Error: virtual environment not found at $VENV_PATH"
exit 1
fi
source "$VENV_PATH"
if [ -z "${VIRTUAL_ENV:-}" ]; then
echo "Error: failed to activate the virtual environment."
exit 1
fi
export APP_ENV="${APP_ENV:-production}"
export RPP_API_URL="${RPP_API_URL:-http://127.0.0.1:8000}"
export RPP_LEGACY_URL="${RPP_LEGACY_URL:-http://127.0.0.1:5000}"
API_HOST="${API_HOST:-0.0.0.0}"
API_PORT="${API_PORT:-8000}"
STREAMLIT_HOST="${STREAMLIT_HOST:-0.0.0.0}"
STREAMLIT_PORT="${STREAMLIT_PORT:-8501}"
GUNICORN_WORKERS="${GUNICORN_WORKERS:-2}"
GUNICORN_TIMEOUT="${GUNICORN_TIMEOUT:-60}"
backend_log="fastapi_backend.log"
frontend_log="streamlit_frontend.log"
backend_pid_file="fastapi_backend.pid"
frontend_pid_file="streamlit_frontend.pid"
cleanup() {
echo
echo "Shutting down services..."
if [ -f "$backend_pid_file" ]; then
backend_pid=$(cat "$backend_pid_file")
if ps -p "$backend_pid" > /dev/null 2>&1; then
echo "Stopping FastAPI backend (PID: $backend_pid)..."
kill "$backend_pid" 2>/dev/null || true
sleep 2
kill -9 "$backend_pid" 2>/dev/null || true
fi
rm -f "$backend_pid_file"
fi
if [ -f "$frontend_pid_file" ]; then
frontend_pid=$(cat "$frontend_pid_file")
if ps -p "$frontend_pid" > /dev/null 2>&1; then
echo "Stopping Streamlit frontend (PID: $frontend_pid)..."
kill "$frontend_pid" 2>/dev/null || true
sleep 2
kill -9 "$frontend_pid" 2>/dev/null || true
fi
rm -f "$frontend_pid_file"
fi
pkill -f "gunicorn .* api.main:app" 2>/dev/null || true
pkill -f "uvicorn api.main:app" 2>/dev/null || true
pkill -f "streamlit run frontend.py" 2>/dev/null || true
echo "Cleanup complete"
}
trap cleanup EXIT INT TERM
echo "Starting FastAPI backend with gunicorn..."
gunicorn \
--workers "$GUNICORN_WORKERS" \
--worker-class uvicorn.workers.UvicornWorker \
--bind "$API_HOST:$API_PORT" \
--log-level info \
--timeout "$GUNICORN_TIMEOUT" \
api.main:app \
>"$backend_log" 2>&1 &
backend_pid=$!
echo "$backend_pid" > "$backend_pid_file"
echo "FastAPI backend started with PID: $backend_pid"
echo "Waiting for backend to accept connections..."
sleep 5
if ! ps -p "$backend_pid" > /dev/null 2>&1; then
echo "ERROR: FastAPI backend failed to start. Check $backend_log"
exit 1
fi
echo "Starting Streamlit app..."
streamlit run frontend.py \
--server.address "$STREAMLIT_HOST" \
--server.port "$STREAMLIT_PORT" \
>"$frontend_log" 2>&1 &
frontend_pid=$!
echo "$frontend_pid" > "$frontend_pid_file"
echo "Streamlit frontend started with PID: $frontend_pid"
sleep 3
if ! ps -p "$frontend_pid" > /dev/null 2>&1; then
echo "ERROR: Streamlit frontend failed to start. Check $frontend_log"
exit 1
fi
echo ""
echo "Services started successfully."
echo "FastAPI backend: http://$API_HOST:$API_PORT (PID: $backend_pid)"
echo "Streamlit app: http://$STREAMLIT_HOST:$STREAMLIT_PORT (PID: $frontend_pid)"
echo "Backend log: tail -f $backend_log"
echo "Frontend log: tail -f $frontend_log"
echo ""
echo "Press Ctrl+C to stop both services"
wait