Skip to content

Commit

Permalink
Change FileWatcher to stop printing watchfiles debug log
Browse files Browse the repository at this point in the history
The underlying Rust code performs periodic checks to see
if it should stop watching. These checks raise TimeoutError,
which by default would log misleading debug messages.
Setting yield_on_timeout=True makes it yield an empty
set instead of printing debug logs.
We handle these empty sets in the `ready()` method by
continuing to wait.
  • Loading branch information
ela-kotulska-frequenz committed Nov 21, 2024
1 parent b3c98e5 commit 8d6bb4e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/frequenz/channels/file_watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ def __init__(
watch_filter=self._filter_events,
force_polling=force_polling,
poll_delay_ms=int(polling_interval.total_seconds() * 1_000),
# The underlying Rust code performs periodic checks to see if it should stop watching.
# These checks raise TimeoutError, which by default would log misleading debug messages.
# Setting yield_on_timeout=True makes it yield an empty set instead of printing debug
# logs. We handle these empty sets in the `ready()` method by continuing to wait.
yield_on_timeout=True,
)
self._awatch_stopped_exc: Exception | None = None
self._changes: set[FileChange] = set()
Expand Down Expand Up @@ -204,11 +209,16 @@ async def ready(self) -> bool:
if self._awatch_stopped_exc is not None:
return False

try:
self._changes = await anext(self._awatch)
except StopAsyncIteration as err:
self._awatch_stopped_exc = err
return False
# awatch will yield an empty set if rust notify timeout is reached.
# This is safety heartbeat mechanism for Windows.
# But on non-Windows systems, we don't need it, so we can ignore it.
# For more details, refer awatch documentation.
while len(self._changes) == 0:
try:
self._changes = await anext(self._awatch)
except StopAsyncIteration as err:
self._awatch_stopped_exc = err
return False

return True

Expand Down
1 change: 1 addition & 0 deletions tests/test_file_watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ async def test_file_watcher_filter_events(
watch_filter=filter_events,
force_polling=True,
poll_delay_ms=1_000,
yield_on_timeout=True,
)
]
for event_type in EventType:
Expand Down

0 comments on commit 8d6bb4e

Please sign in to comment.