Skip to content

Commit ecd11b8

Browse files
Add polling parameters to ConfigManagingActor
The ConfigManagingActor now allows to force file polling and set the polling interval. Signed-off-by: Daniel Zullo <[email protected]>
1 parent 2e7a096 commit ecd11b8

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

RELEASE_NOTES.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010

1111
## New Features
1212

13-
<!-- Here goes the main new features and examples or instructions on how to use them -->
13+
- `ConfigManagingActor`: The file polling mechanism is now forced by default. Two new parameters have been added:
14+
- `force_polling`: Whether to force file polling to check for changes. Default is `True`.
15+
- `polling_interval`: The interval to check for changes. Only relevant if polling is enabled. Default is 1 second.
1416

1517
## Bug Fixes
1618

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ dependencies = [
3030
# changing the version
3131
# (plugins.mkdocstrings.handlers.python.import)
3232
"frequenz-client-microgrid >= 0.5.1, < 0.6.0",
33-
"frequenz-channels >= 1.1.0, < 2.0.0",
33+
"frequenz-channels >= 1.2.0, < 2.0.0",
3434
"networkx >= 2.8, < 4",
3535
"numpy >= 1.26.4, < 2",
3636
"typing_extensions >= 4.6.1, < 5",

src/frequenz/sdk/config/_config_managing.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import pathlib
88
import tomllib
99
from collections import abc
10+
from datetime import timedelta
1011
from typing import Any, assert_never
1112

1213
from frequenz.channels import Sender
@@ -29,13 +30,16 @@ class ConfigManagingActor(Actor):
2930
reading too.
3031
"""
3132

33+
# pylint: disable-next=too-many-arguments
3234
def __init__(
3335
self,
3436
config_path: pathlib.Path | str,
3537
output: Sender[abc.Mapping[str, Any]],
3638
event_types: abc.Set[EventType] = frozenset(EventType),
3739
*,
3840
name: str | None = None,
41+
force_polling: bool = True,
42+
polling_interval: timedelta = timedelta(seconds=1),
3943
) -> None:
4044
"""Initialize this instance.
4145
@@ -45,6 +49,9 @@ def __init__(
4549
event_types: The set of event types to monitor.
4650
name: The name of the actor. If `None`, `str(id(self))` will
4751
be used. This is used mostly for debugging purposes.
52+
force_polling: Whether to force file polling to check for changes.
53+
polling_interval: The interval to poll for changes. Only relevant if
54+
polling is enabled.
4855
"""
4956
super().__init__(name=name)
5057
self._config_path: pathlib.Path = (
@@ -54,6 +61,8 @@ def __init__(
5461
)
5562
self._output: Sender[abc.Mapping[str, Any]] = output
5663
self._event_types: abc.Set[EventType] = event_types
64+
self._force_polling: bool = force_polling
65+
self._polling_interval: timedelta = polling_interval
5766

5867
def _read_config(self) -> abc.Mapping[str, Any]:
5968
"""Read the contents of the configuration file.
@@ -89,7 +98,10 @@ async def _run(self) -> None:
8998
# parent directory instead just in case a configuration file doesn't exist yet
9099
# or it is deleted and recreated again.
91100
file_watcher = FileWatcher(
92-
paths=[self._config_path.parent], event_types=self._event_types
101+
paths=[self._config_path.parent],
102+
event_types=self._event_types,
103+
force_polling=self._force_polling,
104+
polling_interval=self._polling_interval,
93105
)
94106

95107
try:

tests/actor/test_config_manager.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ async def test_update(self, config_file: pathlib.Path) -> None:
7171
)
7272
config_receiver = config_channel.new_receiver()
7373

74-
async with ConfigManagingActor(config_file, config_channel.new_sender()):
74+
async with ConfigManagingActor(
75+
config_file, config_channel.new_sender(), force_polling=False
76+
):
7577
config = await config_receiver.receive()
7678
assert config is not None
7779
assert config.get("logging_lvl") == "DEBUG"
@@ -100,7 +102,9 @@ async def test_update_relative_path(self, config_file: pathlib.Path) -> None:
100102
current_dir = pathlib.Path.cwd()
101103
relative_path = os.path.relpath(config_file, current_dir)
102104

103-
async with ConfigManagingActor(relative_path, config_channel.new_sender()):
105+
async with ConfigManagingActor(
106+
relative_path, config_channel.new_sender(), force_polling=False
107+
):
104108
config = await config_receiver.receive()
105109
assert config is not None
106110
assert config.get("var2") is None

0 commit comments

Comments
 (0)