pub struct Run<C = Value, E = Value> { /* private fields */ }Expand description
A run of a machine on xmachines-runtime, one chunk at a time: the
Stepper of the workflow contract. It applies a chunk with one call of
the runtime, which puts the run back itself when it refuses the chunk:
the machine is shared and never compiled again.
The snapshot is lazy: Run::step gives what a chunk did and keeps the
runtime’s step; Run::snapshot makes the snapshot of it, and the
engine’s state only when asked. Stepper::apply makes the records,
with the state at a checkpoint.
use std::sync::Arc;
use serde_json::{json, Value};
use xmachines_runtime::{Machine, Registry};
use xmachines_workflow::{Chunk, Options, Record, Run, Stepper};
let machine = Arc::new(Machine::<Value, Value>::from_json(
&json!({ "initial": "a", "states": { "a": { "on": { "go": { "target": "b" } } }, "b": {} } }),
&Registry::new(),
).unwrap());
let (mut run, opening) = Run::open(machine, Options::default()).unwrap();
assert_eq!(opening.len(), 1);
let mut records = Vec::new();
let seq = run.apply(Chunk::Batch(vec![json!({ "kind": "time", "now": 5 }), json!({ "type": "go" })]), &mut records).unwrap();
let Record::Snapshot { snapshot, .. } = &records[0] else { unreachable!() };
assert_eq!((seq, snapshot.value.clone(), snapshot.time), (1, json!("b"), 5));Implementations§
Source§impl<C, E> Run<C, E>
impl<C, E> Run<C, E>
Sourcepub fn open(
machine: Arc<Machine<C, E>>,
options: Options,
) -> Result<(Run<C, E>, Vec<Record>), Fault>
pub fn open( machine: Arc<Machine<C, E>>, options: Options, ) -> Result<(Run<C, E>, Vec<Record>), Fault>
A run of machine, started, or reopened of options.from, and its
opening records: the events the root emitted as it started and its
snapshot, of seq 0, or the snapshot of the checkpoint it reopened.
A start whose root fails is WORKFLOW_MACHINE_FAILED; a checkpoint
with no state, or one the runtime can’t restore,
WORKFLOW_INVALID_SNAPSHOT; a maxTimers or a checkpoint of no
whole number of 1 or more, a start time past 2^53 - 1, or an input
beside from, WORKFLOW_INVALID_INPUT.
Sourcepub fn from_actor(
actor: Actor<C, E>,
seq: u64,
checkpoints: Checkpoints,
) -> Result<Run<C, E>, Fault>
pub fn from_actor( actor: Actor<C, E>, seq: u64, checkpoints: Checkpoints, ) -> Result<Run<C, E>, Fault>
A run of an actor a host started or restored itself, as one of a
changelog, migrated to a newer machine: the run goes on from the
actor’s state as the run of chunk seq, and its snapshots hold the
state as checkpoints says. Gives no record: the host has its own of
the actor. An actor whose runtime has broken is
WORKFLOW_ENGINE_FAILED.
Sourcepub fn step(&mut self, chunk: &Chunk<Value>) -> Result<Applied, Fault>
pub fn step(&mut self, chunk: &Chunk<Value>) -> Result<Applied, Fault>
Applies one chunk, all or nothing, and says what it did, making no
snapshot: the cheap form, for a host that makes the snapshot only when
it needs one (Run::snapshot), as a partition of a broker, once per
commit. A fault is as of Stepper::apply.
Sourcepub fn last(&self) -> &Step
pub fn last(&self) -> &Step
The runtime’s step after the last chunk that applied, or of the opening: the root, the time, the deadline and the work, with no snapshot made.
Sourcepub fn snapshot(&self, with_state: bool) -> Snapshot
pub fn snapshot(&self, with_state: bool) -> Snapshot
The snapshot of the last chunk that applied, or of the opening, with
the engine’s state when with_state.
Sourcepub fn record(&self) -> Record
pub fn record(&self) -> Record
The snapshot record of the last chunk that applied, with the state as the run’s checkpoint setting has it.
Sourcepub fn checkpoint(&self) -> Checkpoint
pub fn checkpoint(&self) -> Checkpoint
The run as a checkpoint now, whatever its checkpoint setting: its
latest snapshot with the engine’s state, and the number of the last
chunk read, which a run reopens of (from).
Sourcepub fn can(&self, event: &Value) -> bool
pub fn can(&self, event: &Value) -> bool
Whether the root would take event now: some transition handles it.
It selects by guards alone, never running what a transition does.
An event of a type that starts with xstate., which no caller writes,
and an event a guard of throws on, are never taken. Changes nothing.
A guard that reads the run’s children or history answers of the real
state here, while the kit’s XState engine answers as if there were
none: such a guard can answer differently in the two.
Sourcepub fn actor(&self) -> &Actor<C, E>
pub fn actor(&self) -> &Actor<C, E>
The runtime’s actor, as a host reads it: its persisted system, by
persist_view, written once per commit.
Sourcepub fn logs(&self) -> &[Vec<Value>]
pub fn logs(&self) -> &[Vec<Value>]
The argument lists of the root’s @xstate.log actions of the last
chunk that applied, or of the opening: a run keeps no others.
Sourcepub fn checkpoints(&self) -> Checkpoints
pub fn checkpoints(&self) -> Checkpoints
Which snapshots hold the engine’s state.
Sourcepub fn max_timers(&self) -> usize
pub fn max_timers(&self) -> usize
How many timers a time input fires at most in a chunk of control inputs alone, as the run started with it.
Source§impl<C, E> Run<C, E>
impl<C, E> Run<C, E>
Sourcepub fn apply_text(&mut self, chunk: &str) -> Result<Vec<String>, String>
pub fn apply_text(&mut self, chunk: &str) -> Result<Vec<String>, String>
Stepper::apply of a chunk of JSON text (chunk.schema.json): its
records, each JSON text, or its fault as JSON text. Text that is not
JSON is a chunk that is no input, WORKFLOW_INVALID_INPUT, of the
chunk’s seq, and the run goes on.
Sourcepub fn can_text(&self, event: &str) -> bool
pub fn can_text(&self, event: &str) -> bool
Run::can of an event of JSON text: false of text that is no
event.
Sourcepub fn checkpoint_text(&self) -> String
pub fn checkpoint_text(&self) -> String
Run::checkpoint as JSON text.
Trait Implementations§
Source§impl<C, E> Stepper for Run<C, E>
impl<C, E> Stepper for Run<C, E>
Source§fn apply(
&mut self,
chunk: Chunk<Value>,
out: &mut impl Extend<Record>,
) -> Result<u64, Fault>
fn apply( &mut self, chunk: Chunk<Value>, out: &mut impl Extend<Record>, ) -> Result<u64, Fault>
out:
the events it made the machine emit, then its snapshot, or its
rejection. Gives the chunk’s seq. A fault is the chunk’s terminal
outcome, and gave no record: the run goes on after one whose code
FaultCode::is_recoverable, and gives the same fault again of every
chunk after any other.