| | | 1 | | using System.Diagnostics.CodeAnalysis; |
| | | 2 | | using System.Runtime.ExceptionServices; |
| | | 3 | | using TeleFlow.Framework.Updates; |
| | | 4 | | |
| | | 5 | | namespace TeleFlow.Framework.Application; |
| | | 6 | | |
| | | 7 | | public sealed class TeleFlowApplication : ITeleFlowApplication |
| | | 8 | | { |
| | | 9 | | private readonly IServiceProvider _services; |
| | | 10 | | private readonly IUpdateSource _updateSource; |
| | | 11 | | private readonly IUpdateProcessor _updateProcessor; |
| | | 12 | | private readonly TeleFlowApplicationLifecycleRunner _lifecycle; |
| | | 13 | | private readonly bool _ownsServices; |
| | | 14 | | private bool _disposed; |
| | | 15 | | |
| | | 16 | | internal TeleFlowApplication( |
| | | 17 | | IServiceProvider services, |
| | | 18 | | IUpdateSource updateSource, |
| | | 19 | | IUpdateProcessor updateProcessor, |
| | | 20 | | TeleFlowApplicationLifecycleRunner lifecycle, |
| | | 21 | | bool ownsServices) |
| | | 22 | | { |
| | 48 | 23 | | _services = services ?? throw new ArgumentNullException(nameof(services)); |
| | 48 | 24 | | _updateSource = updateSource ?? throw new ArgumentNullException(nameof(updateSource)); |
| | 48 | 25 | | _updateProcessor = updateProcessor ?? throw new ArgumentNullException(nameof(updateProcessor)); |
| | 48 | 26 | | _lifecycle = lifecycle ?? throw new ArgumentNullException(nameof(lifecycle)); |
| | 48 | 27 | | _ownsServices = ownsServices; |
| | 48 | 28 | | } |
| | | 29 | | |
| | | 30 | | public static ITeleFlowApplicationBuilder CreateBuilder(string[]? args = null) |
| | | 31 | | { |
| | 55 | 32 | | return new TeleFlowApplicationBuilder(args); |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | [SuppressMessage( |
| | | 36 | | "Design", |
| | | 37 | | "CA1031:Do not catch general exception types", |
| | | 38 | | Justification = "The application must run shutdown tasks for any update-source failure while preserving and reth |
| | | 39 | | public async Task RunAsync(CancellationToken cancellationToken = default) |
| | | 40 | | { |
| | 45 | 41 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | | 42 | | |
| | 44 | 43 | | await _lifecycle.RunStartupAsync(cancellationToken).ConfigureAwait(false); |
| | | 44 | | |
| | 42 | 45 | | ExceptionDispatchInfo? updateSourceException = null; |
| | | 46 | | |
| | | 47 | | try |
| | | 48 | | { |
| | 42 | 49 | | await _updateSource.StartAsync(ProcessUpdateAsync, cancellationToken).ConfigureAwait(false); |
| | 35 | 50 | | } |
| | 7 | 51 | | catch (Exception exception) |
| | | 52 | | { |
| | 7 | 53 | | updateSourceException = ExceptionDispatchInfo.Capture(exception); |
| | 7 | 54 | | } |
| | | 55 | | |
| | | 56 | | try |
| | | 57 | | { |
| | 42 | 58 | | await _lifecycle.RunShutdownAsync(cancellationToken).ConfigureAwait(false); |
| | 41 | 59 | | } |
| | 1 | 60 | | catch (Exception shutdownException) when (updateSourceException is not null) |
| | | 61 | | { |
| | 1 | 62 | | throw new AggregateException(updateSourceException.SourceException, shutdownException); |
| | | 63 | | } |
| | | 64 | | |
| | 41 | 65 | | if (updateSourceException is not null) |
| | | 66 | | { |
| | 6 | 67 | | updateSourceException.Throw(); |
| | | 68 | | } |
| | 35 | 69 | | } |
| | | 70 | | |
| | | 71 | | internal Task ProcessUpdateAsync(IUpdatePayload payload, CancellationToken cancellationToken) |
| | | 72 | | { |
| | 50 | 73 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | 50 | 74 | | ArgumentNullException.ThrowIfNull(payload); |
| | | 75 | | |
| | 50 | 76 | | return _updateProcessor.ProcessAsync(payload, cancellationToken); |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | public void Dispose() |
| | | 80 | | { |
| | 3 | 81 | | if (_disposed) |
| | | 82 | | { |
| | 1 | 83 | | return; |
| | | 84 | | } |
| | | 85 | | |
| | 2 | 86 | | _disposed = true; |
| | | 87 | | |
| | 2 | 88 | | if (!_ownsServices) |
| | | 89 | | { |
| | 0 | 90 | | return; |
| | | 91 | | } |
| | | 92 | | |
| | 2 | 93 | | if (_services is IDisposable disposable) |
| | | 94 | | { |
| | 2 | 95 | | disposable.Dispose(); |
| | | 96 | | } |
| | 2 | 97 | | } |
| | | 98 | | |
| | | 99 | | public async ValueTask DisposeAsync() |
| | | 100 | | { |
| | 12 | 101 | | if (_disposed) |
| | | 102 | | { |
| | 1 | 103 | | return; |
| | | 104 | | } |
| | | 105 | | |
| | 11 | 106 | | _disposed = true; |
| | | 107 | | |
| | 11 | 108 | | if (!_ownsServices) |
| | | 109 | | { |
| | 3 | 110 | | return; |
| | | 111 | | } |
| | | 112 | | |
| | 8 | 113 | | if (_services is IAsyncDisposable asyncDisposable) |
| | | 114 | | { |
| | 8 | 115 | | await asyncDisposable.DisposeAsync().ConfigureAwait(false); |
| | 8 | 116 | | return; |
| | | 117 | | } |
| | | 118 | | |
| | 0 | 119 | | if (_services is IDisposable disposable) |
| | | 120 | | { |
| | 0 | 121 | | disposable.Dispose(); |
| | | 122 | | } |
| | 12 | 123 | | } |
| | | 124 | | |
| | | 125 | | } |