| | | 1 | | using Elsa.Mediator.Contracts; |
| | | 2 | | using Elsa.Mediator.Options; |
| | | 3 | | using Microsoft.Extensions.Logging; |
| | | 4 | | using Microsoft.Extensions.Options; |
| | | 5 | | |
| | | 6 | | namespace Elsa.Mediator.Services; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Continuously reads from a channel to which jobs can be sent, executing each received job. |
| | | 10 | | /// </summary> |
| | | 11 | | public class BackgroundJobProcessor |
| | | 12 | | { |
| | | 13 | | private readonly int _workerCount; |
| | | 14 | | private readonly IJobsChannel _jobsChannel; |
| | | 15 | | private readonly ILogger<BackgroundJobProcessor> _logger; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Initializes a new instance of the <see cref="BackgroundJobProcessor"/> class. |
| | | 19 | | /// </summary> |
| | 3 | 20 | | public BackgroundJobProcessor(IOptions<MediatorOptions> options, IJobsChannel jobsChannel, ILogger<BackgroundJobProc |
| | | 21 | | { |
| | 3 | 22 | | _workerCount = options.Value.JobWorkerCount; |
| | 3 | 23 | | _jobsChannel = jobsChannel; |
| | 3 | 24 | | _logger = logger; |
| | 3 | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Runs the processor until cancellation is requested. |
| | | 29 | | /// </summary> |
| | | 30 | | public async Task ExecuteAsync(CancellationToken cancellationToken) |
| | | 31 | | { |
| | 3 | 32 | | var workers = new Task[_workerCount]; |
| | | 33 | | |
| | 30 | 34 | | for (var i = 0; i < _workerCount; i++) |
| | 12 | 35 | | workers[i] = ProcessJobsAsync(cancellationToken); |
| | | 36 | | |
| | 3 | 37 | | await Task.WhenAll(workers); |
| | 3 | 38 | | } |
| | | 39 | | |
| | | 40 | | private async Task ProcessJobsAsync(CancellationToken cancellationToken) |
| | | 41 | | { |
| | | 42 | | try |
| | | 43 | | { |
| | 24 | 44 | | await foreach (var jobItem in _jobsChannel.Reader.ReadAllAsync(cancellationToken)) |
| | | 45 | | { |
| | | 46 | | try |
| | | 47 | | { |
| | 0 | 48 | | using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, job |
| | 0 | 49 | | await jobItem.Action(linkedTokenSource.Token); |
| | 0 | 50 | | _logger.LogInformation("Worker {CurrentTaskId} processed job {JobId}", Task.CurrentId, jobItem.JobId |
| | 0 | 51 | | } |
| | 0 | 52 | | catch (OperationCanceledException) |
| | | 53 | | { |
| | 0 | 54 | | _logger.LogInformation("Job {JobId} was canceled", jobItem.JobId); |
| | 0 | 55 | | } |
| | 0 | 56 | | catch (Exception ex) |
| | | 57 | | { |
| | 0 | 58 | | _logger.LogError(ex, "Job {JobId} failed", jobItem.JobId); |
| | 0 | 59 | | } |
| | | 60 | | finally |
| | | 61 | | { |
| | 0 | 62 | | jobItem.OnJobCompleted(jobItem.JobId); |
| | | 63 | | } |
| | 0 | 64 | | } |
| | 0 | 65 | | } |
| | 12 | 66 | | catch (OperationCanceledException ex) when (cancellationToken.IsCancellationRequested) |
| | | 67 | | { |
| | 12 | 68 | | _logger.LogDebug(ex, "An operation was cancelled while processing the job queue"); |
| | 12 | 69 | | } |
| | 12 | 70 | | } |
| | | 71 | | } |