ForcePilot/backend/package/yuxi/channels/services/maintenance.py
Kris fb8bd90329 refactor(channel-services): 整理导入顺序并新增插件状态清理功能
1. 调整多个服务文件的导入顺序以符合规范
2. 新增Postgres插件状态存储实现,支持CRUD和过期清理
3. 在维护任务中添加插件过期状态自动清理逻辑
4. 新增统计收集器,支持请求/错误计数、响应时间统计和指标上报
2026-05-13 16:21:09 +08:00

43 lines
1.4 KiB
Python

from __future__ import annotations
import asyncio
from typing import TYPE_CHECKING
from yuxi.utils.logging_config import logger
if TYPE_CHECKING:
from yuxi.channels.manager import ChannelManager
from yuxi.channels.services.runtime_state import RuntimeState
class MaintenanceRunner:
def __init__(self, channel_manager: ChannelManager, state: RuntimeState):
self._manager = channel_manager
self._state = state
async def run(self, interval: float = 600) -> None:
logger.info("MaintenanceRunner started")
while True:
await asyncio.sleep(interval)
try:
await self._run_maintenance()
except asyncio.CancelledError:
break
except Exception:
logger.exception("MaintenanceRunner error")
async def _run_maintenance(self) -> None:
for channel_id, adapter in self._manager._adapters.items():
try:
await adapter._refresh_token_if_needed()
except Exception:
logger.debug(f"Token refresh skip for {channel_id}")
if self._manager._state_store is not None:
try:
await self._manager._state_store.cleanup_expired()
except Exception:
logger.debug("Plugin state cleanup failed")
logger.debug("Maintenance cycle complete")