auto-sync: 2026-05-17 05:46:05
This commit is contained in:
+30
-25
@@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import asyncio
|
|
||||||
import logging
|
import logging
|
||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
@@ -13,6 +12,9 @@ from fastapi import FastAPI
|
|||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
|
|
||||||
|
from src.blackboard.registry import ProjectRegistry
|
||||||
|
from src.daemon.ticker import Ticker
|
||||||
|
|
||||||
logger = logging.getLogger("moziplus-v2")
|
logger = logging.getLogger("moziplus-v2")
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
@@ -33,26 +35,22 @@ def load_config() -> dict:
|
|||||||
config = load_config()
|
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():
|
def get_ticker() -> Optional[Ticker]:
|
||||||
"""Daemon ticker 主循环(占位)"""
|
"""获取全局 Ticker 实例"""
|
||||||
tick_interval = config.get("daemon", {}).get("tick_interval", 30)
|
return ticker
|
||||||
logger.info("Ticker started (interval=%ss)", tick_interval)
|
|
||||||
while True:
|
|
||||||
try:
|
def get_registry() -> ProjectRegistry:
|
||||||
# F6 会在这里注入真正的 tick 逻辑
|
"""获取全局项目注册表"""
|
||||||
await asyncio.sleep(tick_interval)
|
return ProjectRegistry(DATA_ROOT)
|
||||||
except asyncio.CancelledError:
|
|
||||||
logger.info("Ticker cancelled")
|
|
||||||
return
|
|
||||||
except Exception:
|
|
||||||
logger.exception("Tick error")
|
|
||||||
await asyncio.sleep(tick_interval)
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
@@ -63,16 +61,23 @@ async def _run_ticker():
|
|||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def lifespan(app: FastAPI):
|
async def lifespan(app: FastAPI):
|
||||||
"""启动 Daemon ticker,关闭时清理"""
|
"""启动 Daemon ticker,关闭时清理"""
|
||||||
global _ticker_task
|
global ticker
|
||||||
logger.info("moziplus-v2 starting...")
|
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
|
yield
|
||||||
if _ticker_task:
|
|
||||||
_ticker_task.cancel()
|
if ticker:
|
||||||
try:
|
await ticker.stop()
|
||||||
await _ticker_task
|
|
||||||
except asyncio.CancelledError:
|
|
||||||
pass
|
|
||||||
logger.info("moziplus-v2 stopped")
|
logger.info("moziplus-v2 stopped")
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user