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

echo "🚀 完全重启RPC和API，确保加载最新正确代码"

# 杀死所有相关进程
pkill -f "test_server" 2>/dev/null
pkill -f "uvicorn" 2>/dev/null
ssh admin@192.168.2.154 "export PATH=\$PATH:/var/packages/Docker/target/usr/bin && docker exec sanguo_vnpy bash -c '
for pid in \$(ps -ef | grep -E \"python|test_server|uvicorn\" | grep -v grep | awk \"{print \\\$1}\"); do
    kill -9 \$pid 2>/dev/null
done
'"

sleep 3

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

# 复制最新代码
cat /Users/chufeng/.openclaw/workspace-jiangwei/test_server_100_correct_8005.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_8005.py'"
cat /Users/chufeng/.openclaw/workspace-jiangwei/backtest_api_100_correct_mapped_8088.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_100_correct_mapped_8088.py'"

echo "📝 已复制最新代码"

# 启动
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_8005.py &
echo \"✅ RPC started on 8005\"
sleep 2
cd /app/scripts
python3 -m uvicorn backtest_api_100_correct_mapped_8088:app --host 0.0.0.0 --port 8088 &
echo \"✅ API started on 8088\"
sleep 2
'"

echo "✅ 重启完成"
echo "API: http://192.168.2.154:8088/api/backtest/run"
