feat: Async payrun job processing to prevent HTTP timeouts#6
Closed
gsayer wants to merge 3 commits intoPayroll-Engine:mainfrom
Closed
feat: Async payrun job processing to prevent HTTP timeouts#6gsayer wants to merge 3 commits intoPayroll-Engine:mainfrom
gsayer wants to merge 3 commits intoPayroll-Engine:mainfrom
Conversation
Implement asynchronous payrun job processing using Channel<T> and BackgroundService:
- Return HTTP 202 Accepted immediately with Location header for status polling
- Process employees in background worker service
- Prevent HTTP 524 timeout errors when processing large payrolls (500+ employees)
Breaking change: POST /api/tenants/{id}/payruns/jobs now returns HTTP 202 instead of 201
53e44f0 to
dbfa109
Compare
added 2 commits
January 12, 2026 14:17
PayrunProcessor now checks if a job was pre-created by the async controller (PayrunJobId > 0) and loads/updates it instead of always creating a new job. This fixes the bug where: - Controller creates job ID=3, enqueues with PayrunJobId=3 - Processor ignored PayrunJobId, created new job ID=4 - Result: job 3 stuck in "Process", job 4 orphaned with results Changes: - PayrunProcessor: detect pre-created jobs and load them - PayrunProcessor: use UpdateAsync instead of CreateAsync for existing jobs - PayrunJobFactory: add UpdatePayrunJob() method for async flow
CompleteJobAsync was missing the JobStatus update, causing jobs to remain stuck in "Process" status after successful completion. Now sets JobStatus = Draft to match the original sync behavior where completed jobs await user review before Release → Complete.
Contributor
|
Feature implement 3a845ac |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Channel<T>andBackgroundServiceProblem
When starting a payrun job via
POST /tenants/{tenantId}/payruns/jobswith a large number of employees, the HTTP request blocks until all payroll calculations are complete. For 1,000 employees at ~0.5-1s each, this results in 8-15+ minutes of blocking time, far exceeding typical gateway timeouts (e.g., Cloudflare's 100s limit results in HTTP 524 errors).Solution
Decouple the HTTP response from job completion:
Channel<T>Locationheader pointing to status endpointBackgroundServiceprocesses jobs from the queue asynchronouslyGET /api/tenants/{id}/payruns/jobs/{jobId}/statusfor completionChanges
New files
Domain/Domain.Application/Service/IPayrunJobQueue.cs- Queue interfaceDomain/Domain.Application/Service/PayrunJobQueueItem.cs- Queue item DTODomain/Domain.Application/PayrunJobQueue.cs- Channel-based queue implementationBackend.Server/PayrunJobWorkerService.cs- Background worker serviceApi/Api.Core/AcceptedObjectResult.cs- HTTP 202 result helperModified files
Api/Api.Controller/PayrunJobController.cs- Async job creation + queue integrationBackend.Controller/PayrunJobController.cs- Updated constructor and HTTP attributesBackend.Server/ProviderStartupExtensions.cs- DI registrationBackend.Server/Startup.cs- Queue and worker service registrationBreaking Changes
POST /api/tenants/{id}/payruns/jobsnow returns HTTP 202 Accepted instead of HTTP 201 CreatedLocationheader for status pollingTest plan
dotnet build PayrollEngine.Backend.slndotnet test Domain/Domain.Model.Tests/