| | | 1 | | using Elsa.Expressions.Options; |
| | | 2 | | using Elsa.Extensions; |
| | | 3 | | using Elsa.Features.Abstractions; |
| | | 4 | | using Elsa.Features.Services; |
| | | 5 | | using Elsa.Resilience.Endpoints.SimulateResponse; |
| | | 6 | | using Elsa.Resilience.Entities; |
| | | 7 | | using Elsa.Resilience.Modifiers; |
| | | 8 | | using Elsa.Resilience.Options; |
| | | 9 | | using Elsa.Resilience.Recorders; |
| | | 10 | | using Elsa.Resilience.Serialization; |
| | | 11 | | using Elsa.Resilience.StrategySources; |
| | | 12 | | using Elsa.Workflows; |
| | | 13 | | using Elsa.Workflows.Options; |
| | | 14 | | using Microsoft.Extensions.DependencyInjection; |
| | | 15 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 16 | | using Elsa.Common.Serialization; |
| | | 17 | | |
| | | 18 | | namespace Elsa.Resilience.Features; |
| | | 19 | | |
| | 4 | 20 | | public class ResilienceFeature(IModule module) : FeatureBase(module) |
| | | 21 | | { |
| | 4 | 22 | | private Func<IServiceProvider, IRetryAttemptRecorder> _retryAttemptRecorder = sp => sp.GetRequiredService<ActivityEx |
| | 7 | 23 | | private Func<IServiceProvider, IRetryAttemptReader> _retryAttemptReader = sp => sp.GetRequiredService<ActivityExecut |
| | | 24 | | |
| | | 25 | | public ResilienceFeature AddResilienceStrategyType<T>() where T : IResilienceStrategy |
| | | 26 | | { |
| | 4 | 27 | | return AddResilienceStrategyType(typeof(T)); |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | public ResilienceFeature AddResilienceStrategyType(Type strategyType) |
| | | 31 | | { |
| | 5 | 32 | | Services.Configure<ResilienceOptions>(options => options.StrategyTypes.Add(strategyType)); |
| | 4 | 33 | | return this; |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | public ResilienceFeature WithActivityExecutionContextRetryAttemptRecorder() |
| | | 37 | | { |
| | 0 | 38 | | return WithRetryAttemptRecorder<ActivityExecutionContextRetryAttemptRecorder>() |
| | 0 | 39 | | .WithRetryAttemptReader<ActivityExecutionContextRetryAttemptReader>(); |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | public ResilienceFeature WithVoidRetryAttemptRecorder() |
| | | 43 | | { |
| | 0 | 44 | | return WithRetryAttemptRecorder<VoidRetryAttemptRecorder>() |
| | 0 | 45 | | .WithRetryAttemptReader<VoidRetryAttemptReader>(); |
| | | 46 | | } |
| | | 47 | | |
| | 0 | 48 | | public ResilienceFeature WithRetryAttemptRecorder<T>() => WithRetryAttemptRecorder(sp => (IRetryAttemptRecorder)Acti |
| | 0 | 49 | | public ResilienceFeature WithRetryAttemptReader<T>() => WithRetryAttemptReader(sp => (IRetryAttemptReader)ActivatorU |
| | | 50 | | |
| | | 51 | | public ResilienceFeature WithRetryAttemptRecorder(Func<IServiceProvider, IRetryAttemptRecorder> recorder) |
| | | 52 | | { |
| | 1 | 53 | | _retryAttemptRecorder = recorder; |
| | 1 | 54 | | return this; |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | public ResilienceFeature WithRetryAttemptReader(Func<IServiceProvider, IRetryAttemptReader> reader) |
| | | 58 | | { |
| | 0 | 59 | | _retryAttemptReader = reader; |
| | 0 | 60 | | return this; |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | public override void Configure() |
| | | 64 | | { |
| | 4 | 65 | | Module.AddFastEndpointsAssembly<ResilienceFeature>(); |
| | | 66 | | |
| | 4 | 67 | | Services.Configure<ExpressionOptions>(options => |
| | 4 | 68 | | { |
| | 4 | 69 | | options.AddTypeAlias<List<RetryAttemptRecord>>("RetryAttemptRecordList"); |
| | 8 | 70 | | }); |
| | | 71 | | |
| | 4 | 72 | | Services.Configure<SerializationTypeOptions>(options => |
| | 4 | 73 | | { |
| | 4 | 74 | | options.RegisterTypeAlias(typeof(List<RetryAttemptRecord>), "RetryAttemptRecordList"); |
| | 8 | 75 | | }); |
| | 4 | 76 | | } |
| | | 77 | | |
| | | 78 | | public override void Apply() |
| | | 79 | | { |
| | 4 | 80 | | Services.AddOptions<ResilienceOptions>(); |
| | 4 | 81 | | Services.AddOptions<SimulateResponseOptions>(); |
| | 4 | 82 | | Services.TryAddSingleton(TimeProvider.System); |
| | | 83 | | |
| | 4 | 84 | | Services |
| | 4 | 85 | | .AddSingleton<ResilienceStrategySerializer>() |
| | 4 | 86 | | .AddSingleton<SimulateResponseSessionStore>() |
| | 4 | 87 | | .AddSingleton<IActivityDescriptorModifier, ResilientActivityDescriptorModifier>() |
| | 4 | 88 | | .AddScoped<IResilienceStrategyCatalog, ResilienceStrategyCatalog>() |
| | 4 | 89 | | .AddScoped<IResilienceStrategyConfigEvaluator, ResilienceStrategyConfigEvaluator>() |
| | 4 | 90 | | .AddScoped<IResilientActivityInvoker, ResilientActivityInvoker>() |
| | 4 | 91 | | .AddScoped<IResilienceStrategySource, ConfigurationResilienceStrategySource>() |
| | 4 | 92 | | .AddSingleton(VoidRetryAttemptRecorder.Instance) |
| | 4 | 93 | | .AddSingleton(VoidRetryAttemptReader.Instance) |
| | 4 | 94 | | .AddScoped<ActivityExecutionContextRetryAttemptRecorder>() |
| | 4 | 95 | | .AddScoped<ActivityExecutionContextRetryAttemptReader>() |
| | 4 | 96 | | .AddScoped(_retryAttemptRecorder) |
| | 4 | 97 | | .AddScoped(_retryAttemptReader) |
| | 4 | 98 | | .AddHandlersFrom<ResilienceFeature>(); |
| | | 99 | | |
| | | 100 | | // Register transient exception detection infrastructure |
| | 4 | 101 | | Services |
| | 4 | 102 | | .AddSingleton<ITransientExceptionStrategy, DefaultTransientExceptionStrategy>() |
| | 4 | 103 | | .AddSingleton<ITransientExceptionDetector, TransientExceptionDetector>(); |
| | 4 | 104 | | } |
| | | 105 | | } |