Skip to content

Commit d40f9ca

Browse files
committed
Migrate ConfigManagingActor to the Actor class
Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 4f408b3 commit d40f9ca

File tree

2 files changed

+28
-27
lines changed

2 files changed

+28
-27
lines changed

src/frequenz/sdk/actor/_config_managing.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@
1212
from frequenz.channels import Sender
1313
from frequenz.channels.util import FileWatcher
1414

15-
from ..actor._decorator import actor
15+
from ..actor._actor import Actor
1616
from ..config import Config
1717

1818
_logger = logging.getLogger(__name__)
1919

2020

21-
@actor
22-
class ConfigManagingActor:
21+
class ConfigManagingActor(Actor):
2322
"""An actor that monitors a TOML configuration file for updates.
2423
2524
When the file is updated, the new configuration is sent, as a [`dict`][], to the
@@ -44,6 +43,7 @@ def __init__(
4443
output: The sender to send the config to.
4544
event_types: The set of event types to monitor.
4645
"""
46+
super().__init__()
4747
self._config_path: pathlib.Path = (
4848
config_path
4949
if isinstance(config_path, pathlib.Path)
@@ -79,8 +79,13 @@ async def send_config(self) -> None:
7979
config = Config(conf_vars)
8080
await self._output.send(config)
8181

82-
async def run(self) -> None:
83-
"""Monitor for and send configuration file updates."""
82+
async def _run(self) -> None:
83+
"""Monitor for and send configuration file updates.
84+
85+
At startup, the Config Manager sends the current config so that it
86+
can be cache in the Broadcast channel and served to receivers even if
87+
there hasn't been any change to the config file itself.
88+
"""
8489
await self.send_config()
8590

8691
async for event in self._file_watcher:

tests/actor/test_config_manager.py

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -80,27 +80,23 @@ async def test_update(self, config_file: pathlib.Path) -> None:
8080
config_channel: Broadcast[Config] = Broadcast(
8181
"Config Channel", resend_latest=True
8282
)
83-
_config_manager = ConfigManagingActor(config_file, config_channel.new_sender())
84-
8583
config_receiver = config_channel.new_receiver()
8684

87-
config = await config_receiver.receive()
88-
assert config is not None
89-
assert config.get("logging_lvl") == "DEBUG"
90-
assert config.get("var1") == "1"
91-
assert config.get("var2") is None
92-
assert config.get("var3") is None
93-
94-
number = 5
95-
config_file.write_text(create_content(number=number))
96-
97-
config = await config_receiver.receive()
98-
assert config is not None
99-
assert config.get("logging_lvl") == "ERROR"
100-
assert config.get("var1") == "0"
101-
assert config.get("var2") == str(number)
102-
assert config.get("var3") is None
103-
assert config_file.read_text() == create_content(number=number)
104-
105-
# pylint: disable=protected-access,no-member
106-
await _config_manager._stop() # type: ignore
85+
async with ConfigManagingActor(config_file, config_channel.new_sender()):
86+
config = await config_receiver.receive()
87+
assert config is not None
88+
assert config.get("logging_lvl") == "DEBUG"
89+
assert config.get("var1") == "1"
90+
assert config.get("var2") is None
91+
assert config.get("var3") is None
92+
93+
number = 5
94+
config_file.write_text(create_content(number=number))
95+
96+
config = await config_receiver.receive()
97+
assert config is not None
98+
assert config.get("logging_lvl") == "ERROR"
99+
assert config.get("var1") == "0"
100+
assert config.get("var2") == str(number)
101+
assert config.get("var3") is None
102+
assert config_file.read_text() == create_content(number=number)

0 commit comments

Comments
 (0)