| | | 1 | | using System.Collections.Concurrent; |
| | | 2 | | using ConsoleLogStreaming.Core; |
| | | 3 | | using Elsa.Diagnostics.ConsoleLogs.Services; |
| | | 4 | | using Microsoft.AspNetCore.SignalR; |
| | | 5 | | using Microsoft.Extensions.Logging; |
| | | 6 | | namespace Elsa.Diagnostics.ConsoleLogs.RealTime; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Manages pushed SignalR console log subscriptions. |
| | | 10 | | /// </summary> |
| | | 11 | | public sealed class ElsaConsoleLogSubscriptionManager : IDisposable |
| | | 12 | | { |
| | 12 | 13 | | private readonly ConcurrentDictionary<string, ConsoleLogSubscription> _subscriptions = new(StringComparer.Ordinal); |
| | | 14 | | private readonly IConsoleLogProvider _provider; |
| | | 15 | | private readonly IConsoleLogSourceRegistry _sourceRegistry; |
| | | 16 | | private readonly IHubContext<ElsaConsoleLogsHub, IElsaConsoleLogsClient> _hubContext; |
| | | 17 | | private readonly ILogger<ElsaConsoleLogSubscriptionManager> _logger; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Initializes a new instance of the subscription manager. |
| | | 21 | | /// </summary> |
| | 12 | 22 | | public ElsaConsoleLogSubscriptionManager( |
| | 12 | 23 | | IConsoleLogProvider provider, |
| | 12 | 24 | | IConsoleLogSourceRegistry sourceRegistry, |
| | 12 | 25 | | IHubContext<ElsaConsoleLogsHub, IElsaConsoleLogsClient> hubContext, |
| | 12 | 26 | | ILogger<ElsaConsoleLogSubscriptionManager> logger) |
| | | 27 | | { |
| | 12 | 28 | | _provider = provider; |
| | 12 | 29 | | _sourceRegistry = sourceRegistry; |
| | 12 | 30 | | _hubContext = hubContext; |
| | 12 | 31 | | _logger = logger; |
| | 12 | 32 | | _sourceRegistry.SourceChanged += OnSourceChanged; |
| | 12 | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Starts a pushed subscription for a connection. |
| | | 37 | | /// </summary> |
| | | 38 | | public Task SubscribeAsync(string connectionId, ElsaConsoleLogFilter filter, CancellationToken cancellationToken) |
| | | 39 | | { |
| | 7 | 40 | | Unsubscribe(connectionId); |
| | 7 | 41 | | var subscriptionCancellation = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); |
| | 7 | 42 | | var subscription = new ConsoleLogSubscription(filter, subscriptionCancellation); |
| | 7 | 43 | | _subscriptions[connectionId] = subscription; |
| | | 44 | | |
| | 7 | 45 | | _ = StreamAsync(connectionId, filter, subscription, subscriptionCancellation.Token); |
| | 7 | 46 | | return Task.CompletedTask; |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Replaces the active pushed subscription filter. |
| | | 51 | | /// </summary> |
| | | 52 | | public Task UpdateFilterAsync(string connectionId, ElsaConsoleLogFilter filter, CancellationToken cancellationToken) |
| | | 53 | | { |
| | 0 | 54 | | return SubscribeAsync(connectionId, filter, cancellationToken); |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Removes a pushed subscription. |
| | | 59 | | /// </summary> |
| | | 60 | | public Task UnsubscribeAsync(string connectionId) |
| | | 61 | | { |
| | 1 | 62 | | Unsubscribe(connectionId); |
| | 1 | 63 | | return Task.CompletedTask; |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <inheritdoc /> |
| | | 67 | | public void Dispose() |
| | | 68 | | { |
| | 4 | 69 | | _sourceRegistry.SourceChanged -= OnSourceChanged; |
| | | 70 | | |
| | 12 | 71 | | foreach (var subscription in _subscriptions.Values) |
| | | 72 | | { |
| | 2 | 73 | | subscription.CancellationTokenSource.Cancel(); |
| | 2 | 74 | | subscription.CancellationTokenSource.Dispose(); |
| | | 75 | | } |
| | | 76 | | |
| | 4 | 77 | | _subscriptions.Clear(); |
| | 4 | 78 | | } |
| | | 79 | | |
| | | 80 | | private async Task StreamAsync(string connectionId, ElsaConsoleLogFilter filter, ConsoleLogSubscription subscription |
| | | 81 | | { |
| | | 82 | | try |
| | | 83 | | { |
| | 7 | 84 | | string? lastSourceSignature = null; |
| | | 85 | | |
| | 20 | 86 | | await foreach (var item in _provider.SubscribeAsync(ConsoleLogFilterMapper.ToStreamingFilter(filter), cancel |
| | | 87 | | { |
| | 3 | 88 | | if (item.Line != null) |
| | | 89 | | { |
| | 3 | 90 | | await _hubContext.Clients.Client(connectionId).ReceiveConsoleLogLineAsync(item.Line, cancellationTok |
| | | 91 | | |
| | 3 | 92 | | var sourceSignature = GetSourceSignature(item.Line.Source); |
| | 3 | 93 | | if (!string.Equals(sourceSignature, lastSourceSignature, StringComparison.Ordinal)) |
| | | 94 | | { |
| | 2 | 95 | | lastSourceSignature = sourceSignature; |
| | 2 | 96 | | await _hubContext.Clients.Client(connectionId).ReceiveSourceChangedAsync(item.Line.Source, cance |
| | | 97 | | } |
| | | 98 | | } |
| | | 99 | | |
| | 3 | 100 | | if (item.Dropped != null) |
| | 0 | 101 | | await _hubContext.Clients.Client(connectionId).ReceiveDroppedLinesAsync(item.Dropped, cancellationTo |
| | 3 | 102 | | } |
| | 6 | 103 | | } |
| | 1 | 104 | | catch (OperationCanceledException e) |
| | | 105 | | { |
| | 1 | 106 | | _logger.LogDebug(e, "Console log subscription for connection {ConnectionId} was canceled", connectionId); |
| | 1 | 107 | | } |
| | 0 | 108 | | catch (Exception e) when (e is not OperationCanceledException) |
| | | 109 | | { |
| | 0 | 110 | | _logger.LogWarning(e, "Console log subscription for connection {ConnectionId} stopped unexpectedly", connect |
| | 0 | 111 | | } |
| | | 112 | | finally |
| | | 113 | | { |
| | 7 | 114 | | Remove(connectionId, subscription); |
| | | 115 | | } |
| | 7 | 116 | | } |
| | | 117 | | |
| | | 118 | | private void Unsubscribe(string connectionId) |
| | | 119 | | { |
| | 8 | 120 | | if (!_subscriptions.TryRemove(connectionId, out var subscription)) |
| | 8 | 121 | | return; |
| | | 122 | | |
| | 0 | 123 | | subscription.CancellationTokenSource.Cancel(); |
| | 0 | 124 | | subscription.CancellationTokenSource.Dispose(); |
| | 0 | 125 | | } |
| | | 126 | | |
| | | 127 | | private void Remove(string connectionId, ConsoleLogSubscription subscription) |
| | | 128 | | { |
| | 7 | 129 | | var entry = new KeyValuePair<string, ConsoleLogSubscription>(connectionId, subscription); |
| | 7 | 130 | | if (((ICollection<KeyValuePair<string, ConsoleLogSubscription>>)_subscriptions).Remove(entry)) |
| | 5 | 131 | | subscription.CancellationTokenSource.Dispose(); |
| | 7 | 132 | | } |
| | | 133 | | |
| | | 134 | | private void OnSourceChanged(ConsoleLogSource source) |
| | | 135 | | { |
| | 1 | 136 | | _ = BroadcastSourceChangedAsync(source, _subscriptions.ToArray()); |
| | 1 | 137 | | } |
| | | 138 | | |
| | | 139 | | private async Task BroadcastSourceChangedAsync(ConsoleLogSource source, IReadOnlyCollection<KeyValuePair<string, Con |
| | | 140 | | { |
| | | 141 | | try |
| | | 142 | | { |
| | 4 | 143 | | foreach (var (connectionId, subscription) in subscriptions) |
| | | 144 | | { |
| | 1 | 145 | | if (!MatchesSource(source, subscription.Filter)) |
| | | 146 | | continue; |
| | | 147 | | |
| | 1 | 148 | | await _hubContext.Clients.Client(connectionId).ReceiveSourceChangedAsync(source, subscription.Cancellati |
| | | 149 | | } |
| | 1 | 150 | | } |
| | 0 | 151 | | catch (OperationCanceledException e) |
| | | 152 | | { |
| | 0 | 153 | | _logger.LogDebug(e, "Console log source change broadcast for source {SourceId} was canceled", source.Id); |
| | 0 | 154 | | } |
| | 0 | 155 | | catch (Exception e) when (e is not OperationCanceledException) |
| | | 156 | | { |
| | 0 | 157 | | _logger.LogDebug(e, "Failed to broadcast console log source change for source {SourceId}", source.Id); |
| | 0 | 158 | | } |
| | 1 | 159 | | } |
| | | 160 | | |
| | | 161 | | private static bool MatchesSource(ConsoleLogSource source, ElsaConsoleLogFilter filter) |
| | | 162 | | { |
| | 1 | 163 | | return string.IsNullOrWhiteSpace(filter.SourceId) || string.Equals(source.Id, filter.SourceId, StringComparison. |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | private static string GetSourceSignature(ConsoleLogSource source) |
| | | 167 | | { |
| | 3 | 168 | | var metadata = source.Metadata |
| | 0 | 169 | | .OrderBy(x => x.Key, StringComparer.Ordinal) |
| | 0 | 170 | | .ThenBy(x => x.Value, StringComparer.Ordinal) |
| | 3 | 171 | | .Select(x => $"{x.Key}={x.Value}"); |
| | | 172 | | |
| | 3 | 173 | | return string.Join('\u001f', |
| | 3 | 174 | | source.Id, |
| | 3 | 175 | | source.DisplayName, |
| | 3 | 176 | | source.ServiceName, |
| | 3 | 177 | | source.ProcessId?.ToString(), |
| | 3 | 178 | | source.MachineName, |
| | 3 | 179 | | source.Health.ToString(), |
| | 3 | 180 | | string.Join('\u001e', metadata)); |
| | | 181 | | } |
| | | 182 | | |
| | 18 | 183 | | private sealed record ConsoleLogSubscription(ElsaConsoleLogFilter Filter, CancellationTokenSource CancellationTokenS |
| | | 184 | | } |