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

16
main.py
View File

@@ -31,7 +31,7 @@ def trigger_scan():
def add_watch_recursive(inotify, path, watch_flags, wd_to_path):
"""Walk a directory path and add watches recursively."""
try:
logging.info(f"Watching {path} and its subdirectories recursively.")
for root, _, _ in os.walk(path):
try:
logging.info(f"Watching {root}")
@@ -39,8 +39,6 @@ def add_watch_recursive(inotify, path, watch_flags, wd_to_path):
wd_to_path[wd] = root
except OSError as 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():
@@ -49,12 +47,16 @@ def main():
wd_to_path = {}
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:
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)
if events:
scan_needed = False