Skip to main content

isothrum_runner/
state.rs

1//! The words for what a run is doing and where it stands, shared by the API, the record and the
2//! event stream.
3
4mod transition;
5
6use std::fmt;
7
8use serde::{Deserialize, Serialize};
9
10pub use transition::{Cause, Desired, IllegalTransition, Transition};
11
12/// An operation on a run.
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, schemars::JsonSchema)]
14#[serde(rename_all = "snake_case")]
15#[non_exhaustive]
16pub enum Op {
17    /// Start the run.
18    Up,
19    /// Remove everything the run created.
20    Down,
21    /// Stop the run, keeping what it created.
22    Park,
23    /// Start a parked run again.
24    Resume,
25    /// Run the run's checks.
26    Check,
27    /// Run the set-up again on running machines.
28    Provision,
29    /// Hot-add the on-demand access machine into the running run.
30    Attach,
31    /// Hot-remove the on-demand access machine from the running run.
32    Detach,
33    /// Bring the running run up to date with its manifest.
34    Update,
35    /// Take a machine out of the running run, ahead of its removal from the manifest.
36    Retire,
37    /// Save a machine of the running run as a box a later run boots from.
38    Package,
39}
40
41impl Op {
42    /// Every operation, in the order they are declared.
43    pub(crate) const ALL: [Op; 11] = [
44        Op::Up,
45        Op::Down,
46        Op::Park,
47        Op::Resume,
48        Op::Check,
49        Op::Provision,
50        Op::Attach,
51        Op::Detach,
52        Op::Update,
53        Op::Retire,
54        Op::Package,
55    ];
56
57    /// The operation as written in events (`up`).
58    #[must_use]
59    pub fn as_str(self) -> &'static str {
60        match self {
61            Op::Up => "up",
62            Op::Down => "down",
63            Op::Park => "park",
64            Op::Resume => "resume",
65            Op::Check => "check",
66            Op::Provision => "provision",
67            Op::Attach => "attach",
68            Op::Detach => "detach",
69            Op::Update => "update",
70            Op::Retire => "retire",
71            Op::Package => "package",
72        }
73    }
74
75    /// The state a run is in while this operation runs.
76    pub(crate) fn during(self) -> Option<RunState> {
77        match self {
78            Op::Up => Some(RunState::Creating),
79            Op::Down => Some(RunState::Destroying),
80            Op::Park => Some(RunState::Parking),
81            Op::Resume => Some(RunState::Resuming),
82            Op::Update => Some(RunState::Updating),
83            // These change one machine of a run that keeps running, or nothing: the run stays
84            // where it is meanwhile, and their journal says they ran.
85            Op::Check | Op::Provision | Op::Attach | Op::Detach | Op::Retire | Op::Package => None,
86        }
87    }
88}
89
90impl fmt::Display for Op {
91    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
92        f.write_str(self.as_str())
93    }
94}
95
96/// Where a run stands.
97#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, schemars::JsonSchema)]
98#[serde(rename_all = "snake_case")]
99#[non_exhaustive]
100pub enum RunState {
101    /// Being created by an `up`.
102    Creating,
103    /// Up, every member machine running.
104    Ready,
105    /// Something of it exists, but not every member machine runs (a failed start, a machine down).
106    Failed,
107    /// Being parked.
108    Parking,
109    /// Parked: stopped, what it created kept.
110    Parked,
111    /// Being resumed.
112    Resuming,
113    /// Being brought up to date with its manifest by an `update`.
114    Updating,
115    /// Being removed by a `down`.
116    Destroying,
117    /// Nothing of it exists.
118    Absent,
119}
120
121impl RunState {
122    /// Whether the run is in the middle of an operation that changes it: a state some operation
123    /// holds while it runs ([`Op::during`]).
124    pub(crate) fn is_moving(self) -> bool {
125        self.moved_by().is_some()
126    }
127
128    /// The operation that holds the run in this state while it runs ([`Op::during`]), if any:
129    /// the one a record left in it was cut short in, when no operation holds the run.
130    pub(crate) fn moved_by(self) -> Option<Op> {
131        Op::ALL.into_iter().find(|op| op.during() == Some(self))
132    }
133
134    /// The state as written in events (`ready`).
135    #[must_use]
136    pub fn as_str(self) -> &'static str {
137        match self {
138            RunState::Creating => "creating",
139            RunState::Ready => "ready",
140            RunState::Failed => "failed",
141            RunState::Parking => "parking",
142            RunState::Parked => "parked",
143            RunState::Resuming => "resuming",
144            RunState::Updating => "updating",
145            RunState::Destroying => "destroying",
146            RunState::Absent => "absent",
147        }
148    }
149}
150
151impl fmt::Display for RunState {
152    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
153        f.write_str(self.as_str())
154    }
155}
156
157/// How a run is parked: the manifest's `lifecycle.park.pause` or `.shutdown` steps.
158#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
159#[serde(rename_all = "snake_case")]
160#[non_exhaustive]
161pub enum Park {
162    /// Suspended, memory kept where the target can (Docker: stopped).
163    Pause,
164    /// Shut down.
165    Shutdown,
166}
167
168impl Park {
169    /// The mode as the manifest names it.
170    #[must_use]
171    pub fn as_str(self) -> &'static str {
172        match self {
173            Park::Pause => "pause",
174            Park::Shutdown => "shutdown",
175        }
176    }
177}
178
179impl fmt::Display for Park {
180    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
181        f.write_str(self.as_str())
182    }
183}
184
185#[cfg(test)]
186mod tests {
187    use super::*;
188
189    #[test]
190    fn every_op_state_and_park_mode_is_written_as_serde_writes_it() {
191        crate::testing::assert_named_as_serde(Op::as_str);
192        crate::testing::assert_named_as_serde(RunState::as_str);
193        crate::testing::assert_named_as_serde(Park::as_str);
194    }
195
196    #[test]
197    fn every_op_is_listed_and_the_moving_states_are_those_an_op_holds() {
198        assert_eq!(crate::testing::every_variant::<Op>(), Op::ALL);
199        let moving: Vec<RunState> = crate::testing::every_variant::<RunState>().into_iter().filter(|s| s.is_moving()).collect();
200        assert_eq!(
201            moving,
202            [
203                RunState::Creating,
204                RunState::Parking,
205                RunState::Resuming,
206                RunState::Updating,
207                RunState::Destroying
208            ]
209        );
210    }
211}