-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
180 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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 @@ | ||
|
||
public class BackgroundWorker : BackgroundService | ||
{ | ||
private readonly ILogger<BackgroundWorker> _logger; | ||
|
||
public BackgroundWorker(IBackgroundTaskQueue taskQueue, | ||
ILogger<BackgroundWorker> logger) | ||
{ | ||
TaskQueue = taskQueue; | ||
_logger = logger; | ||
} | ||
|
||
public IBackgroundTaskQueue TaskQueue { get; } | ||
|
||
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
{ | ||
_logger.LogInformation("Queued Hosted Service running."); | ||
await BackgroundProcessing(stoppingToken); | ||
} | ||
|
||
private async Task BackgroundProcessing(CancellationToken stoppingToken) | ||
{ | ||
while (!stoppingToken.IsCancellationRequested) | ||
{ | ||
|
||
|
||
var workItem = | ||
await TaskQueue.DequeueAsync(stoppingToken); | ||
_logger.LogInformation("Accepted task at: {time}", DateTimeOffset.Now); | ||
|
||
try | ||
{ | ||
await workItem(stoppingToken); | ||
_logger.LogInformation("Completed task at: {time}", DateTimeOffset.Now); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, | ||
"Error occurred executing {WorkItem}.", nameof(workItem)); | ||
} | ||
} | ||
} | ||
|
||
public override async Task StopAsync(CancellationToken stoppingToken) | ||
{ | ||
_logger.LogInformation("Queued Hosted Service is stopping."); | ||
|
||
await base.StopAsync(stoppingToken); | ||
} | ||
} | ||
|
||
|
This file contains 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,48 @@ | ||
using System.Threading.Channels; | ||
|
||
public interface IBackgroundTaskQueue | ||
{ | ||
ValueTask QueueBackgroundWorkItemAsync(Func<CancellationToken, ValueTask> workItem); | ||
|
||
ValueTask<Func<CancellationToken, ValueTask>> DequeueAsync( | ||
CancellationToken cancellationToken); | ||
} | ||
|
||
public class BackgroundTaskQueue : IBackgroundTaskQueue | ||
{ | ||
private readonly Channel<Func<CancellationToken, ValueTask>> _queue; | ||
|
||
public BackgroundTaskQueue(int capacity) | ||
{ | ||
// Capacity should be set based on the expected application load and | ||
// number of concurrent threads accessing the queue. | ||
// BoundedChannelFullMode.Wait will cause calls to WriteAsync() to return a task, | ||
// which completes only when space became available. This leads to backpressure, | ||
// in case too many publishers/calls start accumulating. | ||
var options = new BoundedChannelOptions(capacity) | ||
{ | ||
FullMode = BoundedChannelFullMode.Wait | ||
}; | ||
_queue = Channel.CreateBounded<Func<CancellationToken, ValueTask>>(options); | ||
} | ||
|
||
public async ValueTask QueueBackgroundWorkItemAsync( | ||
Func<CancellationToken, ValueTask> workItem) | ||
{ | ||
if (workItem == null) | ||
{ | ||
throw new ArgumentNullException(nameof(workItem)); | ||
} | ||
|
||
await _queue.Writer.WriteAsync(workItem); | ||
} | ||
|
||
public async ValueTask<Func<CancellationToken, ValueTask>> DequeueAsync( | ||
CancellationToken cancellationToken) | ||
{ | ||
var workItem = await _queue.Reader.ReadAsync(cancellationToken); | ||
|
||
return workItem; | ||
} | ||
} | ||
|
This file contains 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
This file contains 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