Skip to content

FallbackExecutor

NexusLabs.Needlr.AgentFramework.Workflows

NexusLabs.Needlr.AgentFramework.Workflows.Sequential

FallbackExecutor Class

Tries a primary executor and, on failure, falls back to a secondary executor. User cancellation is never swallowed.

public sealed class FallbackExecutor : NexusLabs.Needlr.AgentFramework.Workflows.Sequential.IStageExecutor

Inheritance System.Object 🡒 FallbackExecutor

Implements IStageExecutor

Example

// Default — falls back on any failure
var executor = new FallbackExecutor(primaryExecutor, fallbackExecutor);

// Narrow — only fall back on timeouts
var executor = new FallbackExecutor(primaryExecutor, fallbackExecutor,
    shouldFallback: ex => ex is TaskCanceledException or HttpRequestException);

Constructors

FallbackExecutor(IStageExecutor, IStageExecutor, Func<Exception,bool>) Constructor

Tries a primary executor and, on failure, falls back to a secondary executor. User cancellation is never swallowed.

public FallbackExecutor(NexusLabs.Needlr.AgentFramework.Workflows.Sequential.IStageExecutor primary, NexusLabs.Needlr.AgentFramework.Workflows.Sequential.IStageExecutor fallback, System.Func<System.Exception,bool>? shouldFallback=null);

Parameters

primary IStageExecutor

The preferred executor to try first.

fallback IStageExecutor

The executor to use if the primary throws.

shouldFallback System.Func<System.Exception,System.Boolean>

Optional predicate controlling which exceptions trigger fallback. When null (the default), any non-cancellation exception triggers fallback. When provided, only exceptions where the predicate returns true trigger fallback; others propagate.

Example

// Default — falls back on any failure
var executor = new FallbackExecutor(primaryExecutor, fallbackExecutor);

// Narrow — only fall back on timeouts
var executor = new FallbackExecutor(primaryExecutor, fallbackExecutor,
    shouldFallback: ex => ex is TaskCanceledException or HttpRequestException);

Methods

FallbackExecutor.ExecuteAsync(StageExecutionContext, CancellationToken) Method

Executes the stage logic and returns a result indicating success or failure.

public System.Threading.Tasks.Task<NexusLabs.Needlr.AgentFramework.Workflows.Sequential.StageExecutionResult> ExecuteAsync(NexusLabs.Needlr.AgentFramework.Workflows.Sequential.StageExecutionContext context, System.Threading.CancellationToken cancellationToken);

Parameters

context StageExecutionContext

The execution context providing access to shared pipeline state.

cancellationToken System.Threading.CancellationToken

Token to observe for cancellation.

Implements ExecuteAsync(StageExecutionContext, CancellationToken)

Returns

System.Threading.Tasks.Task<StageExecutionResult>
A StageExecutionResult describing the outcome.