#!/bin/bash
# 完全重新启动，确保代码正确

echo "🚀 完全重新启动所有服务"

# 杀死所有进程
pkill -f "test_server" 2>/dev/null
pkill -f "uvicorn" 2>/dev/null
pkill -f "python.*8088" 2>/dev/null
pkill -f "python.*8004" 2>/dev/null

sleep 3

echo "🔪 已杀死所有旧进程"

# 复制最新代码到容器
cat /Users/chufeng/.openclaw/workspace-jiangwei/test_server_100_correct.py | ssh admin@192.168.2.154 "export PATH=\$PATH:/var/packages/Docker/target/usr/bin && docker exec -i sanguo_vnpy bash -c 'cat > /app/scripts/test_server_100_correct.py'"
cat /Users/chufeng/.openclaw/workspace-jiangwei/backtest_api_final_mapped_port.py | ssh admin@192.168.2.154 "export PATH=\$PATH:/var/packages/Docker/target/usr/bin && docker exec -i sanguo_vnpy bash -c 'cat > /app/scripts/backtest_api_final_mapped_port.py'"

echo "📝 已复制最新代码"

# 启动RPC和API
ssh admin@192.168.2.154 "export PATH=\$PATH:/var/packages/Docker/target/usr/bin && docker exec sanguo_vnpy bash -c '
    cd /app/scripts
    python3 test_server_100_correct.py &
    echo \"✅ RPC started on port 8004\"
    sleep 2
    cd /app/scripts
    python3 -m uvicorn backtest_api_final_mapped_port:app --host 0.0.0.0 --port 8088 &
    echo \"✅ API started on port 8088\"
    sleep 2
'"

echo "✅ 所有服务已启动"
echo "API: http://192.168.2.154:8088/api/backtest/run"
