fix: Make watch setup resilient to unmounted paths

Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
2026-04-24 10:20:15 -06:00
parent d811483e94
commit 941fb535f6

30
main.py
View File

@@ -31,16 +31,14 @@ def trigger_scan():
def add_watch_recursive(inotify, path, watch_flags, wd_to_path): def add_watch_recursive(inotify, path, watch_flags, wd_to_path):
"""Walk a directory path and add watches recursively.""" """Walk a directory path and add watches recursively."""
try: logging.info(f"Watching {path} and its subdirectories recursively.")
for root, _, _ in os.walk(path): for root, _, _ in os.walk(path):
try: try:
logging.info(f"Watching {root}") logging.info(f"Watching {root}")
wd = inotify.add_watch(root, watch_flags) wd = inotify.add_watch(root, watch_flags)
wd_to_path[wd] = root wd_to_path[wd] = root
except OSError as e: except OSError as e:
logging.error(f"Error adding watch for {root}: {e}") logging.error(f"Error adding watch for {root}: {e}")
except FileNotFoundError:
logging.warning(f"Could not scan {path} as it was not found.")
def main(): def main():
@@ -49,12 +47,16 @@ def main():
wd_to_path = {} wd_to_path = {}
with INotify() as inotify: with INotify() as inotify:
for path in secrets.WATCH_PATHS:
logging.info(f"Watching {path} and its subdirectories recursively.")
add_watch_recursive(inotify, path, watch_flags, wd_to_path)
try: try:
while True: while True:
watched_dirs = set(wd_to_path.values())
for path in secrets.WATCH_PATHS:
if path not in watched_dirs:
if os.path.exists(path):
add_watch_recursive(inotify, path, watch_flags, wd_to_path)
else:
logging.warning(f"Watch path {path} does not exist. Will retry.")
events = inotify.read(timeout=1000) events = inotify.read(timeout=1000)
if events: if events:
scan_needed = False scan_needed = False