import asyncio from fastapi import FastAPI from sqlmodel import Session, select from core.cache import cache from core.database import engine, init_database from core.docker_instance import docker_manager from core.speech_service import inspect_speech_model_status from core.settings import DATABASE_URL_DISPLAY, REDIS_ENABLED from models.bot import BotInstance from services.default_assets_service import validate_runtime_data_assets from services.platform_activity_service import prune_expired_activity_events from services.platform_settings_service import get_speech_runtime_settings from services.runtime_service import docker_callback, set_main_loop def reload_platform_runtime(app: FastAPI) -> None: cache.delete_prefix("") speech_service = getattr(app.state, "speech_service", None) if speech_service is not None and hasattr(speech_service, "reset_runtime"): speech_service.reset_runtime() def register_app_runtime(app: FastAPI) -> None: @app.on_event("startup") async def _on_startup() -> None: redis_state = "Disabled" if REDIS_ENABLED: redis_state = "Connected" if cache.enabled else f"Unavailable ({cache.status})" print( f"🚀 Dashboard Backend 启动中... (DB: {DATABASE_URL_DISPLAY}, REDIS: {redis_state})" ) current_loop = asyncio.get_running_loop() app.state.main_loop = current_loop set_main_loop(current_loop) validate_runtime_data_assets() print("[init] data 目录校验通过") init_database() with Session(engine) as session: prune_expired_activity_events(session, force=True) bots = session.exec(select(BotInstance)).all() for bot in bots: docker_manager.ensure_monitor(bot.id, docker_callback) speech_settings = get_speech_runtime_settings() model_status = inspect_speech_model_status() if speech_settings["enabled"]: if model_status["ready"]: print(f"🎙️ 语音识别模型就绪: {model_status['resolved_path']}") else: hint = f",请将模型文件放到 {model_status['expected_path']}" if model_status["expected_path"] else "" print(f"⚠️ 语音识别模型未就绪: {model_status['message']}{hint}") print("✅ 启动自检完成")