From 11252e0f3dc9b7894c4e30c91f7f7e2c3fd6771a Mon Sep 17 00:00:00 2001 From: cfdaily Date: Sun, 17 May 2026 05:46:05 +0800 Subject: [PATCH] auto-sync: 2026-05-17 05:46:05 --- src/main.py | 55 +++++++++++++++++++++++++++++------------------------ 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/src/main.py b/src/main.py index 5b227f3..1577a6b 100644 --- a/src/main.py +++ b/src/main.py @@ -2,7 +2,6 @@ from __future__ import annotations -import asyncio import logging from contextlib import asynccontextmanager from pathlib import Path @@ -13,6 +12,9 @@ from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from fastapi.staticfiles import StaticFiles +from src.blackboard.registry import ProjectRegistry +from src.daemon.ticker import Ticker + logger = logging.getLogger("moziplus-v2") # --------------------------------------------------------------------------- @@ -33,26 +35,22 @@ def load_config() -> dict: config = load_config() # --------------------------------------------------------------------------- -# Daemon ticker(占位,F6 实现) +# 全局组件 # --------------------------------------------------------------------------- -_ticker_task: Optional[asyncio.Task] = None +DATA_ROOT = Path(config.get("data_root", Path(__file__).parent.parent / "data")) + +ticker: Optional[Ticker] = None -async def _run_ticker(): - """Daemon ticker 主循环(占位)""" - tick_interval = config.get("daemon", {}).get("tick_interval", 30) - logger.info("Ticker started (interval=%ss)", tick_interval) - while True: - try: - # F6 会在这里注入真正的 tick 逻辑 - await asyncio.sleep(tick_interval) - except asyncio.CancelledError: - logger.info("Ticker cancelled") - return - except Exception: - logger.exception("Tick error") - await asyncio.sleep(tick_interval) +def get_ticker() -> Optional[Ticker]: + """获取全局 Ticker 实例""" + return ticker + + +def get_registry() -> ProjectRegistry: + """获取全局项目注册表""" + return ProjectRegistry(DATA_ROOT) # --------------------------------------------------------------------------- @@ -63,16 +61,23 @@ async def _run_ticker(): @asynccontextmanager async def lifespan(app: FastAPI): """启动 Daemon ticker,关闭时清理""" - global _ticker_task + global ticker logger.info("moziplus-v2 starting...") - _ticker_task = asyncio.create_task(_run_ticker()) + + registry = get_registry() + tick_interval = config.get("daemon", {}).get("tick_interval", 30) + + ticker = Ticker( + registry=registry, + tick_interval=tick_interval, + ) + await ticker.start() + logger.info("Ticker started (interval=%ss)", tick_interval) + yield - if _ticker_task: - _ticker_task.cancel() - try: - await _ticker_task - except asyncio.CancelledError: - pass + + if ticker: + await ticker.stop() logger.info("moziplus-v2 stopped")