diff --git a/src/akkudoktoreos/core/dataabc.py b/src/akkudoktoreos/core/dataabc.py index 753407be..fed9ab69 100644 --- a/src/akkudoktoreos/core/dataabc.py +++ b/src/akkudoktoreos/core/dataabc.py @@ -2483,13 +2483,18 @@ class DataContainer(SingletonMixin, DataABC, Generic[DataProviderT]): concurrent writes or other bulk operations can interleave with the provider updates. + A failing provider does not stop the update: the remaining providers are + updated first, then the failures of the enabled providers are raised together. + Args: force_enable: If True, forces the update even if a provider is disabled. force_update: If True, forces providers to update even if data is cached. Raises: - RuntimeError: If an enabled provider fails during update. + RuntimeError: If one or more enabled providers fail during update, after + all providers have been updated. """ + errors: list[str] = [] async with self._container_lock: for provider in self.providers: try: @@ -2504,13 +2509,16 @@ class DataContainer(SingletonMixin, DataABC, Generic[DataProviderT]): f":\n{e}\n{trace}" ) if provider.enabled(): - # The active provider failed — this is a real error worth propagating. + # The active provider failed — this is a real error worth propagating, + # but only after the remaining providers were updated. logger.error(error) - raise RuntimeError(error) + errors.append(error) else: # A non-active provider failed (e.g. missing config while force_enable=True). # Log as warning and continue so the remaining providers still run. logger.warning(error) + if errors: + raise RuntimeError("\n".join(errors)) async def key_to_raw_series( self, diff --git a/tests/test_dataabccontainer.py b/tests/test_dataabccontainer.py index 377b0135..84b24eb9 100644 --- a/tests/test_dataabccontainer.py +++ b/tests/test_dataabccontainer.py @@ -63,6 +63,27 @@ class DerivedDataProvider(DataProvider[DerivedRecord]): DerivedDataProvider.provider_updated = True +class FailingDataProvider(DataProvider[DerivedRecord]): + """Enabled DataProvider whose update fails, like a remote API answering HTTP 500.""" + + records: List[DerivedRecord] = Field( + default_factory=list, description="List of DerivedRecord records" + ) + + @classmethod + def record_class(cls) -> Any: + return DerivedRecord + + def provider_id(self) -> str: + return "FailingDataProvider" + + def enabled(self) -> bool: + return True + + async def _update_data(self, force_update: Optional[bool] = False) -> None: + raise RuntimeError("500 Server Error") + + class DerivedDataContainer(DataContainer): providers: List[Union[DerivedDataProvider, DataProvider]] = Field( default_factory=list, description="List of data providers" @@ -260,6 +281,17 @@ class TestDataContainer: assert DerivedDataProvider.provider_updated is True DerivedDataProvider.provider_enabled = True # restore + async def test_update_data_failing_provider_does_not_skip_the_others( + self, populated + ) -> None: + """A failing enabled provider is reported after all others were updated (#1571).""" + populated.providers.insert(0, FailingDataProvider()) + DerivedDataProvider.provider_enabled = True + DerivedDataProvider.provider_updated = False + with pytest.raises(RuntimeError, match="Provider FailingDataProvider fails on update"): + await populated.update_data() + assert DerivedDataProvider.provider_updated is True + # ----------------------------------------------------------------------- # save / load # -----------------------------------------------------------------------