ForcePilot/backend/package/yuxi/channel/application/service/config_service.py
Kris 6b9fb39807 refactor(channel infra): 重构基础设施代码目录结构
本次提交将原cache、config目录迁移至cache_infra和configuration目录,统一基础设施代码组织:
1. 移动Redis相关缓存实现到cache_infra目录
2. 移动配置相关实现到configuration目录
3. 更新所有模块导入路径和测试文件引用路径
4. 新增对应目录的初始化文件和完整单元测试
5. 重构容器绑定的依赖导入路径
2026-05-31 17:13:08 +08:00

58 lines
1.7 KiB
Python

from __future__ import annotations
import hashlib
import json
from yuxi.channel.domain.middleware.configurable import Configurable
from yuxi.channel.domain.port.config_reload_port import ConfigReloadPort
from yuxi.channel.domain.service.pipeline import Pipeline
from yuxi.channel.infrastructure.configuration.channel_config import ChannelConfig
class ConfigService:
def __init__(
self,
config_data: dict,
config_reload: ConfigReloadPort,
pipeline: Pipeline,
*,
channel_config: ChannelConfig | None = None,
) -> None:
self._config = config_data
self._config_reload = config_reload
self._pipeline = pipeline
self._channel_config = channel_config
async def reload(self) -> tuple[list[str], str | None]:
reloaded = await self._config_reload.reload()
if reloaded:
self._config = reloaded
updated = self._update_configurable_middlewares()
if self._channel_config:
items = await self._channel_config.on_config_updated(self._config)
if items:
updated.extend(items)
config_hash = hashlib.sha256(
json.dumps(self._config, sort_keys=True).encode(),
).hexdigest()[:8]
return updated, config_hash
@property
def config(self) -> dict:
return self._config
def _update_configurable_middlewares(self) -> list[str]:
updated: list[str] = []
for mw in self._pipeline.middlewares:
if isinstance(mw, Configurable):
items = mw.on_config_updated(self._config)
if items:
updated.extend(items)
return updated