Skip to main content

vlo_types/
command.rs

1//! Commands a reducer returns, and the reduction that carries them (see `docs/workflows.md`).
2
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5use serde_json::Value;
6
7use crate::RetryPolicy;
8
9/// What a reducer asks the kernel for. Seven commands, closed set (see `docs/workflows.md`).
10///
11/// `id` lets a reducer name a step explicitly (needed for fan-out bookkeeping);
12/// otherwise the kernel derives `<kind>#<n>` from `KernelState::step_no`.
13#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
14#[serde(tag = "type", rename_all = "snake_case")]
15pub enum Command {
16    /// Dispatch an activity to a worker; opens a `task` step in `scheduled`.
17    Task {
18        /// Activity name the worker dispatches on.
19        activity: String,
20        /// Input schema reference (`<name>@<content-hash>`).
21        #[serde(default, skip_serializing_if = "Option::is_none")]
22        schema_ref: Option<String>,
23        /// Activity input.
24        input: Value,
25        /// Queue to push to; `default` when `None`.
26        #[serde(default, skip_serializing_if = "Option::is_none")]
27        queue: Option<String>,
28        /// Retry policy; the default policy when `None`.
29        #[serde(default, skip_serializing_if = "Option::is_none")]
30        retry: Option<RetryPolicy>,
31        /// Lease duration in milliseconds; `60_000` when `None`.
32        #[serde(default, skip_serializing_if = "Option::is_none")]
33        lease_ttl_ms: Option<u64>,
34        /// Wall-clock budget for one attempt. The worker aborts at it; the host's backstop
35        /// reaps the attempt if the worker never answers.
36        #[serde(default, skip_serializing_if = "Option::is_none")]
37        timeout_ms: Option<u64>,
38        /// Explicit step id; `task#<n>` when `None`.
39        #[serde(default, skip_serializing_if = "Option::is_none")]
40        id: Option<String>,
41    },
42    /// Open a `timer` step that fires at an absolute or relative time.
43    Timer {
44        /// Delay in milliseconds from the event's `ts_ms`; ignored when `fire_at_ms` is set.
45        #[serde(default, skip_serializing_if = "Option::is_none")]
46        fire_in_ms: Option<u64>,
47        /// Absolute fire time in milliseconds since the Unix epoch. One of the two is required.
48        #[serde(default, skip_serializing_if = "Option::is_none")]
49        fire_at_ms: Option<u64>,
50        /// Explicit step id; `timer#<n>` when `None`.
51        #[serde(default, skip_serializing_if = "Option::is_none")]
52        id: Option<String>,
53    },
54    /// Start a child run; opens a `child` step in `scheduled`.
55    Spawn {
56        /// Workflow name of the child.
57        workflow: String,
58        /// Child run input.
59        input: Value,
60        /// Explicit step id; `child#<n>` when `None`.
61        #[serde(default, skip_serializing_if = "Option::is_none")]
62        id: Option<String>,
63    },
64    /// Park on a named signal; opens a `signal` step in `awaiting_signal`.
65    AwaitSignal {
66        /// Signal name.
67        name: String,
68        /// Payload schema reference (`<name>@<content-hash>`).
69        #[serde(default, skip_serializing_if = "Option::is_none")]
70        schema_ref: Option<String>,
71        /// Wait budget in milliseconds; `None` waits forever.
72        #[serde(default, skip_serializing_if = "Option::is_none")]
73        timeout_ms: Option<u64>,
74        /// Explicit step id; `signal#<n>` when `None`.
75        #[serde(default, skip_serializing_if = "Option::is_none")]
76        id: Option<String>,
77    },
78    /// Publish to a side channel. Never journaled, never replayed.
79    Emit {
80        /// Channel name.
81        channel: String,
82        /// Payload.
83        data: Value,
84    },
85    /// End the run as `completed`. Must be the last command of the reduction.
86    Complete {
87        /// Workflow output.
88        output: Value,
89    },
90    /// End the run as `continued` and start a successor with `input`. Must be the last
91    /// command of the reduction.
92    ContinueAsNew {
93        /// Input for the successor run.
94        input: Value,
95    },
96}
97
98/// What the reducer returned: the new user state (byte-exact JSON) and its commands.
99#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
100pub struct Reduction {
101    /// New user state, byte-exact JSON.
102    pub state: String,
103    /// Commands in issue order.
104    #[serde(default)]
105    pub commands: Vec<Command>,
106}