Functional API
With the functional API, you write the workflow as one async function. Tasks start concurrently and return futures.
Entrypoint and tasks
Section titled “Entrypoint and tasks”use warpra::*;use serde_json::{json, Value};
let fetch = TaskDef::new("fetch", |args: Value, _c: NodeCtx| async move { Ok(json!({"doc": format!("doc-{}", args["u"].as_str().unwrap_or(""))}))});let score = TaskDef::new("score", |args: Value, _c: NodeCtx| async move { Ok(json!(args["doc"].as_str().unwrap_or("").len()))});
let wf = Entrypoint::new("wf", move |input: Value, ctx: NodeCtx| { let (fetch, score) = (fetch.clone(), score.clone()); async move { let doc = fetch.call(&ctx, json!({"u": input["u"]})).await?; let ok = ctx.interrupt(json!({"doc": doc}))?; let s = score.call(&ctx, doc.clone()).await?; Ok(EntryResult::final_(json!({"score": s, "approved": ok}), ok)) }}).checkpointer(std::sync::Arc::new(InMemorySaver::new())).build();
let cfg = RunConfig::thread("t");let paused = wf.invoke(json!({"u": "x"}), cfg.clone()).await?; // interrupt with the fetched doclet done = wf.invoke(Command::new().resume(true), cfg).await?; // {"score": 5, "approved": true}Entrypoint::build() returns a Pregel, so invoke, stream, get_state and the rest work as for a StateGraph.
How resume works
Section titled “How resume works”Task results are written as pending writes of the running checkpoint. When the entrypoint re-runs after an interrupt or a crash, tasks that already finished return their recorded result without running again. In the example above, fetch runs once even though the entrypoint runs twice.
Because of this, call tasks in a deterministic order.
Tasks without a TaskDef
Section titled “Tasks without a TaskDef”ctx.call(name, args, f) starts a one-off task from a closure. ctx.call_with(.., TaskOpts) sets options.
Saving a value between runs
Section titled “Saving a value between runs”EntryResult::final_(value, save) returns value to the caller and saves save for the next run on the same thread. Read it with ctx.previous():
let wf = Entrypoint::new("counter", |input: Value, ctx: NodeCtx| async move { let prev = ctx.previous().and_then(|v| v.as_i64()).unwrap_or(0); let total = prev + input.as_i64().unwrap_or(0); Ok::<_, NodeError>(EntryResult::final_(json!(format!("total={total}")), json!(total)))}).checkpointer(std::sync::Arc::new(InMemorySaver::new())).build();// invoke(5) -> "total=5", then invoke(3) on the same thread -> "total=8"Returning a plain Value returns and saves the same value.
Options
Section titled “Options”Entrypoint takes checkpointer, store, cache, retry_policy, cache_policy, step_timeout and timeout. TaskDef takes retry, cache and timeout.
Warpra is open source under Apache-2.0. Made by Liceron.