-
Notifications
You must be signed in to change notification settings - Fork 20
[FEATURE] Background jobs #745
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
a3c57f9
Add HTTP Client Wrapper module: retry, serialization, exception handling
ducdaiii ea773ec
Add HTTP Client Wrapper module: retry, serialization, exception handling
ducdaiii 26a411d
[CodeFactor] Apply fixes
code-factor 7219c84
Refactor: HTTP Client Wrapper module: retry, serialization, exception…
ducdaiii ebdc245
Refactor: HTTP Client Wrapper module: retry, serialization, exception…
ducdaiii 748644e
feat(background-jobs): add full background job module with queue, wor…
ducdaiii 5e3ddd4
[CodeFactor] Apply fixes
code-factor 4e52bd5
Merge branch 'main' into feature/background-jobs
guibranco 61dcd96
Merge branch 'main' into feature/background-jobs
guibranco cf71f8f
Merge branch 'main' into feature/background-jobs
guibranco fef565a
Merge branch 'main' into feature/background-jobs
guibranco b1fde96
Merge branch 'main' into feature/background-jobs
guibranco 72b4fb3
Merge branch 'main' into feature/background-jobs
guibranco 9cec349
Merge branch 'main' into feature/background-jobs
guibranco cf5ac10
Merge branch 'main' into feature/background-jobs
guibranco be52be4
Merge branch 'main' into feature/background-jobs
guibranco a824470
Merge branch 'main' into feature/background-jobs
guibranco 2c15147
style: improve code readability and consistency +semver: minor
guibranco 54e5770
refactor: extract methods for job fetching and processing
guibranco f4af0a2
refactor: improve code readability with formatting adjustments
guibranco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
13 changes: 13 additions & 0 deletions
13
Src/CrispyWaffle.BackgroundJobs/Abstractions/IBackgroundJob.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| using CrispyWaffle.BackgroundJobs.Core; | ||
|
|
||
| namespace CrispyWaffle.BackgroundJobs.Abstractions | ||
| { | ||
| /// <summary> | ||
| /// Low-level job representation (mostly used for in-memory workflows). | ||
| /// For persisted jobs, prefer IBackgroundJobHandler<TData> with registry-based activation. | ||
| /// </summary> | ||
| public interface IBackgroundJob | ||
| { | ||
| Task<JobResult> ExecuteAsync(CancellationToken cancellationToken); | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
Src/CrispyWaffle.BackgroundJobs/Abstractions/IBackgroundJobHandler.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| using CrispyWaffle.BackgroundJobs.Core; | ||
|
|
||
| namespace CrispyWaffle.BackgroundJobs.Abstractions | ||
| { | ||
| /// <summary> | ||
| /// Typed handler used by persisted jobs. Implement this to allow DI-resolved handlers. | ||
| /// TData is the payload type stored as JSON in the job store. | ||
| /// </summary> | ||
| public interface IBackgroundJobHandler<TData> | ||
| { | ||
| Task<JobResult> HandleAsync(TData data, CancellationToken cancellationToken); | ||
| } | ||
| } |
20 changes: 20 additions & 0 deletions
20
Src/CrispyWaffle.BackgroundJobs/Abstractions/IJobScheduler.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| namespace CrispyWaffle.BackgroundJobs.Abstractions | ||
| { | ||
| public interface IJobScheduler | ||
| { | ||
| Task ScheduleAsync( | ||
| string handlerName, | ||
| object payload, | ||
| TimeSpan delay, | ||
| int maxAttempts = 3, | ||
| JobPriority priority = JobPriority.Normal | ||
| ); | ||
|
|
||
| Task EnqueueAsync( | ||
| string handlerName, | ||
| object payload, | ||
| int maxAttempts = 3, | ||
| JobPriority priority = JobPriority.Normal | ||
| ); | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| namespace CrispyWaffle.BackgroundJobs.Abstractions | ||
| { | ||
| public interface IJobStore | ||
| { | ||
| Task SaveAsync(JobEntity job, CancellationToken cancellationToken = default); | ||
|
|
||
| /// <summary> | ||
| /// Fetch the next available job that is due (ScheduledAt <= UtcNow) and Pending. | ||
| /// Implementation should atomically mark it as Processing to avoid double pick-up. | ||
| /// </summary> | ||
| Task<JobEntity?> FetchNextAsync(CancellationToken cancellationToken = default); | ||
|
|
||
| Task MarkCompletedAsync(Guid jobId, CancellationToken cancellationToken = default); | ||
|
|
||
| Task MarkFailedAsync( | ||
| Guid jobId, | ||
| string error, | ||
| CancellationToken cancellationToken = default | ||
| ); | ||
|
|
||
| Task MarkRetryAsync( | ||
| Guid jobId, | ||
| DateTimeOffset? nextAttemptAt, | ||
| int attemptCount, | ||
| CancellationToken cancellationToken = default | ||
| ); | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| namespace CrispyWaffle.BackgroundJobs.Abstractions | ||
| { | ||
| using System; | ||
|
|
||
| /// <summary> | ||
| /// Persistent representation of a job. | ||
| /// HandlerName identifies a registered handler; Payload is JSON. | ||
| /// </summary> | ||
| public class JobEntity | ||
| { | ||
| public Guid Id { get; set; } = Guid.NewGuid(); | ||
|
|
||
| /// <summary> | ||
| /// Logical handler name registered in IJobHandlerRegistry. | ||
| /// </summary> | ||
| public string HandlerName { get; set; } = string.Empty; | ||
|
|
||
| /// <summary> | ||
| /// JSON payload (serialized) for the handler. | ||
| /// </summary> | ||
| public string Payload { get; set; } = string.Empty; | ||
|
|
||
| public JobPriority Priority { get; set; } = JobPriority.Normal; | ||
|
|
||
| public JobStatus Status { get; set; } = JobStatus.Pending; | ||
|
|
||
| /// <summary> | ||
| /// When the job becomes due for execution. Null means immediately. | ||
| /// </summary> | ||
| public DateTimeOffset? ScheduledAt { get; set; } | ||
|
|
||
| public int Attempt { get; set; } = 0; | ||
|
|
||
| public int MaxAttempt { get; set; } = 3; | ||
|
|
||
| public string? LastError { get; set; } | ||
|
|
||
| public DateTimeOffset CreatedAt { get; set; } = DateTimeOffset.UtcNow; | ||
|
|
||
| public DateTimeOffset UpdatedAt { get; set; } = DateTimeOffset.UtcNow; | ||
|
|
||
| /// <summary> | ||
| /// Optional delay in seconds between retries (used only as hint). | ||
| /// </summary> | ||
| public int RetryDelaySeconds { get; set; } = 0; | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| namespace CrispyWaffle.BackgroundJobs.Abstractions | ||
| { | ||
| public enum JobPriority | ||
| { | ||
| High = 0, | ||
| Normal = 1, | ||
| Low = 2, | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| namespace CrispyWaffle.BackgroundJobs.Abstractions | ||
| { | ||
| public enum JobStatus | ||
| { | ||
| Pending = 0, | ||
| Processing = 1, | ||
| Completed = 2, | ||
| Failed = 3, | ||
| Dead = 4, | ||
| } | ||
| } |
52 changes: 52 additions & 0 deletions
52
Src/CrispyWaffle.BackgroundJobs/Core/BackgroundJobQueue.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| using System.Threading.Channels; | ||
| using CrispyWaffle.BackgroundJobs.Abstractions; | ||
|
|
||
| namespace CrispyWaffle.BackgroundJobs.Core | ||
| { | ||
| /// <summary> | ||
| /// Lightweight in-memory priority queue using Channel for signaling. | ||
| /// Priorities: lower int = higher priority. | ||
| /// This queue stores JobEntity (for uniformity with persistent store model). | ||
| /// </summary> | ||
| public class BackgroundJobQueue | ||
| { | ||
| private readonly object _lock = new(); | ||
| private readonly PriorityQueue<JobEntity, int> _pq = new(); | ||
| private readonly Channel<JobEntity> _signal = Channel.CreateUnbounded<JobEntity>( | ||
| new UnboundedChannelOptions { SingleReader = false, SingleWriter = false } | ||
| ); | ||
|
|
||
| public void Enqueue(JobEntity job) | ||
| { | ||
| lock (_lock) | ||
| { | ||
| _pq.Enqueue(job, (int)job.Priority); | ||
| } | ||
| // Always write to signal channel to notify consumers. | ||
| _ = _signal.Writer.WriteAsync(job); | ||
| } | ||
|
|
||
| public async Task<JobEntity?> DequeueAsync(CancellationToken cancellationToken) | ||
| { | ||
| // Wait until a job is signaled and then pop from our priority queue. | ||
| try | ||
| { | ||
| await _signal.Reader.ReadAsync(cancellationToken); | ||
| } | ||
| catch (OperationCanceledException) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| lock (_lock) | ||
| { | ||
| if (_pq.TryDequeue(out var job, out _)) | ||
| { | ||
| return job; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.