2026-03-31 04:31:47 +00:00
|
|
|
import asyncio
|
|
|
|
|
|
|
|
|
|
from fastapi import FastAPI
|
|
|
|
|
from sqlmodel import Session, select
|
|
|
|
|
|
2026-04-04 16:29:37 +00:00
|
|
|
from core.cache import cache
|
2026-03-31 04:31:47 +00:00
|
|
|
from core.database import engine, init_database
|
|
|
|
|
from core.docker_instance import docker_manager
|
|
|
|
|
from core.settings import DATABASE_URL_DISPLAY, REDIS_ENABLED
|
|
|
|
|
from models.bot import BotInstance
|
|
|
|
|
from services.bot_storage_service import _migrate_bot_resources_store
|
|
|
|
|
from services.platform_service import prune_expired_activity_events
|
|
|
|
|
from services.runtime_service import docker_callback, set_main_loop
|
|
|
|
|
|
|
|
|
|
|
2026-04-04 16:29:37 +00:00
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
|
2026-03-31 04:31:47 +00:00
|
|
|
def register_app_runtime(app: FastAPI) -> None:
|
|
|
|
|
@app.on_event("startup")
|
|
|
|
|
async def _on_startup() -> None:
|
|
|
|
|
print(
|
|
|
|
|
f"🚀 Dashboard Backend 启动中... (DB: {DATABASE_URL_DISPLAY}, REDIS: {'Enabled' if REDIS_ENABLED else 'Disabled'})"
|
|
|
|
|
)
|
|
|
|
|
current_loop = asyncio.get_running_loop()
|
|
|
|
|
app.state.main_loop = current_loop
|
|
|
|
|
set_main_loop(current_loop)
|
|
|
|
|
init_database()
|
|
|
|
|
with Session(engine) as session:
|
|
|
|
|
prune_expired_activity_events(session, force=True)
|
|
|
|
|
bots = session.exec(select(BotInstance)).all()
|
|
|
|
|
for bot in bots:
|
|
|
|
|
_migrate_bot_resources_store(bot.id)
|
|
|
|
|
docker_manager.ensure_monitor(bot.id, docker_callback)
|
|
|
|
|
print("✅ 启动自检完成")
|