Skip to content

perf(cleanup): Optimize cleanup job - #9166

Draft
Koc wants to merge 1 commit into
mainfrom
feature/optimize-cleanup-job
Draft

perf(cleanup): Optimize cleanup job#9166
Koc wants to merge 1 commit into
mainfrom
feature/optimize-cleanup-job

Conversation

@Koc

@Koc Koc commented Sep 3, 2026

Copy link
Copy Markdown

馃摑 Summary

馃毀 WIP

This is continuation of the #8421.

Problem:

The text app cleanup background job took roughly 800 seconds per run. The loop over documents with no active session unconditionally called cleanupAttachments, even for documents that had unsaved changes and could not be reset. The same ~8,000 documents kept re-entering the cleanup loop every cron run, and cleanupAttachments is expensive because it resolves the file, reads Markdown content, scans the database, and iterates the attachment directory.

Solution:

  • Attempt to resetDocument before running cleanupAttachments.
  • Catch DocumentHasUnsavedChangesException:
    • If unsaved changes are recent (last step within 30 days), skip the document. Attachment references may only exist in the unsaved state, so cleaning them now would be incorrect.
    • If unsaved changes are older than 30 days, force-reset the document, log a warning, and then clean up attachments.
  • Added a helper to fetch the latest step timestamp for a document.

Impact:

  • Cleanup cron runtime dropped from ~800 seconds to ~20 seconds.
  • Abandoned unsaved changes older than 30 days are now cleaned up instead of accumulating forever.

馃弫 Checklist

  • Code is properly formatted (npm run lint / npm run stylelint / composer run cs:check)
  • Sign-off message is added to all commits
  • Tests (unit, integration and/or end-to-end) passing and the changes are covered with tests
  • Documentation (README or documentation) has been updated or is not required

馃 AI (if applicable)

  • The content of this PR was partly or fully generated using AI tools
  • The AI-generated content was reviewed, comprehended and tested by a human

Signed-off-by: Kostiantyn Miakshyn <molodchick@gmail.com>
Comment thread lib/Cron/Cleanup.php
Comment on lines -37 to +42
$this->attachmentService->cleanupAttachments($document->getId());
$documentId = $document->getId();
try {
$this->documentService->resetDocument($documentId);

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

need to investigate why resetDocument was removed before

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.

1 participant