Skip to content

Commit

Permalink
Use match to process the file watcher events
Browse files Browse the repository at this point in the history
We also improve the logging by having a custom message for each type of
update.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
  • Loading branch information
llucax committed Aug 7, 2023
1 parent f22a5c0 commit 3c725e7
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions src/frequenz/sdk/actor/_config_managing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import pathlib
import tomllib
from collections import abc
from typing import Any
from typing import Any, assert_never

from frequenz.channels import Sender
from frequenz.channels.util import FileWatcher
Expand Down Expand Up @@ -84,13 +84,31 @@ async def run(self) -> None:
await self.send_config()

async for event in self._file_watcher:
if event.type != FileWatcher.EventType.DELETE:
# Since we are watching the whole parent directory, we need to make sure
# we only react to events related to the configuration file.
if event.path == self._config_path:
# Since we are watching the whole parent directory, we need to make sure
# we only react to events related to the configuration file.
if event.path != self._config_path:
continue

match event.type:
case FileWatcher.EventType.CREATE:
_logger.info(
"%s: The configuration file %s was created, sending new config...",
self,
self._config_path,
)
await self.send_config()
case FileWatcher.EventType.MODIFY:
_logger.info(
"%s: Update configs, because file %s was modified.",
"%s: The configuration file %s was modified, sending update...",
self,
self._config_path,
)
await self.send_config()
case FileWatcher.EventType.DELETE:
_logger.info(
"%s: The configuration file %s was deleted, ignoring...",
self,
self._config_path,
)
case _:
assert_never(event.type)

0 comments on commit 3c725e7

Please sign in to comment.