ForcePilot/backend/package/yuxi/channel/application/service/config_service.py

58 lines
1.7 KiB
Python
Raw Normal View History

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