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