Compare commits
9 Commits
cbab9b3949
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 79bf32b35c | |||
| 2eb104f4c3 | |||
| 941fb535f6 | |||
| d811483e94 | |||
| a92c0464d2 | |||
| a0f3b2668b | |||
| 04bb79111b | |||
| 079deae811 | |||
| 8dd622f9cf |
@@ -29,27 +29,71 @@ def trigger_scan():
|
|||||||
logging.error(f"Failed to trigger scan: {e}")
|
logging.error(f"Failed to trigger scan: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
def add_watch_recursive(inotify, path, watch_flags, wd_to_path):
|
||||||
|
"""Walk a directory path and add watches recursively."""
|
||||||
|
logging.info(f"Watching {path} and its subdirectories recursively.")
|
||||||
|
for root, _, _ in os.walk(path):
|
||||||
|
try:
|
||||||
|
logging.info(f"Watching {root}")
|
||||||
|
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}")
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
global timer
|
global timer
|
||||||
watch_flags = flags.MODIFY | flags.CREATE | flags.DELETE | flags.MOVED_FROM | flags.MOVED_TO
|
watch_flags = flags.MODIFY | flags.CREATE | flags.DELETE | flags.MOVED_FROM | flags.MOVED_TO
|
||||||
|
wd_to_path = {}
|
||||||
|
missing_paths = set()
|
||||||
|
|
||||||
with INotify() as inotify:
|
with INotify() as inotify:
|
||||||
for path in WATCH_PATHS:
|
|
||||||
logging.info(f"Watching {path}")
|
|
||||||
inotify.add_watch(path, watch_flags)
|
|
||||||
|
|
||||||
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):
|
||||||
|
if path in missing_paths:
|
||||||
|
logging.info(f"Watch path {path} is now available.")
|
||||||
|
missing_paths.remove(path)
|
||||||
|
add_watch_recursive(inotify, path, watch_flags, wd_to_path)
|
||||||
|
else:
|
||||||
|
if path not in missing_paths:
|
||||||
|
logging.warning(f"Watch path {path} does not exist. Re-checking every second...")
|
||||||
|
missing_paths.add(path)
|
||||||
|
|
||||||
events = inotify.read(timeout=1000)
|
events = inotify.read(timeout=1000)
|
||||||
if events:
|
if events:
|
||||||
|
scan_needed = False
|
||||||
for event in events:
|
for event in events:
|
||||||
logging.debug(f"Event: {event!r}")
|
logging.debug(f"Event: {event!r}")
|
||||||
if timer:
|
if event.mask & flags.IGNORED:
|
||||||
timer.cancel()
|
logging.info(f"Watch for wd {event.wd} was removed (directory deleted?).")
|
||||||
logging.info('Debounce cancelled timer.')
|
if event.wd in wd_to_path:
|
||||||
timer = threading.Timer(SCAN_DELAY_SECONDS, trigger_scan)
|
logging.info(f"Removing path '{wd_to_path[event.wd]}' from watch mapping.")
|
||||||
timer.start()
|
del wd_to_path[event.wd]
|
||||||
logging.info(f"Modification detected. Triggering scan in {SCAN_DELAY_SECONDS} seconds.")
|
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.')
|
||||||
|
timer = threading.Timer(SCAN_DELAY_SECONDS, trigger_scan)
|
||||||
|
timer.start()
|
||||||
|
logging.info(f"Modification detected. Triggering scan in {SCAN_DELAY_SECONDS} seconds.")
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
logging.info("Shutting down...")
|
logging.info("Shutting down...")
|
||||||
finally:
|
finally:
|
||||||
|
|||||||
Reference in New Issue
Block a user