| | | 1 | | using Elsa.Mediator.Contracts; |
| | | 2 | | using Elsa.Mediator.Options; |
| | | 3 | | using Microsoft.Extensions.Hosting; |
| | | 4 | | using Microsoft.Extensions.Logging; |
| | | 5 | | using Microsoft.Extensions.Options; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Mediator.HostedServices; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// A hosted service that runs jobs. |
| | | 11 | | /// </summary> |
| | | 12 | | public class JobRunnerHostedService : BackgroundService |
| | | 13 | | { |
| | | 14 | | private readonly int _workerCount; |
| | | 15 | | private readonly IJobsChannel _jobsChannel; |
| | | 16 | | private readonly ILogger<JobRunnerHostedService> _logger; |
| | | 17 | | |
| | | 18 | | /// <inheritdoc /> |
| | 1 | 19 | | public JobRunnerHostedService(IOptions<MediatorOptions> options, IJobsChannel jobsChannel, ILogger<JobRunnerHostedSe |
| | | 20 | | { |
| | 1 | 21 | | _workerCount = options.Value.JobWorkerCount; |
| | 1 | 22 | | _jobsChannel = jobsChannel; |
| | 1 | 23 | | _logger = logger; |
| | 1 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <inheritdoc /> |
| | | 27 | | protected override async Task ExecuteAsync(CancellationToken stoppingToken) |
| | | 28 | | { |
| | | 29 | | // Create an array of worker tasks to process jobs in parallel |
| | 1 | 30 | | var workers = new Task[_workerCount]; |
| | | 31 | | |
| | | 32 | | // Start multiple workers (tasks) to process jobs concurrently |
| | 10 | 33 | | for (var i = 0; i < _workerCount; i++) |
| | 4 | 34 | | workers[i] = ProcessJobsAsync(stoppingToken); |
| | | 35 | | |
| | | 36 | | // Wait for all worker tasks to complete (typically when the application is shutting down) |
| | 1 | 37 | | await Task.WhenAll(workers); |
| | 0 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Continuously processes jobs from the job channel until cancellation is requested. |
| | | 42 | | /// </summary> |
| | | 43 | | /// <param name="stoppingToken">Cancellation token from the hosted service</param> |
| | | 44 | | private async Task ProcessJobsAsync(CancellationToken stoppingToken) |
| | | 45 | | { |
| | | 46 | | // Process all jobs from the channel until it's completed or cancellation is requested |
| | 8 | 47 | | await foreach (var jobItem in _jobsChannel.Reader.ReadAllAsync(stoppingToken)) |
| | | 48 | | { |
| | | 49 | | try |
| | | 50 | | { |
| | | 51 | | // Link the cancellation tokens so that cancellation can happen from either source |
| | 0 | 52 | | using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(stoppingToken, jobItem.Can |
| | 0 | 53 | | await jobItem.Action(linkedTokenSource.Token); |
| | 0 | 54 | | _logger.LogInformation("Worker {CurrentTaskId} processed job {JobId}", Task.CurrentId, jobItem.JobId); |
| | 0 | 55 | | } |
| | 0 | 56 | | catch (OperationCanceledException) |
| | | 57 | | { |
| | 0 | 58 | | _logger.LogInformation("Job {JobId} was canceled", jobItem.JobId); |
| | 0 | 59 | | } |
| | 0 | 60 | | catch (Exception ex) |
| | | 61 | | { |
| | 0 | 62 | | _logger.LogError(ex, "Job {JobId} failed", jobItem.JobId); |
| | 0 | 63 | | } |
| | | 64 | | finally |
| | | 65 | | { |
| | | 66 | | // Notify that the job has completed (whether successfully or not) |
| | 0 | 67 | | jobItem.OnJobCompleted(jobItem.JobId); |
| | | 68 | | } |
| | 0 | 69 | | } |
| | 0 | 70 | | } |
| | | 71 | | } |