Skip to content

Commit 44b3b5c

Browse files
committed
Use wait_for_first in the LoggingConfigUpdatingActor
We now wait for the first configuration using the new `wait_for_first()` utility function. We also make the logging more explicity about when the old configuration is being kept and why. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent a98b953 commit 44b3b5c

File tree

1 file changed

+37
-15
lines changed

1 file changed

+37
-15
lines changed

src/frequenz/sdk/config/_logging_actor.py

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import marshmallow.validate
1212

1313
from ..actor import Actor
14-
from ._manager import ConfigManager
14+
from ._manager import ConfigManager, wait_for_first
1515

1616
_logger = logging.getLogger(__name__)
1717

@@ -143,23 +143,45 @@ def __init__(
143143
datefmt=log_datefmt,
144144
level=logging.INFO,
145145
)
146-
self._update_logging(self._current_config)
146+
_logger.info("Applying initial default logging configuration...")
147+
self._reconfigure(self._current_config)
147148

148149
async def _run(self) -> None:
149150
"""Listen for configuration changes and update logging."""
150-
async for new_config in self._config_receiver:
151-
match new_config:
152-
case None:
153-
# When we receive None, we want to reset the logging configuration
154-
# to the default
155-
self._update_logging(LoggingConfig())
156-
case LoggingConfig():
157-
self._update_logging(new_config)
158-
case Exception():
159-
# We ignore errors and just keep the old configuration
160-
pass
161-
case unexpected:
162-
assert_never(unexpected)
151+
self._reconfigure(
152+
await wait_for_first(
153+
self._config_receiver, receiver_name=str(self), allow_none=True
154+
)
155+
)
156+
async for config_update in self._config_receiver:
157+
self._reconfigure(config_update)
158+
159+
def _reconfigure(self, config_update: LoggingConfig | Exception | None) -> None:
160+
"""Update the logging configuration.
161+
162+
Args:
163+
config_update: The new configuration, or an exception if there was an error
164+
parsing the configuration, or `None` if the configuration was unset.
165+
"""
166+
match config_update:
167+
case LoggingConfig():
168+
_logger.info(
169+
"New configuration received, updating logging configuration."
170+
)
171+
self._update_logging(config_update)
172+
case None:
173+
_logger.info(
174+
"Configuration was unset, resetting to the default "
175+
"logging configuration."
176+
)
177+
self._update_logging(LoggingConfig())
178+
case Exception():
179+
_logger.info(
180+
"New configuration has errors, keeping the old logging "
181+
"configuration."
182+
)
183+
case unexpected:
184+
assert_never(unexpected)
163185

164186
def _update_logging(self, config: LoggingConfig) -> None:
165187
"""Configure the logging level."""

0 commit comments

Comments
 (0)