RocketChat / RocketChat/Rocket.Chat

perf(cron): Batch process temporary upload cleanup

Open
#39,742 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

type: feature
Dominant language
TypeScript
Stars
46.1k
Forks
13.9k
Avg merge
3d 3h
Merged PRs (30d)
130

Description

Proposed changes
This PR optimizes the temporaryUploadCleanup cron job by replacing sequential execution with fault-tolerant batch processing.

Key Improvements:

  1. Memory & Connection Safety: Added a BATCH_SIZE of 50 to prevent the system from attempting to delete thousands of files simultaneously if a large backlog of temporary files expires at once.
  2. Fault Tolerance (Promise.allSettled): Replaced the sequential for await loop with Promise.allSettled. Previously, if a single file deletion failed (e.g., file already missing from storage), the entire cron job would crash, leaving the remaining expired files in the database. Now, the process is resilient and will complete the remaining deletions even if individual promises reject.
  3. Optimized Store Reference: Lifted FileUpload.getStore('Uploads') out of the loop to prevent unnecessary redundant lookups.

Issue(s)

Proactive performance and resilience optimization for backend background jobs.

Steps to test or reproduce

  1. Upload several temporary files.
  2. Trigger the temporaryUploadCleanup cron job.
  3. Verify files are successfully batch-deleted without throwing unhandled promise rejections if a file is manually removed from the storage bucket beforehand.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the temporaryUploadCleanup cron job and inspect how FileUpload.getStore('Uploads') and expired temporary files are currently processed. Exercise the job with several temporary files, including one removed from storage beforehand; done means cleanup continues after an individual deletion failure, processes files in batches of 50, and avoids unhandled promise rejections.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
backend, performance
Issue type
Refactor
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.