pub struct StepBuilder<D: WorkflowData> { /* private fields */ }Expand description
Builder for configuring a single step in the workflow.
Owns the WorkflowBuilder, consuming self on each method call. This avoids all lifetime/borrow issues.
Implementations§
Source§impl<D: WorkflowData> StepBuilder<D>
impl<D: WorkflowData> StepBuilder<D>
Sourcepub fn name(self, name: &str) -> Self
pub fn name(self, name: &str) -> Self
Set the human-readable display name of the current step.
This name appears in logs, the execution trace, and the web UI.
Sourcepub fn on_error(self, behavior: ErrorBehavior) -> Self
pub fn on_error(self, behavior: ErrorBehavior) -> Self
Sourcepub fn config(self, config: Value) -> Self
pub fn config(self, config: Value) -> Self
Attach arbitrary JSON configuration to this step.
The step can read it at runtime via context.step.step_config.
Sourcepub fn compensate_with<C: StepBody + Default + 'static>(self) -> Self
pub fn compensate_with<C: StepBody + Default + 'static>(self) -> Self
Sourcepub fn then<S: StepBody + Default + 'static>(self) -> StepBuilder<D>
pub fn then<S: StepBody + Default + 'static>(self) -> StepBuilder<D>
Chain the next step sequentially.
Wires an outcome from the current step to the new one so that when the
current step returns ExecutionResult::next,
execution continues with S.
Sourcepub fn then_fn(
self,
f: impl Fn() -> ExecutionResult + Send + Sync + 'static,
) -> StepBuilder<D>
pub fn then_fn( self, f: impl Fn() -> ExecutionResult + Send + Sync + 'static, ) -> StepBuilder<D>
Chain an inline function step.
Sourcepub fn wait_for(self, event_name: &str, event_key: &str) -> StepBuilder<D>
pub fn wait_for(self, event_name: &str, event_key: &str) -> StepBuilder<D>
Suspend the workflow until an external event arrives.
The workflow pauses and the executor releases the lock. When you call
WorkflowHost::publish_event (from the wfe crate) with
a matching event_name and event_key, the workflow resumes from this point.
§Example
.then::<RequestApproval>()
.name("Request Approval")
.wait_for("approval", "order-123")
.name("Wait for approval")Sourcepub fn delay(self, duration: Duration) -> StepBuilder<D>
pub fn delay(self, duration: Duration) -> StepBuilder<D>
Pause execution for a fixed duration.
The executor persists the workflow, sleeps for the given duration, then re-queues the instance for continued execution.
Sourcepub fn if_do<S: StepBody + Default + 'static>(
self,
build_children: impl FnOnce(&mut WorkflowBuilder<D>),
) -> StepBuilder<D>
pub fn if_do<S: StepBody + Default + 'static>( self, build_children: impl FnOnce(&mut WorkflowBuilder<D>), ) -> StepBuilder<D>
Conditional branching.
The closure builds the child steps that run when the condition evaluates
to true. Use .then::<ConditionStep>().if_do(|b| { ... }) where
ConditionStep returns ExecutionResult::branch("true")
or branch("false") to control which path is taken.
Sourcepub fn while_do<S: StepBody + Default + 'static>(
self,
build_children: impl FnOnce(&mut WorkflowBuilder<D>),
) -> StepBuilder<D>
pub fn while_do<S: StepBody + Default + 'static>( self, build_children: impl FnOnce(&mut WorkflowBuilder<D>), ) -> StepBuilder<D>
Loop while a condition holds.
The closure builds the body of the loop. A condition step (type S)
should return ExecutionResult::next()
to continue looping or ExecutionResult::next() with a condition that evaluates to false
to break out.
Sourcepub fn for_each<S: StepBody + Default + 'static>(
self,
build_children: impl FnOnce(&mut WorkflowBuilder<D>),
) -> StepBuilder<D>
pub fn for_each<S: StepBody + Default + 'static>( self, build_children: impl FnOnce(&mut WorkflowBuilder<D>), ) -> StepBuilder<D>
Iterate over a collection.
The step type S receives each item via
StepExecutionContext::item.
The collection is taken from the workflow data field configured in the
step’s step_config.
Sourcepub fn saga(
self,
build_children: impl FnOnce(&mut WorkflowBuilder<D>),
) -> StepBuilder<D>
pub fn saga( self, build_children: impl FnOnce(&mut WorkflowBuilder<D>), ) -> StepBuilder<D>
Transaction-like container with compensation on failure.
Child steps run normally. If any child fails, the executor runs
compensation steps (registered via compensate_with)
in reverse order to undo partial work.
§Example
.saga(|b| {
b.add_step_typed::<ReserveInventory>("reserve", None);
b.add_step_typed::<ChargeCard>("charge", None);
b.add_step_typed::<ShipOrder>("ship", None);
})Sourcepub fn parallel(
self,
build_branches: impl FnOnce(ParallelBuilder<D>) -> ParallelBuilder<D>,
) -> StepBuilder<D>
pub fn parallel( self, build_branches: impl FnOnce(ParallelBuilder<D>) -> ParallelBuilder<D>, ) -> StepBuilder<D>
Run multiple branches concurrently.
All branches inside the [ParallelBuilder] execute in parallel. The
workflow continues to the next step only after all branches complete.
Sourcepub fn end_workflow(self) -> WorkflowBuilder<D>
pub fn end_workflow(self) -> WorkflowBuilder<D>
Finish building the current branch and return the WorkflowBuilder.
Call this when you are done chaining steps. You can then call
WorkflowBuilder::build to compile the definition.
§Example
let def = WorkflowBuilder::<MyData>::new()
.start_with::<A>()
.then::<B>()
.end_workflow()
.build("my-workflow", 1);