diff --git a/backend/app/tasks/scan.py b/backend/app/tasks/scan.py index 8c7a400..02b1838 100644 --- a/backend/app/tasks/scan.py +++ b/backend/app/tasks/scan.py @@ -65,16 +65,34 @@ def scan_directory(self, triggered_by: str = "manual", session.flush() batch_id = batch.id + # Skip-set: any source_path that already has a non-failed ImportTask + # row. Re-running scan_directory must not re-enqueue files the + # importer has already handled (or is currently handling); doing so + # creates duplicate work and inflates the queue. Failed prior tasks + # are eligible for retry. + non_failed_existing = set(session.execute( + select(ImportTask.source_path).where( + ImportTask.status.in_( + ["pending", "queued", "processing", "complete", "skipped"] + ), + ) + ).scalars().all()) + # Walk and enumerate. files_seen = 0 + files_skipped_existing = 0 for entry in _iter_import_files(import_root): + entry_str = str(entry) + if entry_str in non_failed_existing: + files_skipped_existing += 1 + continue try: size = entry.stat().st_size except OSError: size = None task = ImportTask( batch_id=batch_id, - source_path=str(entry), + source_path=entry_str, task_type="media", status="pending", size_bytes=size,