fix(worker): bound BullMQ job retention to prevent Redis OOM
Fixes SOU-2311
Problem
A customer's Redis (Valkey) hit OOMKills and crash-looped. DEFAULT_JOB_OPTIONS.keepJobs retained completed and failed BullMQ jobs by age only (14 days) with no count cap, so memory scaled with repos × reindex runs × 14 days. With ~680 repos on the default hourly reindex that is ~228K completed repo-index jobs, each a hash plus a :logs list, matching the observed 935K keys / 1.52 GB.
Change
Job retention is now a retention policy on each queue spec with two modes:
window(default): BullMQ's count and age caps for the queue as a whole. Completed jobs keep 1 day / 5,000, failed jobs keep 14 days / 10,000. Used byrepo-cleanup,attachment-prune, andaudit-log-prune.latestPerResource: keeps one job per resource. When a job starts, the job manager records it undersourcebot:latest-job:<queue>:<resource>(keyed by the queue's existing deduplication id) and removes the job it supersedes. A 7-day age-only backstop reclaims jobs for resources that stop running, and the tracking key expires with the same TTL. Used byrepo-index,repo-permission-sync,connection-sync, andaccount-permission-sync, the queues whose latest job the web app resolves through alatest...JobIdpointer.
Window retention alone cannot guarantee a resource's latest job survives, since the window is shared by every resource in the queue. The failed set in particular drives the failed-repo filter, the sync-issue popover, and retry-all, so a code host outage that fails every repo at once would overflow any fixed cap. latestPerResource turns that into a guarantee bounded only by the age backstop.
Tracking happens inside the execution lock, right after onStarted, so the database pointer has already moved before the predecessor is removed. Retries of the same job are a no-op, a still-locked predecessor is left to the backstop, and tracking failures are logged without failing the job.
Rollout
Existing jobs drain by age only: anything older than 7 days is pruned at the first completions after upgrade (1,000 per completion), and the rest age out over the following week. Steady state after that is one job per resource. Schedulers pick up the new template on the next reconcile.
Testing
- New unit tests for
trackLatestJob(supersede, no predecessor, retry, locked predecessor, window no-op) and for the job manager ordering and failure handling. - Verified against a running dev worker: a second
repo-indexrun for the same repo removed the first job and its logs key, and the tracking key pointed at the second job with a 7-day TTL.
🤖 Generated with Claude Code
[!NOTE]
Medium Risk
Changes core worker queue retention and job lifecycle ordering for sync/index queues; mistakes could drop job history the UI resolves via latest…JobId, though processing is designed to continue if Redis tracking fails.
Overview
BullMQ job retention is reworked so Redis memory no longer grows with repo/connection count. Queue specs now use a retention policy instead of flat keepJobs: window (tighter global count/age caps on prune queues) or latestPerResource (one finished job per deduplication resource, with a 7-day age backstop).
For latestPerResource queues (repo-index, connection-sync, account-permission-sync, repo-permission-sync), the job manager calls trackLatestJob after onStarted (inside the execution lock) to record the current job in Redis and remove the superseded BullMQ job when possible; registration requires an onStarted hook that publishes the job id to the parent’s latest…JobId field. Repo indexing moves latestIndexingJobId updates into onStarted and drops the transactional write from job preparation. Tracking failures are logged and do not fail the running job.
Reviewed by Cursor Bugbot for commit 0e4c41c95ad78305885d94c6bf2908268e2cc5b1. Bugbot is set up for automated code reviews on this repo. Configure here.
Summary by cubic
Fixes SOU-2311, which caused Redis OOMKills and crash-loops, by replacing age-only 14-day job retention with per-queue retention policies that cap memory growth.
- Window mode caps completed jobs at 1 day / 5,000 and failed jobs at 14 days / 10,000 per queue for
repo-cleanup,attachment-prune, andaudit-log-prune. latestPerResourcemode keeps one job per resource, removing the job it supersedes when a new job starts, with a 7-day age backstop for resources that stop running.- Applies to
repo-index,repo-permission-sync,connection-sync, andaccount-permission-sync, whose latest job the web app resolves through alatest...JobIdpointer. - Tracking failures are logged without failing the job; existing jobs age out over the following week after upgrade.
- Adds a CHANGELOG entry describing the bounded retention.
Summary by CodeRabbit
- New Features
- Background queues now retain only the latest job for each repository, connection, or account, removing a superseded job when possible. A one-week age limit serves as a retention backstop.
- Other queues continue to use count- and time-based retention limits.
- Reliability
- If tracking or removing a superseded job fails, the current job can still proceed.