Skip to content

Commit 3c725e7

Browse files
committed
Use match to process the file watcher events
We also improve the logging by having a custom message for each type of update. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent f22a5c0 commit 3c725e7

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

src/frequenz/sdk/actor/_config_managing.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import pathlib
88
import tomllib
99
from collections import abc
10-
from typing import Any
10+
from typing import Any, assert_never
1111

1212
from frequenz.channels import Sender
1313
from frequenz.channels.util import FileWatcher
@@ -84,13 +84,31 @@ async def run(self) -> None:
8484
await self.send_config()
8585

8686
async for event in self._file_watcher:
87-
if event.type != FileWatcher.EventType.DELETE:
88-
# Since we are watching the whole parent directory, we need to make sure
89-
# we only react to events related to the configuration file.
90-
if event.path == self._config_path:
87+
# Since we are watching the whole parent directory, we need to make sure
88+
# we only react to events related to the configuration file.
89+
if event.path != self._config_path:
90+
continue
91+
92+
match event.type:
93+
case FileWatcher.EventType.CREATE:
94+
_logger.info(
95+
"%s: The configuration file %s was created, sending new config...",
96+
self,
97+
self._config_path,
98+
)
99+
await self.send_config()
100+
case FileWatcher.EventType.MODIFY:
91101
_logger.info(
92-
"%s: Update configs, because file %s was modified.",
102+
"%s: The configuration file %s was modified, sending update...",
93103
self,
94104
self._config_path,
95105
)
96106
await self.send_config()
107+
case FileWatcher.EventType.DELETE:
108+
_logger.info(
109+
"%s: The configuration file %s was deleted, ignoring...",
110+
self,
111+
self._config_path,
112+
)
113+
case _:
114+
assert_never(event.type)

0 commit comments

Comments
 (0)