-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackgroundQService.cs
More file actions
49 lines (40 loc) · 1.7 KB
/
Copy pathBackgroundQService.cs
File metadata and controls
49 lines (40 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace CodeServiceJD.BackgroundQ
{
public class BackgroundQService<T> : BackgroundService
{
private readonly ILogger<BackgroundQService<T>> _logger;
private readonly IBackgroundQ<T> _queue;
private readonly IBackgroundQProcessor<T> _processor;
public BackgroundQService(IBackgroundQ<T> queue, IBackgroundQProcessor<T> processor, ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<BackgroundQService<T>>();
_queue = queue;
_processor = processor;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_logger.LogDebug($"Background Q for {typeof(T).Name} ExecuteAsync is starting.");
stoppingToken.Register(() =>
_logger.LogDebug($"Background Q for {typeof(T).Name} background task cancellation of stoppingToken."));
while (!stoppingToken.IsCancellationRequested)
{
_logger.LogDebug($"Background Q for {typeof(T).Name} task doing background work.");
T element = await _queue.GetElementAsync(stoppingToken);
try
{
await _processor.ProcessQElementAsync(element);
}
catch (Exception e)
{
_logger.LogError(e, $"Background Q for {typeof(T).Name} internal exception");
}
}
_logger.LogDebug($"Background Q for {typeof(T).Name} background task ExecuteAsync finished.");
}
}
}