Skip to content

Add dedicated workers, parallel execution and successful job retention#25742

Open
maliming wants to merge 5 commits into
devfrom
maliming/background-jobs-worker-enhancements
Open

Add dedicated workers, parallel execution and successful job retention#25742
maliming wants to merge 5 commits into
devfrom
maliming/background-jobs-worker-enhancements

Conversation

@maliming

@maliming maliming commented Jul 3, 2026

Copy link
Copy Markdown
Member

Resolve #25664
Resolve #25656
Resolve #25606

Three opt-in enhancements to the default background job worker. All are off by default, so existing applications behave exactly as before.

Storing successful jobs (#25664)

By default a job is deleted as soon as it runs successfully. Set StoreSuccessfulJobs = true to keep completed jobs in the store instead — a new CompletionTime column marks them. Completed jobs are excluded from the waiting query so they are never run again, and a new BackgroundJobCleanupWorker prunes them once they are older than SuccessfulJobRetentionTime (defaults: 7 days retention, cleanup runs hourly via CleanSuccessfulJobsPeriod; set retention to null to keep them forever).

Dedicated workers per job type (#25656)

AddDedicatedWorker(...) registers a worker that only processes the given job argument types, each with its own distributed lock. A singleton BackgroundJobWorkerManager (following the same pattern as the outbox/inbox sender managers) resolves the workers from DI and starts each one with a BackgroundJobNameFilter; the default worker keeps handling everything not claimed by a dedicated worker. Duplicate job types or lock names are rejected at startup.

Parallel job execution (#25606)

Set MaxParallelJobExecutionCount greater than 1 to execute multiple jobs per poll cycle. In this mode the single worker-level lock is dropped and each job is instead claimed with its own distributed lock (PerJobDistributedLockPrefix + job id), so different instances in a cluster can process different jobs at the same time while still never running the same job twice.

IBackgroundJobStore and IBackgroundJobWorker gained the overloads these features need; the EF Core, MongoDB and in-memory stores implement them, and the EF Core index was reordered to serve both the waiting and cleanup queries. Documentation is updated under framework/infrastructure/background-jobs.


Integration tests app: https://github.com/maliming/abp-background-jobs-integration-tests

Copilot AI review requested due to automatic review settings July 3, 2026 06:33
@maliming maliming added this to the 10.7-preview milestone Jul 3, 2026

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 three opt-in enhancements to ABP’s background jobs module (default behavior remains unchanged): retaining successful jobs (via CompletionTime) with periodic cleanup, dedicated workers filtered by job type, and parallel per-job distributed locking for cross-instance execution.

Changes:

  • Introduces successful-job retention using CompletionTime plus a BackgroundJobCleanupWorker that prunes completed jobs based on retention options.
  • Adds dedicated-worker configuration (AddDedicatedWorker...) backed by a BackgroundJobWorkerManager and job-name filtering in stores/repositories.
  • Implements parallel job execution (MaxParallelJobExecutionCount) using per-job distributed locks, and adds extensive tests + documentation updates.

Reviewed changes

Copilot reviewed 47 out of 49 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
modules/background-jobs/test/Volo.Abp.BackgroundJobs.TestBase/Volo/Abp/BackgroundJobs/BackgroundJobsTestDataBuilder.cs Adds additional seeded job to test job-name filtering scenarios.
modules/background-jobs/test/Volo.Abp.BackgroundJobs.TestBase/Volo/Abp/BackgroundJobs/BackgroundJobsTestData.cs Adds a new test job id used by filtering tests.
modules/background-jobs/test/Volo.Abp.BackgroundJobs.TestBase/Volo/Abp/BackgroundJobs/BackgroundJobRepository_Tests.cs Adds repository/store coverage for filtering, completion exclusion, and retention deletion.
modules/background-jobs/src/Volo.Abp.BackgroundJobs.MongoDB/Volo/Abp/BackgroundJobs/MongoDB/MongoBackgroundJobRepository.cs Adds job-name filtering, completion exclusion, and retention delete API for MongoDB.
modules/background-jobs/src/Volo.Abp.BackgroundJobs.EntityFrameworkCore/Volo/Abp/BackgroundJobs/EntityFrameworkCore/EfCoreBackgroundJobRepository.cs Adds job-name filtering, completion exclusion, and retention delete API for EF Core.
modules/background-jobs/src/Volo.Abp.BackgroundJobs.EntityFrameworkCore/Volo/Abp/BackgroundJobs/EntityFrameworkCore/BackgroundJobsDbContextModelCreatingExtensions.cs Adds CompletionTime mapping and updates index to support new queries.
modules/background-jobs/src/Volo.Abp.BackgroundJobs.Domain/Volo/Abp/BackgroundJobs/IBackgroundJobRepository.cs Extends repository contract for filtering and cleanup deletion.
modules/background-jobs/src/Volo.Abp.BackgroundJobs.Domain/Volo/Abp/BackgroundJobs/BackgroundJobStore.cs Extends store API for filtering and cleanup deletion; adjusts FindAsync behavior.
modules/background-jobs/src/Volo.Abp.BackgroundJobs.Domain/Volo/Abp/BackgroundJobs/BackgroundJobsDomainMapperlyMappers.cs Formatting cleanup for mapper namespace/whitespace.
modules/background-jobs/src/Volo.Abp.BackgroundJobs.Domain/Volo/Abp/BackgroundJobs/BackgroundJobRecord.cs Adds CompletionTime to persist successful-job retention state.
modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Migrations/DemoAppDbContextModelSnapshot.cs Updates snapshot to include CompletionTime and new index.
modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Migrations/20260701082002_Added_CompletionTime_To_BackgroundJobs.Designer.cs Adds EF migration designer for CompletionTime and index changes.
modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Migrations/20260701082002_Added_CompletionTime_To_BackgroundJobs.cs Adds EF migration applying CompletionTime column + index changes.
modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Jobs/SendEmailJob.cs Adds demo “fast job” used to demonstrate default worker behavior.
modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Jobs/CalculateAzureFeesJob.cs Adds demo “slow job” used to demonstrate dedicated worker routing.
modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Jobs/CalculateAwsFeesJob.cs Adds demo “slow job” used to demonstrate dedicated worker routing.
modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/DemoAppModule.cs Demonstrates enabling the new options and enqueuing sample jobs.
framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/WorkerTestJobs.cs Adds test jobs/args and helpers for parallel + multi-worker validation.
framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/TestableBackgroundJobWorker.cs Exposes worker internals for unit testing of new behaviors.
framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/TestableBackgroundJobCleanupWorker.cs Exposes cleanup worker for unit testing.
framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/RecordingBackgroundJobWorker.cs Adds a recording worker used to assert manager startup behavior.
framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/BackgroundJobWorkerTestBase.cs Adds shared integrated-test base for worker tests.
framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/BackgroundJobWorker_Tests.cs Adds tests for retention, filtering, eligibility, and parallel execution.
framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/BackgroundJobWorker_MultiWorkerRegistration_Tests.cs Verifies manager starts dedicated workers + default worker with correct filters.
framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/BackgroundJobWorker_DuplicateConfiguration_Tests.cs Ensures invalid multi-worker configurations fail before starting any worker.
framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/BackgroundJobWorker_AutoLockName_Tests.cs Verifies derived lock-name behavior (stable, bounded length).
framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/BackgroundJobCleanupWorker_Tests.cs Tests retention cleanup logic and edge cases (e.g., MaxJobFetchCount=0).
framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/AbpSameJobNameTestModule.cs Test module for “distinct types, same resolved job name” validation.
framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/AbpMultiWorkerTestModule.cs Test module for multi-worker startup behavior.
framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/AbpDuplicateWorkerTestModule.cs Test module for duplicate job type across workers validation.
framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/AbpDuplicateLockNameTestModule.cs Test module for duplicate lock name across workers validation.
framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/AbpBackgroundJobWorkerTestModule.cs Configures environment for worker tests (manual execution).
framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/AbpBackgroundJobCleanupTestModule.cs Configures environment for cleanup worker tests.
framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/AbpAutoLockNameWorkerTestModule.cs Configures environment for derived-lock-name tests.
framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/InMemoryBackgroundJobStore.cs Adds filtering + completion exclusion + retention deletion to in-memory store.
framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/IBackgroundJobWorker.cs Redefines worker contract to support configurable lock/filter start.
framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/IBackgroundJobStore.cs Extends store contract for filtering and retention cleanup deletion.
framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/DedicatedWorkerDefinition.cs Adds validated dedicated-worker definition model for manager startup.
framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobWorkerManager.cs Introduces manager that validates configurations and starts workers.
framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobWorkerConfiguration.cs Adds dedicated worker configuration model (lock name + args types).
framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobWorker.cs Implements configurable workers, filtering, retention on success, and parallel execution.
framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobNameFilterMode.cs Adds filter mode enum (None/Include/Exclude).
framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobNameFilter.cs Adds job-name filter model used by workers and stores.
framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobInfo.cs Adds CompletionTime to represent successful completion retention.
framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobCleanupWorker.cs Adds worker that deletes completed jobs older than retention.
framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/AbpBackgroundJobWorkerOptions.cs Adds options for retention, dedicated workers, and parallel execution.
framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/AbpBackgroundJobsModule.cs Switches startup to manager + conditional cleanup worker registration.
docs/en/modules/background-jobs.md Links background-jobs module docs to the new framework documentation sections.
docs/en/framework/infrastructure/background-jobs/index.md Documents retention, dedicated workers, and parallel execution options and behavior.
Files not reviewed (1)
  • modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Migrations/20260701082002_Added_CompletionTime_To_BackgroundJobs.Designer.cs: Generated file

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

Copilot reviewed 47 out of 49 changed files in this pull request and generated 2 comments.

Files not reviewed (1)
  • modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Migrations/20260701082002_Added_CompletionTime_To_BackgroundJobs.Designer.cs: Generated file

@codecov

codecov Bot commented Jul 3, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 77.77778% with 132 lines in your changes missing coverage. Please review.
✅ Project coverage is 49.06%. Comparing base (7ecb148) to head (9892971).
⚠️ Report is 56 commits behind head on dev.

Files with missing lines Patch % Lines
...obs/Volo/Abp/BackgroundJobs/BackgroundJobWorker.cs 54.59% 82 Missing and 2 partials ⚠️
...bp/BackgroundJobs/AbpBackgroundJobWorkerOptions.cs 75.40% 14 Missing and 1 partial ⚠️
...o/Abp/BackgroundJobs/BackgroundJobWorkerManager.cs 86.30% 9 Missing and 1 partial ⚠️
...main/Volo/Abp/BackgroundJobs/BackgroundJobStore.cs 30.76% 8 Missing and 1 partial ⚠️
...groundJobs/MongoDB/MongoBackgroundJobRepository.cs 89.18% 2 Missing and 2 partials ⚠️
...o/Abp/BackgroundJobs/BackgroundJobCleanupWorker.cs 90.00% 2 Missing and 1 partial ⚠️
...BackgroundJobs/BackgroundJobWorkerConfiguration.cs 72.72% 2 Missing and 1 partial ⚠️
...tityFrameworkCore/EfCoreBackgroundJobRepository.cs 90.90% 2 Missing and 1 partial ⚠️
...o/Abp/BackgroundJobs/InMemoryBackgroundJobStore.cs 95.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##              dev   #25742      +/-   ##
==========================================
+ Coverage   48.86%   49.06%   +0.19%     
==========================================
  Files        3733     3738       +5     
  Lines      126277   126814     +537     
  Branches     9709     9750      +41     
==========================================
+ Hits        61707    62222     +515     
- Misses      62726    62743      +17     
- Partials     1844     1849       +5     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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

Copilot reviewed 47 out of 49 changed files in this pull request and generated 2 comments.

Files not reviewed (1)
  • modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Migrations/20260701082002_Added_CompletionTime_To_BackgroundJobs.Designer.cs: Generated file

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

Copilot reviewed 47 out of 49 changed files in this pull request and generated 1 comment.

Files not reviewed (1)
  • modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Migrations/20260701082002_Added_CompletionTime_To_BackgroundJobs.Designer.cs: Generated file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

2 participants