Skip to content

Add startup cleanup for dangling documents#71

Merged
maxi07 merged 5 commits into
mainfrom
copilot/add-cleanup-for-failed-files
Jul 3, 2026
Merged

Add startup cleanup for dangling documents#71
maxi07 merged 5 commits into
mainfrom
copilot/add-cleanup-for-failed-files

Conversation

Copilot AI commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

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() finds scanneddata rows with status_code BETWEEN 0 AND 4 (all in-progress/pending states), then for each:
    • Moves the original (non-OCR) file into the failed-documents dir so it stays recoverable via the web UI, and drops any leftover *_OCR.pdf working file.
    • Marks the row 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 of main(). 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 triggering sqlite_wrapper's DB init on module import (keeps test_detection side-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

  • No scheduled/periodic cleanup was added, per the issue guidance — this runs deterministically at startup only.
  • RabbitMQ queues are durable, so a persisted message for a now-failed document may still be redelivered; since its source file has moved, the downstream service fails it consistently rather than resurrecting it. Worth a look if we want stricter dedup here.

Copilot AI linked an issue Jul 2, 2026 that may be closed by this pull request
2 tasks
Copilot AI changed the title [WIP] Add cleanup for failed files on startup Add startup cleanup for dangling documents Jul 2, 2026
Copilot finished work on behalf of maxi07 July 2, 2026 15:11
Copilot AI requested a review from maxi07 July 2, 2026 15:11
@maxi07
maxi07 requested a review from Copilot July 2, 2026 17:13

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 find scanneddata rows with status_code BETWEEN 0 AND 4, move source scans to the failed directory, remove leftover *_OCR.pdf working files, and mark rows as failed.
  • Hooks the cleanup into detection_service/main.py at the start of main() 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).

Comment thread scansynclib/scansynclib/cleanup.py Outdated
Comment thread scansynclib/scansynclib/cleanup.py Outdated
maxi07 and others added 2 commits July 2, 2026 19:19
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
maxi07 marked this pull request as ready for review July 2, 2026 17:24
@maxi07
maxi07 merged commit bab8e49 into main Jul 3, 2026
3 checks passed
@maxi07
maxi07 deleted the copilot/add-cleanup-for-failed-files branch July 4, 2026 09:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add cleanup for failed files on startup

3 participants