Skip to content

Let a system job drop its finished runs and add a non-waiting lock - #2707

Draft
ravikiranvm wants to merge 1 commit into
mainfrom
ops-5063
Draft

ravikiranvm wants to merge 1 commit into
mainfrom
ops-5063

Conversation

@ravikiranvm

Copy link
Copy Markdown
Contributor

Part of OPS-5063.

Two small additions for background jobs that run often. Nothing uses them in this repo yet, and existing jobs and existing lock callers behave as before.

  • A system job can ask the queue to drop its finished runs. A job definition gets an optional discardFinishedRuns flag. When it is set, the queue removes each run once it completes or fails. Without it, as today, every finished run is kept.
  • A lock that is taken only if it is free. distributedLock.tryAcquireLock({ key, timeout }) returns the lock, or null when someone else holds it. It never waits. timeout is how long the lock lives if it is not released.

Additional Notes

Why:

  • The queue keeps every finished run, and each schedule change reads all of them to find the job's previous entry. That is fine for hourly and daily jobs. A job that runs every few minutes would add hundreds of records a day.
  • A job that runs every few minutes should skip a run while the previous one is still going. The existing acquireLock can only wait, for up to about twice the lock's lifetime, which would hold up the single worker all system jobs share.

Details:

  • With Redis, tryAcquireLock makes one attempt with no retries. It returns null only when Redis reports the key as locked; any other failure is reported and thrown, so a Redis problem is not mistaken for "another run is still going".
  • Without Redis (memory mode), it checks the in-process lock.

Considered and not chosen:

  • Dropping finished runs for every system job. It would change what is kept for the existing jobs; the flag leaves that choice to each job.
  • Calling acquireLock with a short timeout and treating the error as "held". With Redis the timeout is also the lock's lifetime, so a short timeout gives a lock that expires in the middle of a long run.

Testing Checklist

  • I tested the feature thoroughly, including edge cases
  • I verified all affected areas still work as expected
  • Automated tests were added/updated if necessary
  • Changes are backwards compatible with any existing data, otherwise a migration script is provided

What was run:

  • New unit tests for the job option, the Redis lock and the in-memory lock (15 tests with the existing system-job tests), lint on the changed files, and a type-check of server-api.
  • Both additions were exercised on a local stack by a job that uses them: its finished runs were not kept in Redis while other jobs' runs were, and the job took and released the lock on each run. That run used an earlier version of the Redis lock that treated every failure as "held"; the stricter check is covered by unit tests only.
  • Not run: the full test suites and nx build.

🤖 Generated with Claude Code

A job definition can set discardFinishedRuns so the queue removes each
run once it ends; the queue keeps every finished run by default and
scans them on each schedule change. distributedLock.tryAcquireLock takes
a lock only if it is free and returns null otherwise, for work that
should be skipped while another run of it is going.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@linear

linear Bot commented Dec 2, 2026

Copy link
Copy Markdown

OPS-5063

@ravikiranvm
ravikiranvm requested a balanced review from Copilot December 2, 2026 08:09
@sonarqubecloud

sonarqubecloud Bot commented Dec 2, 2026

Copy link
Copy Markdown

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.

🟡 Changes recommended

Active lock-acquisition issues can cause indefinite waits because reused memory mutexes ignore later timeouts and distributed try-acquisition leaves Redis transport retries unbounded.

2 open issues
What changed in this PR

Adds opt-in cleanup and non-waiting lock acquisition for frequent background jobs, while preserving default job retention.

Changes:

  • Adds discardFinishedRuns to remove completed and failed queue runs.
  • Adds try-acquisition for Redis and in-memory locks.
  • Adds focused tests for retention, contention, and error handling.

Blocking: Two required fixes remain.
Non-blocking: None.
Merge recommendation: Do not merge

File Description
packages/​server/​shared/​test/​memory-lock.test.ts Tests in-memory try-acquisition and shared ownership.
packages/​server/​shared/​test/​distributed-lock.test.ts Tests Redis acquisition, contention, and failures.
packages/​server/​shared/​src/​lib/​memory-lock.ts Adds in-memory try-acquisition.
packages/​server/​shared/​src/​lib/​distributed-lock.ts Adds try-acquisition and Redis error classification.
packages/​server/​api/​test/​unit/​helper/​system-jobs/​redis-system-job.test.ts Tests default and opt-in retention options.
packages/​server/​api/​src/​app/​helper/​system-jobs/​redis-system-job.ts Applies finished-run removal options.
packages/​server/​api/​src/​app/​helper/​system-jobs/​common.ts Adds the optional job-retention flag.

Review effort: Balanced

This branch has not been deployed

No deployments
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.

2 participants