| | | 1 | | using System.Threading.Channels; |
| | | 2 | | using Elsa.Mediator.Contracts; |
| | | 3 | | using Microsoft.Extensions.Hosting; |
| | | 4 | | using Microsoft.Extensions.Logging; |
| | | 5 | | |
| | | 6 | | namespace Elsa.Mediator.HostedServices; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Continuously reads from a channel to which commands can be sent, executing each received command. |
| | | 10 | | /// </summary> |
| | | 11 | | public class MessageProcessorHostedService<T> : BackgroundService where T : notnull |
| | | 12 | | { |
| | | 13 | | private readonly int _workerCount; |
| | | 14 | | private readonly Channel<T> _channel; |
| | | 15 | | private readonly IEnumerable<IConsumer<T>> _consumers; |
| | | 16 | | private readonly ILogger _logger; |
| | | 17 | | private readonly List<MessageWorker<T>> _workers; |
| | | 18 | | |
| | | 19 | | /// <inheritdoc /> |
| | | 20 | | // ReSharper disable once ContextualLoggerProblem |
| | | 21 | | public MessageProcessorHostedService(int workerCount, Channel<T> channel, IEnumerable<IConsumer<T>> consumers, ILogg |
| | | 22 | | { |
| | | 23 | | _workerCount = workerCount; |
| | | 24 | | _channel = channel; |
| | | 25 | | _consumers = consumers; |
| | | 26 | | _logger = logger; |
| | | 27 | | _workers = new List<MessageWorker<T>>(workerCount); |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <inheritdoc /> |
| | | 31 | | protected override async Task ExecuteAsync(CancellationToken cancellationToken) |
| | | 32 | | { |
| | | 33 | | var index = 0; |
| | | 34 | | |
| | | 35 | | for (var i = 0; i < _workerCount; i++) |
| | | 36 | | { |
| | | 37 | | var worker = new MessageWorker<T>(Channel.CreateUnbounded<T>(), _consumers, _logger); |
| | | 38 | | _workers.Add(worker); |
| | | 39 | | _ = worker.StartAsync(cancellationToken); |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | await foreach (var message in _channel.Reader.ReadAllAsync(cancellationToken)) |
| | | 43 | | { |
| | | 44 | | var worker = _workers[index]; |
| | | 45 | | await worker.DeliverMessageAsync(message, cancellationToken); |
| | | 46 | | index = (index + 1) % _workerCount; |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | foreach (var worker in _workers) |
| | | 50 | | worker.Complete(); |
| | | 51 | | |
| | | 52 | | _workers.Clear(); |
| | | 53 | | } |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Represents a worker that continuously reads from a channel and processes each received message. |
| | | 58 | | /// </summary> |
| | | 59 | | /// <typeparam name="T">The type of message to process.</typeparam> |
| | | 60 | | public class MessageWorker<T> where T : notnull |
| | | 61 | | { |
| | | 62 | | private readonly Channel<T> _channel; |
| | | 63 | | private readonly IEnumerable<IConsumer<T>> _consumers; |
| | | 64 | | private readonly ILogger _logger; |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Initializes a new instance of the <see cref="MessageWorker{T}"/> class. |
| | | 68 | | /// </summary> |
| | | 69 | | /// <param name="channel">The channel to read from.</param> |
| | | 70 | | /// <param name="consumers">The consumers that will process each received message.</param> |
| | | 71 | | /// <param name="logger">The logger.</param> |
| | 0 | 72 | | public MessageWorker(Channel<T> channel, IEnumerable<IConsumer<T>> consumers, ILogger logger) |
| | | 73 | | { |
| | 0 | 74 | | _channel = channel; |
| | 0 | 75 | | _consumers = consumers; |
| | 0 | 76 | | _logger = logger; |
| | 0 | 77 | | } |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Continuously reads from the channel and processes each received message. |
| | | 81 | | /// </summary> |
| | | 82 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 83 | | public async Task StartAsync(CancellationToken cancellationToken) |
| | | 84 | | { |
| | 0 | 85 | | await foreach (var message in _channel.Reader.ReadAllAsync(cancellationToken)) |
| | 0 | 86 | | foreach (var consumer in _consumers) |
| | 0 | 87 | | await InvokeConsumerAsync(consumer, message, cancellationToken); |
| | 0 | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// Delivers a message to the channel. |
| | | 92 | | /// </summary> |
| | | 93 | | /// <param name="message">The message to deliver.</param> |
| | | 94 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 95 | | public async Task DeliverMessageAsync(T message, CancellationToken cancellationToken) |
| | | 96 | | { |
| | 0 | 97 | | await _channel.Writer.WriteAsync(message, cancellationToken); |
| | 0 | 98 | | } |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// Completes the channel. |
| | | 102 | | /// </summary> |
| | | 103 | | public void Complete() |
| | | 104 | | { |
| | 0 | 105 | | _channel.Writer.Complete(); |
| | 0 | 106 | | } |
| | | 107 | | |
| | | 108 | | private async Task InvokeConsumerAsync(IConsumer<T> consumer, T message, CancellationToken cancellationToken) |
| | | 109 | | { |
| | | 110 | | try |
| | | 111 | | { |
| | 0 | 112 | | await consumer.ConsumeAsync(message, cancellationToken); |
| | 0 | 113 | | } |
| | 0 | 114 | | catch (Exception ex) |
| | | 115 | | { |
| | 0 | 116 | | _logger.LogError(ex, "An error occurred while invoking consumer {ConsumerType} for message {MessageType}", c |
| | 0 | 117 | | } |
| | 0 | 118 | | } |
| | | 119 | | } |