#!/bin/bash
# Start SGLang server for the 64k-context comparison.
set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=/dev/null
source "${SCRIPT_DIR}/config.env"

cd /data/user1/yy
mkdir -p logs

export PATH="${VENV_SGLANG}/bin:$PATH"
export PYTHONUNBUFFERED=1
export SGLANG_LOG_LEVEL=info
export TMPDIR=/data/user1/yy/tmp
export CUDA_VISIBLE_DEVICES="${CUDA_VISIBLE_DEVICES}"

LOG="/data/user1/yy/logs/dsv4_h200_64k_sglang_vs_vllm_sglang_$(date +%Y%m%d_%H%M%S).log"
PID_FILE="/data/user1/yy/dsv4_h200_64k_sglang_vs_vllm_sglang.pid"

rm -f "$PID_FILE"

echo "=== Starting SGLang server (TP=$TP, max_model_len=$MAX_MODEL_LEN, max_running=$MAX_RUNNING) ==="
echo "Model: $MODEL_PATH"
echo "Port: $SGLANG_PORT"
echo "Log: $LOG"

nohup sglang serve \
    --trust-remote-code \
    --model-path "$MODEL_PATH" \
    --tp "$TP" \
    --moe-runner-backend marlin \
    --context-length "$MAX_MODEL_LEN" \
    --max-running-requests "$MAX_RUNNING" \
    --mem-fraction-static 0.88 \
    --host 0.0.0.0 \
    --port "$SGLANG_PORT" \
    > "$LOG" 2>&1 &

PID=$!
echo $PID > "$PID_FILE"
echo "PID: $PID"
echo "Waiting for health..."

for i in $(seq 1 240); do
    if curl --fail --silent --show-error --max-time 5 "http://127.0.0.1:${SGLANG_PORT}/health" >/dev/null 2>&1; then
        echo "SGLang server is ready at http://127.0.0.1:${SGLANG_PORT}"
        echo "Log: $LOG"
        exit 0
    fi
    if ! kill -0 $PID 2>/dev/null; then
        echo "ERROR: SGLang server exited early"
        tail -200 "$LOG"
        exit 1
    fi
    echo "Waiting... ($i/240)"
    sleep 5
done

echo "ERROR: SGLang server not healthy after 240 retries"
tail -200 "$LOG"
exit 1
