Class RetryWorkflowOperation
- Namespace
- WorkflowForge.Extensions.Resilience
- Assembly
- WorkflowForge.Extensions.Resilience.dll
Wraps an operation with retry logic for transient failures using resilience strategies. Essential for building resilient workflows that handle and pass through data.
public sealed class RetryWorkflowOperation : WorkflowOperationBase, IWorkflowOperation, IDisposable
- Inheritance
-
RetryWorkflowOperation
- Implements
- Inherited Members
- Extension Methods
Constructors
RetryWorkflowOperation(IWorkflowOperation, IWorkflowResilienceStrategy, string?, Guid?)
Initializes a new instance of the RetryWorkflowOperation class.
public RetryWorkflowOperation(IWorkflowOperation operation, IWorkflowResilienceStrategy retryStrategy, string? name = null, Guid? id = null)
Parameters
operationIWorkflowOperationThe operation to retry.
retryStrategyIWorkflowResilienceStrategyThe resilience strategy to use for retry logic.
namestringOptional name for the retry operation.
idGuid?Optional operation ID.
Properties
Id
Gets the unique identifier for this operation.
public override Guid Id { get; }
Property Value
Name
Gets the name of this operation for identification and logging purposes.
public override string Name { get; }
Property Value
Methods
Create(IWorkflowOperation, int, string?, IWorkflowForgeLogger?)
Creates a retry operation with default exponential backoff settings.
public static RetryWorkflowOperation Create(IWorkflowOperation operation, int maxAttempts = 3, string? name = null, IWorkflowForgeLogger? logger = null)
Parameters
operationIWorkflowOperationThe operation to retry.
maxAttemptsintMaximum number of attempts (default: 3).
namestringOptional name for the retry operation.
loggerIWorkflowForgeLoggerOptional logger for retry events.
Returns
- RetryWorkflowOperation
A new retry operation instance.
Dispose(bool)
Releases managed resources. Override in derived classes to dispose of owned resources.
protected override void Dispose(bool disposing)
Parameters
ForTransientErrors(IWorkflowOperation, int, string?, IWorkflowForgeLogger?)
Creates a retry operation optimized for transient errors.
public static RetryWorkflowOperation ForTransientErrors(IWorkflowOperation operation, int maxAttempts = 3, string? name = null, IWorkflowForgeLogger? logger = null)
Parameters
operationIWorkflowOperationThe operation to retry.
maxAttemptsintMaximum number of attempts (default: 3).
namestringOptional name for the retry operation.
loggerIWorkflowForgeLoggerOptional logger for retry events.
Returns
- RetryWorkflowOperation
A new retry operation instance.
ForgeAsyncCore(object?, IWorkflowFoundry, CancellationToken)
Core operation logic. Implement this method with your operation's business logic. This is called between OnBeforeExecuteAsync and OnAfterExecuteAsync.
protected override Task<object?> ForgeAsyncCore(object? inputData, IWorkflowFoundry foundry, CancellationToken cancellationToken = default)
Parameters
inputDataobjectThe input data for the operation.
foundryIWorkflowFoundryThe workflow foundry providing context and services.
cancellationTokenCancellationTokenThe cancellation token.
Returns
FromSettings(IWorkflowOperation, RetryPolicySettings, Func<Exception, bool>?, string?, IWorkflowForgeLogger?)
Creates a retry operation from retry policy settings.
public static RetryWorkflowOperation FromSettings(IWorkflowOperation operation, RetryPolicySettings settings, Func<Exception, bool>? retryPredicate = null, string? name = null, IWorkflowForgeLogger? logger = null)
Parameters
operationIWorkflowOperationThe operation to retry.
settingsRetryPolicySettingsThe retry policy settings.
retryPredicateFunc<Exception, bool>Optional predicate to determine which exceptions to retry.
namestringOptional name for the retry operation.
loggerIWorkflowForgeLoggerOptional logger for retry events.
Returns
- RetryWorkflowOperation
A new retry operation instance.
RestoreAsync(object?, IWorkflowFoundry, CancellationToken)
Restores (compensates) the operation's effects to undo its changes. Used for rollback scenarios when subsequent operations fail.
public override Task RestoreAsync(object? outputData, IWorkflowFoundry foundry, CancellationToken cancellationToken = default)
Parameters
outputDataobjectThe data that was produced by this operation during ForgeAsync.
foundryIWorkflowFoundryThe workflow foundry providing execution context and shared services.
cancellationTokenCancellationTokenThe cancellation token for cooperative cancellation.
Returns
- Task
A task representing the asynchronous restoration.
Remarks
The base implementation is a no-op. Override this method in your operation to provide compensation/rollback logic. Operations that don't override this are safely skipped during compensation.
Exceptions
- ArgumentNullException
Thrown when foundry is null.
- ObjectDisposedException
Thrown when the operation has been disposed.
WithExponentialBackoff(IWorkflowOperation, TimeSpan?, TimeSpan?, int, double, Func<Exception, bool>?, bool, string?, IWorkflowForgeLogger?)
Creates a retry operation with exponential backoff strategy.
public static RetryWorkflowOperation WithExponentialBackoff(IWorkflowOperation operation, TimeSpan? baseDelay = null, TimeSpan? maxDelay = null, int maxAttempts = 3, double backoffMultiplier = 2, Func<Exception, bool>? retryPredicate = null, bool enableJitter = true, string? name = null, IWorkflowForgeLogger? logger = null)
Parameters
operationIWorkflowOperationThe operation to retry.
baseDelayTimeSpan?Base delay for exponential backoff.
maxDelayTimeSpan?Maximum delay between retries.
maxAttemptsintMaximum number of attempts (default: 3).
backoffMultiplierdoubleMultiplier for exponential backoff (default: 2.0).
retryPredicateFunc<Exception, bool>Optional predicate to determine which exceptions to retry.
enableJitterboolWhether to enable jitter in retry delays (default: true).
namestringOptional name for the retry operation.
loggerIWorkflowForgeLoggerOptional logger for retry events.
Returns
- RetryWorkflowOperation
A new retry operation instance.
WithFixedInterval(IWorkflowOperation, TimeSpan, int, Func<Exception, bool>?, string?, IWorkflowForgeLogger?)
Creates a retry operation with a fixed interval strategy.
public static RetryWorkflowOperation WithFixedInterval(IWorkflowOperation operation, TimeSpan interval, int maxAttempts = 3, Func<Exception, bool>? retryPredicate = null, string? name = null, IWorkflowForgeLogger? logger = null)
Parameters
operationIWorkflowOperationThe operation to retry.
intervalTimeSpanThe fixed interval between retries.
maxAttemptsintMaximum number of attempts (default: 3).
retryPredicateFunc<Exception, bool>Optional predicate to determine which exceptions to retry.
namestringOptional name for the retry operation.
loggerIWorkflowForgeLoggerOptional logger for retry events.
Returns
- RetryWorkflowOperation
A new retry operation instance.
WithRandomInterval(IWorkflowOperation, TimeSpan, TimeSpan, int, Func<Exception, bool>?, string?, IWorkflowForgeLogger?)
Creates a retry operation with random interval strategy.
public static RetryWorkflowOperation WithRandomInterval(IWorkflowOperation operation, TimeSpan minDelay, TimeSpan maxDelay, int maxAttempts = 3, Func<Exception, bool>? retryPredicate = null, string? name = null, IWorkflowForgeLogger? logger = null)
Parameters
operationIWorkflowOperationThe operation to retry.
minDelayTimeSpanMinimum delay between retries.
maxDelayTimeSpanMaximum delay between retries.
maxAttemptsintMaximum number of attempts (default: 3).
retryPredicateFunc<Exception, bool>Optional predicate to determine which exceptions to retry.
namestringOptional name for the retry operation.
loggerIWorkflowForgeLoggerOptional logger for retry events.
Returns
- RetryWorkflowOperation
A new retry operation instance.