from collections.abc import Awaitable, Callable from dataclasses import dataclass from plba.runtime.worker.models import ComponentHealth HealthReader = Callable[[], Awaitable[ComponentHealth]] @dataclass(frozen=True, slots=True) class HealthContributor: name: str health_reader: HealthReader critical: bool = True class DuplicateHealthContributorError(ValueError): pass class HealthContributorRegistry: def __init__(self) -> None: self._contributors: dict[str, HealthContributor] = {} def register(self, contributor: HealthContributor) -> None: if not contributor.name or contributor.name.strip() != contributor.name: raise ValueError("health contributor name must be non-empty") if contributor.name in self._contributors: raise DuplicateHealthContributorError( f"health contributor already registered: {contributor.name}" ) self._contributors[contributor.name] = contributor def all(self) -> tuple[HealthContributor, ...]: return tuple(self._contributors.values())