#!/usr/bin/env python3
"""
重启最终修复后的服务
"""

import subprocess
import sys
import time

def main():
    print("🚀 重启最终修复后的回测服务")
    print("="*60)
    
    # 杀死旧进程
    print("🔪 清理旧进程...")
    cmds = [
        "ssh admin@192.168.2.154 'export PATH=$PATH:/var/packages/Docker/target/usr/bin && docker exec sanguo_vnpy pkill -f test_server'",
        "ssh admin@192.168.2.154 'export PATH=$PATH:/var/packages/Docker/target/usr/bin && docker exec sanguo_vnpy pkill -f backtest'",
        "ssh admin@192.168.2.154 'export PATH=$PATH:/var/packages/Docker/target/usr/bin && docker exec sanguo_vnpy pkill -f uvicorn'",
    ]
    
    for cmd in cmds:
        subprocess.run(cmd, shell=True, capture_output=True)
        time.sleep(1)
    
    print("✅ 旧进程清理完成")
    time.sleep(2)
    
    # 启动RPC服务（包含vnpy.app兼容性）
    print("\n🚀 启动RPC服务（端口8002，包含vnpy.app兼容性）...")
    cmd = "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_new_port.py &\"'"
    subprocess.run(cmd, shell=True)
    time.sleep(3)
    
    # 启动API服务
    print("🚀 启动API服务（端口8088）...")
    cmd = "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 -m uvicorn backtest_api_new_port:app --host 0.0.0.0 --port 8088 &\"'"
    subprocess.run(cmd, shell=True)
    time.sleep(3)
    
    print("\n✅ 服务重启完成！")
    print("="*60)
    print("服务信息:")
    print("  RPC服务: tcp://0.0.0.0:8002 (容器内)")
    print("  API服务: http://192.168.2.154:8088")
    print("  API端点: /api/backtest/run")
    print("  文档: /docs")
    print("="*60)
    print("\n📊 数据信息:")
    print("  标的: 510300.SSE")
    print("  数据行数: 3361")
    print("  时间范围: 2012-05-28 -> 2026-03-27")
    print("  数据库: /app/data/database_test.db (容器内)")
    print("  vnpy.app兼容性: ✅ 已内置")
    print("="*60)
    print("\n🎯 现在关羽将军可以开始回测了！")
    print("请求API: POST http://192.168.2.154:8088/api/backtest/run")

if __name__ == "__main__":
    main()
