QTrade/scripts/start_services.py

166 lines
4.5 KiB
Python
Raw Normal View History

"""
QTrade 多环境服务启动脚本
支持启动三套独立环境:
- production: 实盘环境 (端口9000+, DB: QTrade_RealTrade, Redis: real:)
- backtest: 回测环境 (端口10000+, DB: QTrade_BackTestTrade, Redis: bt:)
- simulation: 模拟环境 (端口11000+, DB: QTrade_SimuTrade, Redis: simu:)
用法:
# 启动实盘环境的数据服务和策略引擎
python scripts/start_services.py production data_service strategy_engine
# 启动回测环境的所有服务
python scripts/start_services.py backtest data_service strategy_engine backtest_service
# 启动模拟环境
python scripts/start_services.py simulation data_service strategy_engine trade_service
"""
import os
import subprocess
import sys
import time
from pathlib import Path
# 项目根目录
PROJECT_ROOT = Path(__file__).parent.parent
# 环境配置
ENV_CONFIGS = {
"production": {
"env_file": ".env.production",
"config_file": "configs/production.yaml",
"description": "实盘环境",
},
"backtest": {
"env_file": ".env.backtest",
"config_file": "configs/backtest.yaml",
"description": "回测环境",
},
"simulation": {
"env_file": ".env.simulation",
"config_file": "configs/simulation.yaml",
"description": "模拟环境",
},
}
# 服务配置
SERVICE_APPS = {
"data_service": "src.data_service.app:app",
"strategy_engine": "src.strategy_engine.app:app",
"trade_service": "src.trade_service.app:app",
"backtest_service": "src.backtest_service.app:app",
}
def start_service(service_name: str, env: str, detach: bool = True):
"""
启动指定环境的服务
Args:
service_name: 服务名称
env: 环境名称 (production/backtest/simulation)
detach: 是否后台运行
"""
if env not in ENV_CONFIGS:
print(f"❌ 未知环境: {env}")
print(f"支持的环境: {', '.join(ENV_CONFIGS.keys())}")
sys.exit(1)
if service_name not in SERVICE_APPS:
print(f"❌ 未知服务: {service_name}")
print(f"支持的服务: {', '.join(SERVICE_APPS.keys())}")
sys.exit(1)
env_config = ENV_CONFIGS[env]
app_path = SERVICE_APPS[service_name]
env_file = PROJECT_ROOT / env_config["env_file"]
if not env_file.exists():
print(f"❌ 环境配置文件不存在: {env_file}")
sys.exit(1)
print(f"🚀 启动 {env_config['description']} - {service_name}")
# 构建命令
cmd = [
"uvicorn",
app_path,
"--env-file",
str(env_file),
"--reload",
]
# 设置环境变量
env_vars = os.environ.copy()
env_vars["ENV"] = env
env_vars["CONFIG_FILE"] = env_config["config_file"]
if detach:
# Windows 后台运行
if sys.platform == "win32":
# 创建新的控制台窗口
subprocess.Popen(
cmd,
env=env_vars,
creationflags=subprocess.CREATE_NEW_CONSOLE,
cwd=PROJECT_ROOT,
)
else:
# Linux/Mac 后台运行
subprocess.Popen(
cmd,
env=env_vars,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
cwd=PROJECT_ROOT,
)
print(f"✓ {service_name} 已在后台启动")
else:
# 前台运行
subprocess.run(cmd, env=env_vars, cwd=PROJECT_ROOT)
def main():
if len(sys.argv) < 3:
print("用法: python scripts/start_services.py <env> <service1> [service2] ...")
print()
print("环境:")
for env, config in ENV_CONFIGS.items():
print(f" {env:12} - {config['description']}")
print()
print("服务:")
for service in SERVICE_APPS.keys():
print(f" {service}")
print()
print("示例:")
print(
" python scripts/start_services.py production data_service strategy_engine"
)
print(
" python scripts/start_services.py backtest data_service strategy_engine backtest_service"
)
sys.exit(1)
env = sys.argv[1]
services = sys.argv[2:]
print("=" * 60)
print(f"环境: {ENV_CONFIGS[env]['description']}")
print(f"服务: {', '.join(services)}")
print("=" * 60)
print()
for service in services:
start_service(service, env)
time.sleep(1) # 间隔启动
print()
print("✅ 所有服务已启动")
if __name__ == "__main__":
main()