Spaces:
Sleeping
Sleeping
File size: 1,992 Bytes
c4b28eb 307e1fd c4b28eb 4b80514 c4b28eb e745b75 c4b28eb 13846a7 a0280a8 307e1fd a0280a8 307e1fd a0280a8 307e1fd a0280a8 307e1fd a0280a8 307e1fd a0280a8 307e1fd a0280a8 307e1fd | 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 | #!/usr/bin/env bash
set -euo pipefail
# Default to 1 worker to prevent OOM on low-memory hosts
WORKERS_VALUE="${WORKERS:-1}"
TIMEOUT_VALUE="${TIMEOUT:-120}"
PORT_VALUE="${PORT:-10000}"
# Initialize the database
echo "Initializing database..."
python scripts/init_pgvector.py
echo "Starting gunicorn on port ${PORT_VALUE} with ${WORKERS_VALUE} workers and timeout ${TIMEOUT_VALUE}s"
export PYTHONPATH="/app${PYTHONPATH:+:$PYTHONPATH}"
# Start gunicorn in background so we can trap signals and collect diagnostics
gunicorn \
--bind 0.0.0.0:${PORT_VALUE} \
--workers "${WORKERS_VALUE}" \
--timeout "${TIMEOUT_VALUE}" \
--log-level debug \
--access-logfile - \
--error-logfile - \
--capture-output \
--config gunicorn.conf.py \
app:app &
GUNICORN_PID=$!
# Trap TERM and INT, log diagnostics, forward the signal to gunicorn, and wait
handle_term() {
echo "===== SIGTERM received at $(date -u +'%Y-%m-%dT%H:%M:%SZ') ====="
echo "--- Top processes by RSS ---"
ps aux --sort=-rss | head -n 20 || true
echo "--- /proc/meminfo (if available) ---"
cat /proc/meminfo || true
echo "Forwarding SIGTERM to gunicorn (pid ${GUNICORN_PID})"
kill -TERM "${GUNICORN_PID}" 2>/dev/null || true
# Wait for gunicorn to exit
wait "${GUNICORN_PID}" || true
echo "Gunicorn exited; wrapper exiting"
exit 0
}
trap 'handle_term' SIGTERM SIGINT
# Give gunicorn a moment to start before pre-warm
echo "Waiting for server to start to pre-warm..."
sleep 5
# Pre-warm application (best-effort; don't fail startup if warm request fails)
echo "Pre-warming application..."
curl -sS -X POST http://localhost:${PORT_VALUE}/chat \
-H "Content-Type: application/json" \
-d '{"message":"pre-warm"}' \
--max-time 180 --fail >/dev/null 2>&1 || echo "Pre-warm request failed but continuing..."
echo "Server is running."
# Wait for gunicorn to exit and forward its exit code
wait "${GUNICORN_PID}"
EXIT_CODE=$?
echo "Gunicorn stopped with exit code ${EXIT_CODE}"
exit "${EXIT_CODE}"
|