6931a7b541
架构(简化,避per-account闭包注入): - live_orchestrator live_step(account_id)自包含: 恢复cash/positions/pending→fetch_day raw当日→engine.step→存状态 - run_live_step(db)遍历live accounts调live_step; scheduler register_live_step_job全局20:30 job - app startup注册全局job; routes create mode=live存account running(不跑回放) - TODO(分期项): prev_close昨日raw/listing_days IPO算/realized_pnl恢复 - 113/113通过, live_orchestrator import OK
50 lines
1.8 KiB
Python
50 lines
1.8 KiB
Python
"""
|
||
FastAPI application factory for Sanguo Quant API
|
||
"""
|
||
from fastapi import FastAPI
|
||
from .routes import router, set_orchestrator, set_auth_config
|
||
from .routes_paper import router as paper_router, set_db_path
|
||
from .auth import set_jwt_config
|
||
from .ws import manager
|
||
from sanguo_orchestrator.runner import Orchestrator
|
||
|
||
|
||
def create_app(db_path: str, file_dir=None, auth_config=None, max_workers: int = 2) -> FastAPI:
|
||
"""Create FastAPI application with orchestrator and optional authentication"""
|
||
app = FastAPI(title="Sanguo Quant API")
|
||
|
||
# Initialize orchestrator
|
||
orch = Orchestrator(db_path=db_path, file_dir=file_dir, max_workers=max_workers)
|
||
|
||
# Set up WebSocket stage callback
|
||
async def _on_stage(task_id, stage):
|
||
"""Broadcast stage updates to WebSocket subscribers"""
|
||
await manager.broadcast(task_id, {"task_id": task_id, "stage": stage})
|
||
|
||
orch.set_on_stage(_on_stage)
|
||
set_orchestrator(orch)
|
||
|
||
# Configure authentication if provided
|
||
if auth_config:
|
||
set_auth_config(auth_config)
|
||
set_jwt_config(
|
||
auth_config.get("jwt_secret", "x"),
|
||
auth_config.get("expire_minutes", 60)
|
||
)
|
||
|
||
# Include routes
|
||
app.include_router(router, prefix="/api/v1")
|
||
app.include_router(paper_router, prefix="/api/v1")
|
||
set_db_path(db_path)
|
||
|
||
@app.on_event("startup")
|
||
def _register_live_step():
|
||
"""容器启动:注册每日 20:30 全局实走 step job(C-S3,遍历 live accounts)。"""
|
||
try:
|
||
from sanguo_trader.scheduler import register_live_step_job
|
||
register_live_step_job(db_path)
|
||
except Exception as e: # noqa: BLE001 本机无 apscheduler 时跳过
|
||
import logging
|
||
logging.getLogger(__name__).warning("register_live_step_job 失败: %s", e)
|
||
|
||
return app |