Add startup cleanup for dangling documents#71
Merged
Conversation
2 tasks
Copilot
AI
changed the title
[WIP] Add cleanup for failed files on startup
Add startup cleanup for dangling documents
Jul 2, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a deterministic “startup cleanup” step to prevent documents from remaining stuck in in-progress states after crashes/redeploys by failing those DB rows and preserving/moving their source scans into the failed-documents area (implemented in shared scansynclib, invoked from detection_service).
Changes:
- Introduces
scansynclib.cleanup.cleanup_dangling_documents()to findscanneddatarows withstatus_code BETWEEN 0 AND 4, move source scans to the failed directory, remove leftover*_OCR.pdfworking files, and mark rows as failed. - Hooks the cleanup into
detection_service/main.pyat the start ofmain()via a lazy import to avoid module-import DB initialization side effects. - Adds pytest coverage for move-to-failed, OCR file removal, missing file handling, and the no-op path.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
scansynclib/scansynclib/cleanup.py |
New startup cleanup logic to move dangling source files to the failed directory, delete leftover OCR working files, and fail dangling DB rows. |
detection_service/main.py |
Calls the cleanup once at service startup (lazy import) before scanning/publishing new files. |
tests/test_cleanup.py |
New unit tests validating cleanup behaviors (move, OCR deletion, missing source file, empty pending set). |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
maxi07
marked this pull request as ready for review
July 2, 2026 17:24
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
After a crash or redeploy, documents left mid-pipeline stay stuck in a "processing" state forever and their scans can be silently lost. This adds a startup cleanup that fails such documents and preserves their source files.
Changes
scansynclib/scansynclib/cleanup.py(new):cleanup_dangling_documents()findsscanneddatarows withstatus_code BETWEEN 0 AND 4(all in-progress/pending states), then for each:failed-documentsdir so it stays recoverable via the web UI, and drops any leftover*_OCR.pdfworking file.FAILED(status_code = -1) so it surfaces in the failed section. Missing files are tolerated — the row is still failed so nothing dangles.detection_service/main.py: invokes cleanup once at the top ofmain(). Detection is chosen because it's a single-instance container; running this in the replicated OCR/metadata services would clobber documents a sibling replica is actively processing. Import is lazy to avoid triggeringsqlite_wrapper's DB init on module import (keepstest_detectionside-effect free).tests/test_cleanup.py(new): covers move-to-failed, OCR-file removal, missing-file handling, and the empty no-op path.Notes for reviewers