|
8 | 8 | from collections.abc import Generator, Sequence
|
9 | 9 |
|
10 | 10 | from watchdog.events import FileSystemEvent, PatternMatchingEventHandler
|
11 |
| -from watchdog.observers import Observer |
| 11 | +from watchdog.observers.inotify import InotifyObserver |
| 12 | +from watchdog.observers.inotify_buffer import InotifyBuffer |
12 | 13 |
|
13 | 14 | from ._cache import Cache, CacheError
|
14 | 15 | from ._config import WatcherConfig
|
15 | 16 | from ._log import LOGGER
|
16 | 17 |
|
| 18 | +# When a file or folder is moved, two events are created: IN_MOVED_FROM and IN_MOVED_TO. |
| 19 | +# watchdog tries to combine these two events into a single move event. In case it received |
| 20 | +# IN_MOVED_FROM but not yet in IN_MOVED_TO, it waits up `InotifyBuffer.delay` seconds for |
| 21 | +# IN_MOVED_TO, thereby delaying the reporting of other events. This might be an actual problem |
| 22 | +# for us if an automation call is triggered right after files relevant to this call were modified. |
| 23 | +# In such scenarios, the reload notification from the watcher might arrive too late if watchdog |
| 24 | +# adds some delay. As a result, the automation call uses an outdated configuration. |
| 25 | +# See also https://man7.org/linux/man-pages/man7/inotify.7.html, "Dealing with rename() events" |
| 26 | +InotifyBuffer.delay = 0.0 |
| 27 | + |
17 | 28 |
|
18 | 29 | @contextlib.contextmanager
|
19 | 30 | def run(config: WatcherConfig, cache: Cache) -> Generator[None]:
|
20 | 31 | LOGGER.info("[watcher] Initializing")
|
21 |
| - observer = Observer() |
| 32 | + observer = InotifyObserver(generate_full_events=True) |
22 | 33 | for schedule in config.schedules:
|
23 | 34 | handler = _AutomationWatcherHandler(
|
24 | 35 | cache=cache,
|
|
0 commit comments