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}
30
31impl Op {
32    /// The operation as written in events (`up`).
33    #[must_use]
34    pub fn as_str(self) -> &'static str {
35        match self {
36            Op::Up => "up",
37            Op::Down => "down",
38            Op::Park => "park",
39            Op::Resume => "resume",
40            Op::Check => "check",
41            Op::Provision => "provision",
42            Op::Attach => "attach",
43            Op::Detach => "detach",
44        }
45    }
46
47    /// The state a run is in while this operation runs.
48    pub(crate) fn during(self) -> Option<RunState> {
49        match self {
50            Op::Up => Some(RunState::Starting),
51            Op::Down => Some(RunState::Stopping),
52            Op::Park => Some(RunState::Parking),
53            Op::Resume => Some(RunState::Resuming),
54            Op::Check | Op::Provision | Op::Attach | Op::Detach => None,
55        }
56    }
57}
58
59impl fmt::Display for Op {
60    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61        f.write_str(self.as_str())
62    }
63}
64
65/// Where a run stands.
66#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, schemars::JsonSchema)]
67#[serde(rename_all = "snake_case")]
68#[non_exhaustive]
69pub enum RunState {
70    /// Being started.
71    Starting,
72    /// Up, every target machine running.
73    Ready,
74    /// Something of it exists, but not every target machine runs (a failed start, a machine down).
75    Failed,
76    /// Being parked.
77    Parking,
78    /// Parked: stopped, what it created kept.
79    Parked,
80    /// Being resumed.
81    Resuming,
82    /// Being removed.
83    Stopping,
84    /// Nothing of it exists.
85    Absent,
86}
87
88impl RunState {
89    /// The state as written in events (`ready`).
90    #[must_use]
91    pub fn as_str(self) -> &'static str {
92        match self {
93            RunState::Starting => "starting",
94            RunState::Ready => "ready",
95            RunState::Failed => "failed",
96            RunState::Parking => "parking",
97            RunState::Parked => "parked",
98            RunState::Resuming => "resuming",
99            RunState::Stopping => "stopping",
100            RunState::Absent => "absent",
101        }
102    }
103}
104
105impl fmt::Display for RunState {
106    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
107        f.write_str(self.as_str())
108    }
109}
110
111/// How a run is parked: the manifest's `lifecycle.park.pause` or `.shutdown` steps.
112#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
113#[serde(rename_all = "snake_case")]
114#[non_exhaustive]
115pub enum Park {
116    /// Suspended, memory kept where the target can (Docker: stopped).
117    Pause,
118    /// Shut down.
119    Shutdown,
120}
121
122impl Park {
123    /// The mode as the manifest names it.
124    #[must_use]
125    pub fn as_str(self) -> &'static str {
126        match self {
127            Park::Pause => "pause",
128            Park::Shutdown => "shutdown",
129        }
130    }
131}
132
133impl fmt::Display for Park {
134    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
135        f.write_str(self.as_str())
136    }
137}