#!/bin/bash
# vLLM + Mooncake kv_consumer.
# Pre-flight gated: if dummy bootstrap doesn't satisfy vLLM startup, this
# config is marked SKIP and run_all.sh skips it.
set -euo pipefail
source "$(dirname "${BASH_SOURCE[0]}")/common.sh"

DUMMY_BOOTSTRAP_PORT="${DUMMY_BOOTSTRAP_PORT:-8997}"

# Start dummy bootstrap (in background) so the consumer has someone to talk to.
"$PYTHON" "$(dirname "${BASH_SOURCE[0]}")/../tools/dummy_bootstrap.py" \
    --port "$DUMMY_BOOTSTRAP_PORT" \
    > "$LOG_DIR/dummy_bootstrap.log" 2>&1 &
DUMMY_PID=$!
echo "$DUMMY_PID" > "$LOG_DIR/.dummy.pid"
sleep 2

KV_CFG="{\"kv_connector\":\"MooncakeConnector\",\"kv_role\":\"kv_consumer\",\"kv_connector_extra_config\":{\"prefill_addr\":\"127.0.0.1:${DUMMY_BOOTSTRAP_PORT}\"}}"

CUDA_VISIBLE_DEVICES="$GPU_ID" \
"$PYTHON" -m vllm.entrypoints.openai.api_server \
    "${COMMON_VLLM_ARGS[@]}" \
    --kv-transfer-config "$KV_CFG" \
    > "$LOG_DIR/vllm_stdout.log" 2> "$LOG_DIR/vllm_stderr.log" &
VLLM_PID=$!
echo "$VLLM_PID" > "$LOG_DIR/.vllm.pid"
echo "vLLM PID=$VLLM_PID"
wait_for_ready 300
