Expand description
Intercept inbound and outbound calls made during workflow execution.
Workflow interceptors allow observing, transforming, or short-circuit workflow operations without putting that behavior in each workflow implementation.
WorkflowInterceptor has two groups of methods:
- Inbound methods wrap calls into workflow code, such as executing the workflow or handling a signal, query, or update.
- Outbound methods wrap commands issued by workflow code, such as scheduling an activity, starting a timer, or signaling another workflow.
Each method receives a WorkflowNext continuation. An interceptor can change the input
before calling WorkflowNext::run, inspect or change the returned value, or deliberately not
call next to short-circuit the operation. Most interceptors should call next exactly once.
Async operation interceptors return WorkflowInterceptorFuture. Wrap an async block with
WorkflowInterceptorFuture::new when work must happen after the next interceptor completes.
Synchronous methods, including queries and update validators, cannot await workflow operations.
Workers register interceptors with register_workflow_interceptors on their
worker options. Interceptors are entered in insertion order for inbound calls and in reverse
insertion order for outbound calls.
§Determinism
Interceptors execute as part of the workflow and are replayed with it. They must follow the same
determinism rules as workflow code: do not read wall-clock time, perform network or filesystem
I/O, use nondeterministic randomness, or await arbitrary futures. Use values from the
interceptor context and SDK-provided workflow futures instead. WorkflowInterceptorFuture
identifies a future for the workflow scheduler; it does not make an arbitrary future
deterministic.
WorkflowInterceptorContext::is_replaying and
WorkflowInterceptorContext::is_replaying_history_events can be used to suppress duplicate external
observability during replay, but replay state must not change commands or results that affect
workflow behavior.
§Example
This interceptor wraps workflow execution and transforms string outputs after the workflow has completed. The constructor is passed to the worker during worker setup.
struct UppercaseStringOutput;
impl WorkflowInterceptor for UppercaseStringOutput {
fn execute<'a>(
&'a self,
_ctx: WorkflowInterceptorContext,
input: ExecuteWorkflowInput,
next: WorkflowNext<
'a,
ExecuteWorkflowInput,
WorkflowInterceptorFuture<'a, ExecuteWorkflowResult>,
>,
) -> WorkflowInterceptorFuture<'a, ExecuteWorkflowResult> {
WorkflowInterceptorFuture::new(async move {
let output = next.run(input).await?;
if let Some(value) = output.downcast_ref::<String>() {
return Ok(Box::new(value.to_uppercase()) as Box<dyn WorkflowOutputValue>);
}
Ok(output)
})
}
}
fn interceptor_constructor() -> WorkflowInterceptorConstructor {
WorkflowInterceptorConstructor::new(|_ctx: &WorkflowContextView| UppercaseStringOutput)
}
Structs§
- Cancel
External Workflow Input - Input passed to
WorkflowInterceptor::cancel_external_workflow. - Cancellable
Workflow Outbound Future - Future returned by a cancellable outbound interceptor operation.
- Continue
AsNew Input - Input passed to
WorkflowInterceptor::continue_as_new. - Execute
Workflow Input - Input passed to
WorkflowInterceptor::execute. - Handle
Query Input - Input passed to
WorkflowInterceptor::handle_query. - Handle
Signal Input - Input passed to
WorkflowInterceptor::handle_signal. - Handle
Update Input - Input passed to
WorkflowInterceptor::handle_update. - Initialize
Workflow Input - Input passed to
WorkflowInterceptor::initialize_workflow. - Initialize
Workflow Output - Result of workflow initialization.
- Schedule
Activity Input - Input passed to
WorkflowInterceptor::schedule_activity. - Schedule
Local Activity Input - Input passed to
WorkflowInterceptor::schedule_local_activity. - Signal
Workflow Input - Input passed to
WorkflowInterceptor::signal_workflow. - Start
Child Workflow Input - Input passed to
WorkflowInterceptor::start_child_workflow. - Start
Nexus Operation Input - Input passed to
WorkflowInterceptor::start_nexus_operation. - Start
Timer Input - Input passed to
WorkflowInterceptor::start_timer. - Sync
Workflow Interceptor Context - Workflow execution context available to sync-only inbound interceptors.
- Validate
Update Input - Input passed to
WorkflowInterceptor::validate_update. - Workflow
Cancellation Handle - Cancellation callback retained when an interceptor wraps an operation future.
- Workflow
Interceptor Constructor - Creates one interceptor for each in-memory workflow instance.
- Workflow
Interceptor Context - Workflow execution context available to async-capable inbound interceptors.
- Workflow
Interceptor Future - Future produced by workflow interceptors.
- Workflow
Next - Continuation for a workflow interceptor operation.
- Workflow
Outbound Future - Future returned by a non-cancellable outbound interceptor operation.
Enums§
- Signal
Workflow Target - Workflow targeted by an outbound signal.
Traits§
- Workflow
Interceptor - Interceptor for calls into workflow code and commands issued by workflow code.
- Workflow
Outbound Value - Type-erased output returned by an intercepted outbound workflow call.
- Workflow
Output Value - Type-erased workflow output carried through the workflow interceptor chain.
Type Aliases§
- Child
Workflow Outbound Result - Result of an intercepted child workflow completion.
- Continue
AsNew Result - Result of an intercepted continue-as-new call.
- Execute
Workflow Result - Result of an intercepted workflow execution.
- Handle
Query Result - Result of an intercepted query handler.
- Handle
Signal Result - Result of an intercepted signal handler.
- Handle
Update Result - Result of an intercepted update handler.
- Schedule
Activity Result - Result of an intercepted activity call.
- Signal
Workflow Result - Result of an intercepted signal call.
- Start
Child Workflow Result - Result of an intercepted child workflow start.
- Start
Nexus Operation Result - Result of an intercepted Nexus operation start.
- Validate
Update Result - Result of an intercepted update validator.