| | | 1 | | using System.Threading.Channels; |
| | | 2 | | using Elsa.Mediator.Contracts; |
| | | 3 | | using Elsa.Mediator.Middleware.Command; |
| | | 4 | | using Elsa.Mediator.Options; |
| | | 5 | | using Microsoft.Extensions.DependencyInjection; |
| | | 6 | | using Microsoft.Extensions.Logging; |
| | | 7 | | using Microsoft.Extensions.Options; |
| | | 8 | | |
| | | 9 | | namespace Elsa.Mediator.Services; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Continuously reads from a channel to which commands can be sent, executing each received command. |
| | | 13 | | /// </summary> |
| | | 14 | | public class BackgroundCommandProcessor |
| | | 15 | | { |
| | | 16 | | private readonly int _workerCount; |
| | | 17 | | private readonly ICommandsChannel _commandsChannel; |
| | | 18 | | private readonly IServiceScopeFactory _scopeFactory; |
| | | 19 | | private readonly ILogger _logger; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Initializes a new instance of the <see cref="BackgroundCommandProcessor"/> class. |
| | | 23 | | /// </summary> |
| | 4 | 24 | | public BackgroundCommandProcessor(IOptions<MediatorOptions> options, ICommandsChannel commandsChannel, IServiceScope |
| | | 25 | | { |
| | 4 | 26 | | _workerCount = options.Value.CommandWorkerCount; |
| | 4 | 27 | | _commandsChannel = commandsChannel; |
| | 4 | 28 | | _scopeFactory = scopeFactory; |
| | 4 | 29 | | _logger = logger; |
| | 4 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Runs the processor until cancellation is requested. |
| | | 34 | | /// </summary> |
| | | 35 | | public async Task ExecuteAsync(CancellationToken cancellationToken) |
| | | 36 | | { |
| | 4 | 37 | | var index = 0; |
| | 4 | 38 | | var outputs = new List<Channel<CommandContext>>(_workerCount); |
| | 4 | 39 | | var workers = new List<Task>(_workerCount); |
| | | 40 | | |
| | 34 | 41 | | for (var i = 0; i < _workerCount; i++) |
| | | 42 | | { |
| | 13 | 43 | | var output = Channel.CreateUnbounded<CommandContext>(); |
| | 13 | 44 | | outputs.Add(output); |
| | 13 | 45 | | workers.Add(ReadOutputAsync(output, cancellationToken)); |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | try |
| | | 49 | | { |
| | 60 | 50 | | await foreach (var commandContext in _commandsChannel.Reader.ReadAllAsync(cancellationToken)) |
| | | 51 | | { |
| | 26 | 52 | | var output = outputs[index]; |
| | 26 | 53 | | await output.Writer.WriteAsync(commandContext, cancellationToken); |
| | 26 | 54 | | index = (index + 1) % _workerCount; |
| | | 55 | | } |
| | 0 | 56 | | } |
| | 4 | 57 | | catch (OperationCanceledException ex) when (cancellationToken.IsCancellationRequested) |
| | | 58 | | { |
| | 4 | 59 | | _logger.LogDebug(ex, "An operation was cancelled while processing the command queue"); |
| | 4 | 60 | | } |
| | | 61 | | |
| | 34 | 62 | | foreach (var output in outputs) |
| | 13 | 63 | | output.Writer.Complete(); |
| | | 64 | | |
| | 4 | 65 | | await Task.WhenAll(workers); |
| | 4 | 66 | | } |
| | | 67 | | |
| | | 68 | | private async Task ReadOutputAsync(Channel<CommandContext> output, CancellationToken cancellationToken) |
| | | 69 | | { |
| | | 70 | | try |
| | | 71 | | { |
| | 78 | 72 | | await foreach (var commandContext in output.Reader.ReadAllAsync(cancellationToken)) |
| | | 73 | | { |
| | | 74 | | try |
| | | 75 | | { |
| | 26 | 76 | | using var scope = _scopeFactory.CreateScope(); |
| | 26 | 77 | | var commandSender = scope.ServiceProvider.GetRequiredService<ICommandSender>(); |
| | 26 | 78 | | using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, com |
| | | 79 | | |
| | 26 | 80 | | await commandSender.SendAsync( |
| | 26 | 81 | | commandContext.Command, |
| | 26 | 82 | | commandContext.CommandStrategy, |
| | 26 | 83 | | commandContext.Headers, |
| | 26 | 84 | | linkedTokenSource.Token); |
| | 26 | 85 | | } |
| | 0 | 86 | | catch (OperationCanceledException e) |
| | | 87 | | { |
| | 0 | 88 | | _logger.LogDebug(e, "An operation was cancelled while processing the command queue"); |
| | 0 | 89 | | } |
| | 0 | 90 | | catch (Exception e) |
| | | 91 | | { |
| | 0 | 92 | | _logger.LogError(e, "An unhandled exception occurred while processing the command queue"); |
| | 0 | 93 | | } |
| | | 94 | | } |
| | 0 | 95 | | } |
| | 13 | 96 | | catch (OperationCanceledException ex) when (cancellationToken.IsCancellationRequested) |
| | | 97 | | { |
| | 13 | 98 | | _logger.LogDebug(ex, "An operation was cancelled while processing the command queue"); |
| | 13 | 99 | | } |
| | 13 | 100 | | } |
| | | 101 | | } |