Skip to main content

StepBuilder

Struct StepBuilder 

Source
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>

Source

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.

Source

pub fn id(self, external_id: &str) -> Self

Set an external ID for forward references.

Source

pub fn on_error(self, behavior: ErrorBehavior) -> Self

Set the error handling behavior for this step.

When a step returns Err from its run method, the executor checks this behavior to decide what to do next.

§Example
.then::< risky::Step>()
    .name("Risky")
    .on_error(ErrorBehavior::Retry {
        interval: Duration::from_secs(5),
        max_retries: 3,
    })
Source

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.

Source

pub fn compensate_with<C: StepBody + Default + 'static>(self) -> Self

Register a compensation step for saga rollback.

When this step fails inside a saga container, the executor runs the compensation steps in reverse order to undo partial work.

§Example
.then::<ChargeCard>()
    .name("Charge")
    .compensate_with::<RefundCard>()
Source

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.

Source

pub fn then_fn( self, f: impl Fn() -> ExecutionResult + Send + Sync + 'static, ) -> StepBuilder<D>

Chain an inline function step.

Source

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")
Source

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.

Source

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.

Source

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.

Source

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.

Source

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);
})
Source

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.

Source

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);
Source

pub fn build(self, id: impl Into<String>, version: u32) -> WorkflowDefinition

Shortcut for end_workflow().build(id, version).

Auto Trait Implementations§

§

impl<D> Freeze for StepBuilder<D>

§

impl<D> !RefUnwindSafe for StepBuilder<D>

§

impl<D> Send for StepBuilder<D>

§

impl<D> Sync for StepBuilder<D>

§

impl<D> Unpin for StepBuilder<D>
where D: Unpin,

§

impl<D> UnsafeUnpin for StepBuilder<D>

§

impl<D> !UnwindSafe for StepBuilder<D>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more