99from app .config .settings import *
1010from app .logging .setup_logging import get_logger
1111
12- # Configure third-party loggers
1312logging .getLogger ("httpx" ).setLevel (logging .WARNING )
1413logging .getLogger ("httpcore" ).setLevel (logging .WARNING )
1514logging .getLogger ("watchfiles" ).setLevel (logging .WARNING ) # Silence watchfiles logger
2120
2221FolderIdPath = Tuple [str , str ]
2322
24- # Global variables to track watcher state
2523watcher_thread : Optional [threading .Thread ] = None
2624stop_event = threading .Event ()
2725watched_folders : List [FolderIdPath ] = []
@@ -38,10 +36,8 @@ def watcher_util_get_folder_id_if_watched(file_path: str) -> Optional[str]:
3836 Returns:
3937 Folder ID if the path is a watched folder, None otherwise
4038 """
41- # Normalize the file path
4239 normalized_path = os .path .abspath (file_path )
4340
44- # Check if this path matches any of our watched folders
4541 for folder_id , folder_path in watched_folders :
4642 if os .path .abspath (folder_path ) == normalized_path :
4743 return folder_id
@@ -60,28 +56,23 @@ def watcher_util_handle_file_changes(changes: set) -> None:
6056
6157 affected_folders = {} # folder_path -> folder_id mapping
6258
63- # First pass - count changes and identify affected folders
6459 for change , file_path in changes :
65- # Process deletions
6660 if change == Change .deleted :
6761 deleted_folder_id = watcher_util_get_folder_id_if_watched (file_path )
6862 if deleted_folder_id :
6963 deleted_folder_ids .append (deleted_folder_id )
7064 continue
7165
72- # Find affected folder
7366 closest_folder = watcher_util_find_closest_parent_folder (
7467 file_path , watched_folders
7568 )
7669 if closest_folder :
7770 folder_id , folder_path = closest_folder
7871 affected_folders [folder_path ] = folder_id
7972
80- # Process affected folders
8173 for folder_path , folder_id in affected_folders .items ():
8274 watcher_util_call_sync_folder_api (folder_id , folder_path )
8375
84- # Handle deleted folders
8576 if deleted_folder_ids :
8677 logger .info (f"Processing { len (deleted_folder_ids )} deleted folders" )
8778 watcher_util_call_delete_folders_api (deleted_folder_ids )
@@ -101,21 +92,16 @@ def watcher_util_find_closest_parent_folder(
10192 Returns:
10293 Tuple of (folder_id, folder_path) if found, None otherwise
10394 """
104- # Normalize the file path
10595 file_path = os .path .abspath (file_path )
10696
10797 best_match = None
10898 longest_match_length = 0
10999
110100 for folder_id , folder_path in watched_folders :
111- # Normalize the folder path
112101 folder_path = os .path .abspath (folder_path )
113102
114- # Check if this folder is a parent of the file
115103 if file_path .startswith (folder_path ):
116- # Ensure it's a proper parent (not just a prefix)
117104 if file_path == folder_path or file_path [len (folder_path )] == os .sep :
118- # Choose the longest matching path (closest parent)
119105 if len (folder_path ) > longest_match_length :
120106 longest_match_length = len (folder_path )
121107 best_match = (folder_id , folder_path )
@@ -132,7 +118,6 @@ def watcher_util_call_sync_folder_api(folder_id: str, folder_path: str) -> None:
132118 folder_path: Path of the folder to sync
133119 """
134120 try :
135- # Import settings here to avoid circular dependencies
136121 from app .config .settings import BACKEND_URL
137122 url = f"{ BACKEND_URL } /folders/sync-folder"
138123 payload = {"folder_path" : folder_path , "folder_id" : folder_id }
@@ -163,7 +148,6 @@ def watcher_util_call_delete_folders_api(folder_ids: List[str]) -> None:
163148 folder_ids: List of folder IDs to delete
164149 """
165150 try :
166- # Import settings here to avoid circular dependencies
167151 from app .config .settings import BACKEND_URL
168152 url = f"{ BACKEND_URL } /folders/delete-folders"
169153 payload = {"folder_ids" : folder_ids }
@@ -202,13 +186,11 @@ def watcher_util_watcher_worker(folder_paths: List[str]) -> None:
202186 logger .info ("Stop event detected in watcher loop" )
203187 break
204188
205- # Log changes at debug level before processing
206189 if logger .isEnabledFor (10 ): # DEBUG level is 10
207190 from app .utils .watcher_helpers import format_debug_changes
208191
209192 logger .debug ("Detailed changes:\n %s" , format_debug_changes (changes ))
210193
211- # Process changes
212194 watcher_util_handle_file_changes (changes )
213195 except Exception as e :
214196 logger .error (f"Error in watcher worker: { e } " )
@@ -259,11 +241,8 @@ def watcher_util_start_folder_watcher() -> bool:
259241 logger .debug ("Debug logging is enabled" )
260242
261243 try :
262- # Get all folder details from database
263- # db_get_all_folder_details returns: List[Tuple[folder_id, folder_path, parent_folder_id, last_modified_time, AI_Tagging, taggingCompleted]]
264244 all_folder_details = db_get_all_folder_details ()
265245
266- # Extract just folder_id and folder_path (first two elements of tuple)
267246 folders = [(folder_id , folder_path ) for folder_id , folder_path , * _ in all_folder_details ]
268247
269248 if not folders :
@@ -272,7 +251,6 @@ def watcher_util_start_folder_watcher() -> bool:
272251
273252 logger .info (f"Found { len (folders )} folders in database" )
274253
275- # Simple synchronous file system checks
276254 existing_folders = watcher_util_get_existing_folders (folders )
277255 if not existing_folders :
278256 logger .info ("No existing folders to watch" )
@@ -289,7 +267,6 @@ def watcher_util_start_folder_watcher() -> bool:
289267 for folder_id , folder_path in existing_folders :
290268 logger .info (f" - { folder_path } (ID: { folder_id } )" )
291269
292- # Reset stop event and start background thread
293270 stop_event .clear ()
294271 watcher_thread = threading .Thread (
295272 target = watcher_util_watcher_worker ,
@@ -317,10 +294,8 @@ def watcher_util_stop_folder_watcher() -> None:
317294 try :
318295 logger .info ("Stopping folder watcher..." )
319296
320- # Signal the watcher to stop
321297 stop_event .set ()
322298
323- # Wait for thread to finish
324299 watcher_thread .join (timeout = 5.0 )
325300
326301 if watcher_thread .is_alive ():
@@ -332,7 +307,6 @@ def watcher_util_stop_folder_watcher() -> None:
332307 logger .error (f"Error stopping watcher: { e } " )
333308 finally :
334309 watcher_thread = None
335- # Clear state
336310 watched_folders = []
337311 folder_id_map = {}
338312
0 commit comments