Compare commits

..

3 Commits

Author SHA1 Message Date
d811483e94 fix: Only trigger Immich scan on file events
Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
2025-12-17 11:15:31 -07:00
a92c0464d2 fix: Resolve AttributeError and clean up ignored watch descriptors
Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
2025-12-17 11:09:38 -07:00
a0f3b2668b fix: Manually map inotify watch descriptors to paths to fix AttributeError
Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
2025-12-17 10:58:48 -07:00

33
main.py
View File

@@ -29,13 +29,14 @@ def trigger_scan():
logging.error(f"Failed to trigger scan: {e}")
def add_watch_recursive(inotify, path, watch_flags):
def add_watch_recursive(inotify, path, watch_flags, wd_to_path):
"""Walk a directory path and add watches recursively."""
try:
for root, _, _ in os.walk(path):
try:
logging.info(f"Watching {root}")
inotify.add_watch(root, watch_flags)
wd = inotify.add_watch(root, watch_flags)
wd_to_path[wd] = root
except OSError as e:
logging.error(f"Error adding watch for {root}: {e}")
except FileNotFoundError:
@@ -45,26 +46,40 @@ def add_watch_recursive(inotify, path, watch_flags):
def main():
global timer
watch_flags = flags.MODIFY | flags.CREATE | flags.DELETE | flags.MOVED_FROM | flags.MOVED_TO
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)
add_watch_recursive(inotify, path, watch_flags, wd_to_path)
try:
while True:
events = inotify.read(timeout=1000)
if events:
scan_needed = False
for event in events:
logging.debug(f"Event: {event!r}")
if event.mask & flags.ISDIR and (event.mask & flags.CREATE or event.mask & flags.MOVED_TO):
parent_dir_path_bytes = inotify.paths[event.wd]
new_dir_path_bytes = os.path.join(parent_dir_path_bytes, event.name)
new_dir_path = new_dir_path_bytes.decode('utf-8')
logging.info(f"New directory {new_dir_path} detected, adding watches.")
add_watch_recursive(inotify, new_dir_path, watch_flags)
if event.mask & flags.IGNORED:
logging.info(f"Watch for wd {event.wd} was removed (directory deleted?).")
if event.wd in wd_to_path:
logging.info(f"Removing path '{wd_to_path[event.wd]}' from watch mapping.")
del wd_to_path[event.wd]
continue
if event.mask & flags.ISDIR and (event.mask & flags.CREATE or event.mask & flags.MOVED_TO):
parent_dir_path = wd_to_path.get(event.wd)
if not parent_dir_path:
logging.warning(f"Could not find path for watch descriptor {event.wd}")
continue
new_dir_path = os.path.join(parent_dir_path, event.name)
logging.info(f"New directory {new_dir_path} detected, adding watches.")
add_watch_recursive(inotify, new_dir_path, watch_flags, wd_to_path)
continue
scan_needed = True
if scan_needed:
if timer:
timer.cancel()
logging.info('Debounce cancelled timer.')