| | | 1 | | using System.Threading.Channels; |
| | | 2 | | using Elsa.Mediator.Contracts; |
| | | 3 | | using Elsa.Mediator.Middleware.Notification; |
| | | 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 notifications can be sent, publishing each received notification. |
| | | 13 | | /// </summary> |
| | | 14 | | public class BackgroundNotificationProcessor |
| | | 15 | | { |
| | | 16 | | private readonly int _workerCount; |
| | | 17 | | private readonly INotificationsChannel _notificationsChannel; |
| | | 18 | | private readonly IServiceScopeFactory _scopeFactory; |
| | | 19 | | private readonly ILogger _logger; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Initializes a new instance of the <see cref="BackgroundNotificationProcessor"/> class. |
| | | 23 | | /// </summary> |
| | 5 | 24 | | public BackgroundNotificationProcessor(IOptions<MediatorOptions> options, INotificationsChannel notificationsChannel |
| | | 25 | | { |
| | 5 | 26 | | _workerCount = options.Value.NotificationWorkerCount; |
| | 5 | 27 | | _notificationsChannel = notificationsChannel; |
| | 5 | 28 | | _scopeFactory = scopeFactory; |
| | 5 | 29 | | _logger = logger; |
| | 5 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Runs the processor until cancellation is requested. |
| | | 34 | | /// </summary> |
| | | 35 | | public async Task ExecuteAsync(CancellationToken cancellationToken) |
| | | 36 | | { |
| | 5 | 37 | | var index = 0; |
| | 5 | 38 | | var outputs = new List<Channel<NotificationContext>>(_workerCount); |
| | 5 | 39 | | var workers = new List<Task>(_workerCount); |
| | | 40 | | |
| | 40 | 41 | | for (var i = 0; i < _workerCount; i++) |
| | | 42 | | { |
| | 15 | 43 | | var output = Channel.CreateUnbounded<NotificationContext>(); |
| | 15 | 44 | | outputs.Add(output); |
| | 15 | 45 | | workers.Add(ReadOutputAsync(output, cancellationToken)); |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | try |
| | | 49 | | { |
| | 16 | 50 | | await foreach (var notification in _notificationsChannel.Reader.ReadAllAsync(cancellationToken)) |
| | | 51 | | { |
| | 3 | 52 | | var output = outputs[index]; |
| | 3 | 53 | | await output.Writer.WriteAsync(notification, cancellationToken); |
| | 3 | 54 | | index = (index + 1) % _workerCount; |
| | | 55 | | } |
| | 0 | 56 | | } |
| | 5 | 57 | | catch (OperationCanceledException ex) when (cancellationToken.IsCancellationRequested) |
| | | 58 | | { |
| | 5 | 59 | | _logger.LogDebug(ex, "An operation was cancelled while processing the notification queue"); |
| | 5 | 60 | | } |
| | | 61 | | |
| | 40 | 62 | | foreach (var output in outputs) |
| | 15 | 63 | | output.Writer.Complete(); |
| | | 64 | | |
| | 5 | 65 | | await Task.WhenAll(workers); |
| | 5 | 66 | | } |
| | | 67 | | |
| | | 68 | | private async Task ReadOutputAsync(Channel<NotificationContext> output, CancellationToken cancellationToken) |
| | | 69 | | { |
| | | 70 | | try |
| | | 71 | | { |
| | 15 | 72 | | using var scope = _scopeFactory.CreateScope(); |
| | 15 | 73 | | var notificationSender = scope.ServiceProvider.GetRequiredService<INotificationSender>(); |
| | | 74 | | |
| | 36 | 75 | | await foreach (var notificationContext in output.Reader.ReadAllAsync(cancellationToken)) |
| | | 76 | | { |
| | | 77 | | try |
| | | 78 | | { |
| | 3 | 79 | | var notification = notificationContext.Notification; |
| | 3 | 80 | | using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, not |
| | 3 | 81 | | await notificationSender.SendAsync(notification, notificationContext.NotificationStrategy, linkedTok |
| | 3 | 82 | | } |
| | 0 | 83 | | catch (OperationCanceledException e) |
| | | 84 | | { |
| | 0 | 85 | | _logger.LogDebug(e, "An operation was cancelled while processing the notification queue"); |
| | 0 | 86 | | } |
| | 0 | 87 | | catch (Exception e) |
| | | 88 | | { |
| | 0 | 89 | | _logger.LogError(e, "An unhandled exception occurred while processing the notification queue"); |
| | 0 | 90 | | } |
| | | 91 | | } |
| | 0 | 92 | | } |
| | 15 | 93 | | catch (OperationCanceledException ex) when (cancellationToken.IsCancellationRequested) |
| | | 94 | | { |
| | 15 | 95 | | _logger.LogDebug(ex, "An operation was cancelled while processing the notification queue"); |
| | 15 | 96 | | } |
| | 15 | 97 | | } |
| | | 98 | | } |