elsa-core/src/modules/Elsa.Resilience/Features/ResilienceFeature.cs
Sipke Schoorstra c2fb027c41
Refactor: Overhauls workflow JSON type serialization (#7549)
* Avoid null endpoint DTO metadata in tests

* Enforce console logs hub read permission

* Remove unused console logs hub import

* Support mapped endpoint metadata in auth tests

* Reduce console log capture throughput impact

* Address Copilot console logs review

* Refactor task scheduling to support tenant-level background work and enhance logging functionality.

* Introduce ConsoleStreamHook for stdout/stderr tee and enhance logging validation. Adjust test cases and startup warnings for distributed lock provider usage.

* Refactor console logging pipeline with capture optimization and new ConsoleLogsHost; update tests accordingly.

* Add Ansi SGR parser for console logs and associated unit tests

* Remove ANSI color renderings and parsers; integrate ConsoleLogScopeAccessor for improved logging context with workflow instance ID support.

* Address console logs code quality feedback

* Address PR review feedback

* Preserve console logs extension points

* Stabilize console logs host lifecycle

* Address final automated review comments

* Tighten console log capture shutdown

* Address console log review feedback

* Address follow-up review feedback

* Cover final review feedback

* Avoid recursive console provider initialization

* Guard console host lease shutdown

* Preserve console log scope and provider lifetime

* Correlate console log scope fallback

* Tighten console scope correlation

* Expose host services during provider construction

* Redact ANSI-normalized console lines

* Remove `ConsoleCaptureTee` and related services and tests

* Use pipeline contributors for console log context

* Update CShells package versions to 0.0.24-preview.132

* Filter live console logs by workflow instance

* Enhance console logging with activity execution metadata and extend test coverage.

* Address console logs stream consumption comment

* Add diagnostics OpenTelemetry backend

* Introduce dedicated workflow JSON type registry and hardening

This change addresses GitHub issue #7541 by establishing a separate type registry (`IWorkflowJsonTypeRegistry`) for workflow JSON serialization. This decouples workflow type resolution from expression type aliases, enforcing a strict trust boundary.

Key aspects:
- New workflow JSON emits preferred aliases for registered types.
- Existing persisted workflows can be loaded via registered legacy names.
- Unknown, abstract, interface, open generic, or inappropriate collection types are rejected during deserialization, enhancing security.
- Public APIs (e.g., incident strategies) now expose consistent workflow JSON type identifiers.

This ensures secure, predictable, and backward-compatible handling of types within workflow definitions and payloads.

* Remove unused project references and streamline console log endpoint

* Move serialization type aliases to Elsa.Common

* Update serialization integration fixtures for aliases

* Stabilize missing rate limiter policy test
2026-05-31 11:09:39 +02:00

106 lines
4.3 KiB
C#

using Elsa.Expressions.Options;
using Elsa.Extensions;
using Elsa.Features.Abstractions;
using Elsa.Features.Services;
using Elsa.Resilience.Endpoints.SimulateResponse;
using Elsa.Resilience.Entities;
using Elsa.Resilience.Modifiers;
using Elsa.Resilience.Options;
using Elsa.Resilience.Recorders;
using Elsa.Resilience.Serialization;
using Elsa.Resilience.StrategySources;
using Elsa.Workflows;
using Elsa.Workflows.Options;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Elsa.Common.Serialization;
namespace Elsa.Resilience.Features;
public class ResilienceFeature(IModule module) : FeatureBase(module)
{
private Func<IServiceProvider, IRetryAttemptRecorder> _retryAttemptRecorder = sp => sp.GetRequiredService<ActivityExecutionContextRetryAttemptRecorder>();
private Func<IServiceProvider, IRetryAttemptReader> _retryAttemptReader = sp => sp.GetRequiredService<ActivityExecutionContextRetryAttemptReader>();
public ResilienceFeature AddResilienceStrategyType<T>() where T : IResilienceStrategy
{
return AddResilienceStrategyType(typeof(T));
}
public ResilienceFeature AddResilienceStrategyType(Type strategyType)
{
Services.Configure<ResilienceOptions>(options => options.StrategyTypes.Add(strategyType));
return this;
}
public ResilienceFeature WithActivityExecutionContextRetryAttemptRecorder()
{
return WithRetryAttemptRecorder<ActivityExecutionContextRetryAttemptRecorder>()
.WithRetryAttemptReader<ActivityExecutionContextRetryAttemptReader>();
}
public ResilienceFeature WithVoidRetryAttemptRecorder()
{
return WithRetryAttemptRecorder<VoidRetryAttemptRecorder>()
.WithRetryAttemptReader<VoidRetryAttemptReader>();
}
public ResilienceFeature WithRetryAttemptRecorder<T>() => WithRetryAttemptRecorder(sp => (IRetryAttemptRecorder)ActivatorUtilities.CreateInstance<T>(sp)!);
public ResilienceFeature WithRetryAttemptReader<T>() => WithRetryAttemptReader(sp => (IRetryAttemptReader)ActivatorUtilities.CreateInstance<T>(sp)!);
public ResilienceFeature WithRetryAttemptRecorder(Func<IServiceProvider, IRetryAttemptRecorder> recorder)
{
_retryAttemptRecorder = recorder;
return this;
}
public ResilienceFeature WithRetryAttemptReader(Func<IServiceProvider, IRetryAttemptReader> reader)
{
_retryAttemptReader = reader;
return this;
}
public override void Configure()
{
Module.AddFastEndpointsAssembly<ResilienceFeature>();
Services.Configure<ExpressionOptions>(options =>
{
options.AddTypeAlias<List<RetryAttemptRecord>>("RetryAttemptRecordList");
});
Services.Configure<SerializationTypeOptions>(options =>
{
options.RegisterTypeAlias(typeof(List<RetryAttemptRecord>), "RetryAttemptRecordList");
});
}
public override void Apply()
{
Services.AddOptions<ResilienceOptions>();
Services.AddOptions<SimulateResponseOptions>();
Services.TryAddSingleton(TimeProvider.System);
Services
.AddSingleton<ResilienceStrategySerializer>()
.AddSingleton<SimulateResponseSessionStore>()
.AddSingleton<IActivityDescriptorModifier, ResilientActivityDescriptorModifier>()
.AddScoped<IResilienceStrategyCatalog, ResilienceStrategyCatalog>()
.AddScoped<IResilienceStrategyConfigEvaluator, ResilienceStrategyConfigEvaluator>()
.AddScoped<IResilientActivityInvoker, ResilientActivityInvoker>()
.AddScoped<IResilienceStrategySource, ConfigurationResilienceStrategySource>()
.AddSingleton(VoidRetryAttemptRecorder.Instance)
.AddSingleton(VoidRetryAttemptReader.Instance)
.AddScoped<ActivityExecutionContextRetryAttemptRecorder>()
.AddScoped<ActivityExecutionContextRetryAttemptReader>()
.AddScoped(_retryAttemptRecorder)
.AddScoped(_retryAttemptReader)
.AddHandlersFrom<ResilienceFeature>();
// Register transient exception detection infrastructure
Services
.AddSingleton<ITransientExceptionStrategy, DefaultTransientExceptionStrategy>()
.AddSingleton<ITransientExceptionDetector, TransientExceptionDetector>();
}
}