#!/bin/bash
# 在容器内启动修复后的服务

echo "🔪 Killing all old processes..."
python3 -c "
import os
import signal
count = 0
for proc in os.listdir('/proc'):
    if proc.isdigit():
        try:
            cmdline = open(f'/proc/{proc}/cmdline').read()
            if 'python' in cmdline and ('uvicorn' in cmdline or 'rpc' in cmdline or 'backtest' in cmdline or 'final_rpc' in cmdline):
                print(f'Killing pid {proc}: {cmdline[:60]}')
                os.kill(int(proc), signal.SIGKILL)
                count += 1
        except Exception as e:
            pass
print(f'Killed {count} processes')
"

sleep 2

echo "🚀 Starting fixed RPC on port 8008..."
cd /app/scripts && python3 final_rpc_correct.py &
sleep 2

echo "🚀 Starting API on port 8088..."
cd /app/scripts && python3 -m uvicorn api_for_fixed_rpc:app --host 0.0.0.0 --port 8088 &
sleep 2

echo "✅ All services started!"
echo "RPC: 127.0.0.1:8008 (container internal)"
echo "API: 0.0.0.0:8088 (host mapped)"
