Skip to content

Commit b606d98

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 177af6a commit b606d98

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

@@ -141,23 +141,45 @@ def __init__(
141141
datefmt=log_datefmt,
142142
level=logging.INFO,
143143
)
144-
self._update_logging(self._current_config)
144+
_logger.info("Applying initial default logging configuration...")
145+
self._reconfigure(self._current_config)
145146

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

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

0 commit comments

Comments
 (0)