feat(watch): add entity without trigger plugins

This commit is contained in:
arkohut 2024-11-02 00:53:47 +08:00
parent 7dc152492b
commit 27e2971d2f
3 changed files with 28 additions and 14 deletions

View File

@ -833,6 +833,9 @@ def sync(
force: bool = typer.Option( force: bool = typer.Option(
False, "--force", "-f", help="Force update the file even if it hasn't changed" False, "--force", "-f", help="Force update the file even if it hasn't changed"
), ),
without_webhooks: bool = typer.Option(
False, "--no-plugins", help="Disable plugin triggers", is_flag=True
),
): ):
""" """
Sync a specific file with the library. Sync a specific file with the library.
@ -925,7 +928,7 @@ def sync(
update_response = httpx.put( update_response = httpx.put(
f"{BASE_URL}/entities/{existing_entity['id']}", f"{BASE_URL}/entities/{existing_entity['id']}",
json=new_entity, json=new_entity,
params={"trigger_webhooks_flag": "true"}, params={"trigger_webhooks_flag": str(not without_webhooks).lower()},
timeout=60, timeout=60,
) )
if update_response.status_code == 200: if update_response.status_code == 200:
@ -955,6 +958,7 @@ def sync(
create_response = httpx.post( create_response = httpx.post(
f"{BASE_URL}/libraries/{library_id}/entities", f"{BASE_URL}/libraries/{library_id}/entities",
json=new_entity, json=new_entity,
params={"trigger_webhooks_flag": str(not without_webhooks).lower()},
timeout=60, timeout=60,
) )
@ -1034,7 +1038,8 @@ class LibraryFileHandler(FileSystemEventHandler):
def process_pending_files(self): def process_pending_files(self):
current_time = time.time() current_time = time.time()
files_to_process = [] files_to_process_with_plugins = []
files_to_process_without_plugins = []
processed_in_current_loop = 0 processed_in_current_loop = 0
with self.lock: with self.lock:
for path, file_info in list(self.pending_files.items()): for path, file_info in list(self.pending_files.items()):
@ -1043,21 +1048,27 @@ class LibraryFileHandler(FileSystemEventHandler):
if os.path.exists(path) and os.path.getsize(path) > 0: if os.path.exists(path) and os.path.getsize(path) > 0:
self.file_count += 1 self.file_count += 1
if self.file_count % self.sparsity_window == 0: if self.file_count % self.sparsity_window == 0:
files_to_process.append(path) files_to_process_with_plugins.append(path)
print( print(
f"file_count % sparsity_window: {self.file_count} % {self.sparsity_window} == 0" f"file_count % sparsity_window: {self.file_count} % {self.sparsity_window} == 0"
) )
print(f"Picked file for processing: {path}") print(f"Picked file for processing with plugins: {path}")
else: else:
files_to_process_without_plugins.append(path)
self.file_skipped += 1 self.file_skipped += 1
del self.pending_files[path] del self.pending_files[path]
elif not os.path.exists(path): elif not os.path.exists(path):
del self.pending_files[path] del self.pending_files[path]
for path in files_to_process: # Process files with plugins - these count as submitted
self.executor.submit(self.process_file, path) for path in files_to_process_with_plugins:
self.executor.submit(self.process_file, path, False)
self.file_submitted += 1 self.file_submitted += 1
# Process files without plugins - these don't count as submitted
for path in files_to_process_without_plugins:
self.executor.submit(self.process_file, path, True)
if processed_in_current_loop > 0: if processed_in_current_loop > 0:
self.logger.info( self.logger.info(
f"File count: {self.file_count}, Files submitted: {self.file_submitted}, Files synced: {self.file_synced}, Files skipped: {self.file_skipped}" f"File count: {self.file_count}, Files submitted: {self.file_submitted}, Files synced: {self.file_synced}, Files skipped: {self.file_skipped}"
@ -1065,11 +1076,12 @@ class LibraryFileHandler(FileSystemEventHandler):
self.update_sparsity_window() self.update_sparsity_window()
def process_file(self, path): def process_file(self, path, no_plugins):
self.logger.debug(f"Processing file: {path}") self.logger.debug(f"Processing file: {path} (with plugins: {not no_plugins})")
start_time = time.time() start_time = time.time()
sync(self.library_id, path) sync(self.library_id, path, without_webhooks=no_plugins)
end_time = time.time() end_time = time.time()
if not no_plugins:
with self.lock: with self.lock:
self.sync_times.append(end_time - start_time) self.sync_times.append(end_time - start_time)
self.file_synced += 1 self.file_synced += 1

View File

@ -324,7 +324,7 @@ def run_screen_recorder(threshold, base_dir, previous_hashes):
except Exception as e: except Exception as e:
logging.error(f"An error occurred: {str(e)}. Skipping this iteration.") logging.error(f"An error occurred: {str(e)}. Skipping this iteration.")
time.sleep(5) time.sleep(4)
def main(): def main():

View File

@ -258,6 +258,7 @@ async def new_entity(
request: Request, request: Request,
db: Session = Depends(get_db), db: Session = Depends(get_db),
plugins: Annotated[List[int] | None, Query()] = None, plugins: Annotated[List[int] | None, Query()] = None,
trigger_webhooks_flag: bool = True,
): ):
library = crud.get_library_by_id(library_id, db) library = crud.get_library_by_id(library_id, db)
if library is None: if library is None:
@ -266,6 +267,7 @@ async def new_entity(
) )
entity = crud.create_entity(library_id, new_entity, db) entity = crud.create_entity(library_id, new_entity, db)
if trigger_webhooks_flag:
await trigger_webhooks(library, entity, request, plugins) await trigger_webhooks(library, entity, request, plugins)
return entity return entity