Guides

Setting & Getting State


Setting and Getting State

The WorkflowContext provides set_state() and get_state() methods to store and retrieve typed values by string key. State values are JSON-serialized, so any serializable type can be stored.

1
use orcher::prelude::*;
2
use anyhow::Result;
3
4
#[task]
5
pub async fn process_payment(_ctx: TaskContext, amount: f64) -> Result<PaymentResult> {
6
// ...
7
}
8
9
#[workflow]
10
pub async fn order_workflow(
11
ctx: WorkflowContext,
12
input: OrderInput,
13
) -> Result<OrderResult> {
14
// Set state values
15
ctx.set_state("order_id", &input.order_id)?;
16
ctx.set_state("status", &"processing")?;
17
ctx.set_state("customer_id", &input.customer_id)?;
18
19
// Process order
20
let payment_result: PaymentResult = ctx
21
.execute_task(process_payment, input.amount)
22
.await?;
23
24
// Update state after task completes
25
ctx.set_state("payment_id", &payment_result.payment_id)?;
26
ctx.set_state("status", &"paid")?;
27
28
// Get state value
29
let current_status: Option<String> = ctx.get_state("status")?;
30
println!("Current status: {:?}", current_status);
31
32
Ok(OrderResult {
33
order_id: input.order_id,
34
payment_id: payment_result.payment_id,
35
status: current_status.unwrap_or_default(),
36
})
37
}

State Lifecycle

State Persistence

State is stored in-memory during workflow execution and reconstructed from the journal on replay. This means state values set before a crash are available after the workflow resumes — the replay re-executes the workflow code, and set_state() calls run again with the same deterministic values.

  1. After set_state() calls — Value is stored in the in-memory state map
  2. After task completions — The completed task result is journaled; on replay, the same result is returned and the subsequent set_state() calls produce identical state
  3. On crash/restart — The journal is replayed, reconstructing the same state
  4. During replay — set_state() calls execute normally but produce the same values because the workflow is deterministic

State During Replay

The diagram below shows how state is consistent across first execution and replay. Since workflows are deterministic and task results are journaled, every set_state() call produces the same value on replay.

First execution
set_state("order_id", "order_123")
stored in memory
set_state("status", "processing")
stored in memory
execute_task("process_payment")
task executes → result journaled
set_state("payment_id", "pay_456")
stored in memory
set_state("status", "paid")
stored in memory
Replay after crash
set_state("order_id", "order_123")
same value · deterministic
set_state("status", "processing")
same value · deterministic
execute_task("process_payment")
skipped → returns journaled result
set_state("payment_id", "pay_456")
same value · from journaled result
set_state("status", "paid")
same value · deterministic

Every set_state() produces the same value on replay — state stays consistent across crashes.


Next Steps