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