Path: redb-route/redb.Route/Definitions/OnCompletionDefinition.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/Definitions/OnCompletionDefinition.cs
----
using redb.Route.Abstractions;
using redb.Route.Predicates;
namespace redb.Route.Definitions;
/// When an runs.
public enum CompletionMode
{
/// After success and after failure.
Always,
/// Only when the exchange completed without a propagated exception (a handled error counts as complete).
OnCompleteOnly,
/// Only when the exchange escaped the route with an exception.
OnFailureOnly,
}
///
/// Steps that run after the route has finished with an exchange (Camel onCompletion): audit,
/// notifications, cleanup. They receive a copy of the exchange, run outside the route's
/// transaction and error handlers, and never change the route's own result. As in Camel, they run in the
/// caller once the route has finished, and the caller waits for them; or
/// runs them on a bounded pool of their own instead, and
/// runs them before the consumer gets the reply (HTTP / SOAP, where the reply
/// is the consumer's job). A block after the consumer never gets the caller's cancellation: an aborted request
/// does not cut it half way.
/// Declared on a route or on the RouteBuilder for every route it defines.
///
public sealed class OnCompletionDefinition : RouteDefinitionBase, IRouteScope
{
internal OnCompletionDefinition() { }
/// Success, failure or both. Default .
public CompletionMode Mode { get; private set; } = CompletionMode.Always;
/// Optional condition evaluated on the copy; when it does not hold the steps are skipped.
public IPredicate? Condition { get; private set; }
/// Run before the consumer receives the reply instead of asynchronously after it.
public bool BeforeConsumer { get; private set; }
/// Run only after a successful exchange.
public OnCompletionDefinition OnCompleteOnly() { Mode = CompletionMode.OnCompleteOnly; return this; }
/// Run only after a failed exchange (the exception is on the copy).
public OnCompletionDefinition OnFailureOnly() { Mode = CompletionMode.OnFailureOnly; return this; }
/// Run only when the route-language condition holds ("header.audit").
public OnCompletionDefinition When(string condition)
{
ArgumentException.ThrowIfNullOrWhiteSpace(condition);
return When(PredicateFactory.FromString(condition));
}
/// Run only when holds.
public OnCompletionDefinition When(IPredicate predicate)
{
Condition = predicate ?? throw new ArgumentNullException(nameof(predicate));
return this;
}
/// Run only when holds.
public OnCompletionDefinition When(Func predicate)
{
ArgumentNullException.ThrowIfNull(predicate);
return When(new LambdaPredicate(predicate));
}
/// Run synchronously before the consumer receives the reply.
public OnCompletionDefinition ModeBeforeConsumer() { BeforeConsumer = true; return this; }
/// Whether the block runs on a pool of its own instead of in the caller (Camel parallelProcessing).
public bool IsParallelProcessing { get; private set; }
/// The thread pool profile the block's pool is built from; null for the default profile.
public string? ExecutorServiceName { get; private set; }
///
/// Run the block on a pool of its own built from the context's default thread pool profile, so the caller does not
/// wait for it (Camel parallelProcessing). Without it the block runs in the caller once the route has finished.
///
public OnCompletionDefinition ParallelProcessing() { IsParallelProcessing = true; return this; }
///
/// Run the block on a pool of its own built from the named thread pool profile (Camel executorService, which
/// implies parallelProcessing). An unknown name fails when the route is built.
///
public OnCompletionDefinition ExecutorService(string name)
{
ArgumentException.ThrowIfNullOrWhiteSpace(name);
ExecutorServiceName = name;
IsParallelProcessing = true;
return this;
}
/// Closes the scope and returns to the route it was declared on.
public IRouteDefinition EndOnCompletion()
=> (IRouteDefinition)(Parent ?? throw new InvalidOperationException(
"EndOnCompletion() on a builder-level OnCompletion: it has no parent route — stop chaining instead."));
///
public IRouteDefinition End() => EndOnCompletion();
/// Compiles the completion steps as a pipeline; the context runs it after the route.
public override IProcessor CreateProcessor(IRouteContext context) => NodePipeline.Body(context, Outputs);
}