summaryrefslogtreecommitdiffstats
path: root/src/db/model.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/db/model.rs')
-rw-r--r--src/db/model.rs132
1 files changed, 124 insertions, 8 deletions
diff --git a/src/db/model.rs b/src/db/model.rs
index dfc1f8b..bf3e017 100644
--- a/src/db/model.rs
+++ b/src/db/model.rs
@@ -112,14 +112,130 @@ pub mod release_state {
pub const FAILED_UNBUILT: &str = "failed_unbuilt";
}
-pub mod build_state {
- pub const QUEUED: &str = "queued";
- pub const LEASED: &str = "leased";
- pub const RUNNING: &str = "running";
- pub const BLOCKED_MISSING_DEP: &str = "blocked_missing_dep";
- pub const SUCCEEDED: &str = "succeeded";
- pub const FAILED: &str = "failed";
- pub const CANCELLED: &str = "cancelled";
+/// The build state machine (§7.1, §8.2).
+///
+/// This used to be seven `&str` constants and a `set_state(&str)` that wrote any
+/// string a caller invented, so the machine existed only as `WHERE state IN
+/// (...)` clauses scattered across six modules — nothing could tell you that
+/// `succeeded → running` is nonsense. The enum is the machine: a state is one
+/// of these seven, it is parsed where a row is read, and `can_transition` is
+/// the only way a write happens.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
+pub enum BuildState {
+ /// Approved and waiting for a worker.
+ Queued,
+ /// Claimed by a worker with a lease (§8.2).
+ Leased,
+ /// A step is executing.
+ Running,
+ /// Waiting for packages another build must publish first (§10.4).
+ BlockedMissingDep,
+ Succeeded,
+ Failed,
+ Cancelled,
+}
+
+impl BuildState {
+ pub const ALL: &'static [BuildState] = &[
+ BuildState::Queued,
+ BuildState::Leased,
+ BuildState::Running,
+ BuildState::BlockedMissingDep,
+ BuildState::Succeeded,
+ BuildState::Failed,
+ BuildState::Cancelled,
+ ];
+
+ /// The text in the `state` column. The spelling is part of the schema: the
+ /// reaper, the dispatcher and the UI all speak it.
+ pub fn as_str(&self) -> &'static str {
+ match self {
+ BuildState::Queued => "queued",
+ BuildState::Leased => "leased",
+ BuildState::Running => "running",
+ BuildState::BlockedMissingDep => "blocked_missing_dep",
+ BuildState::Succeeded => "succeeded",
+ BuildState::Failed => "failed",
+ BuildState::Cancelled => "cancelled",
+ }
+ }
+
+ /// The only way in from the database text. An unrecognised state is `None`,
+ /// and the row boundary turns that into an error rather than a silent
+ /// fallback: a state we cannot name is a state we must not act on.
+ pub fn parse(s: &str) -> Option<BuildState> {
+ BuildState::ALL.iter().copied().find(|b| b.as_str() == s)
+ }
+
+ /// Nothing happens to a finished build by itself. `Succeeded` is final —
+ /// the release is published and the row is history (§15). `Failed` and
+ /// `Cancelled` end a worker's involvement, and only an explicit human
+ /// requeue or resume moves them again ([`BuildState::can_transition`]).
+ pub fn is_terminal(&self) -> bool {
+ matches!(
+ self,
+ BuildState::Succeeded | BuildState::Failed | BuildState::Cancelled
+ )
+ }
+
+ /// May a row in this state be written to `to`?
+ ///
+ /// Writing the state a row already has is always allowed: it is a no-op, not
+ /// a move. The step loop re-asserts `Running` at every step boundary (§8.3),
+ /// and re-running a finished build must stay harmless rather than turn into
+ /// an error.
+ ///
+ /// Everything else is refused by [`crate::db::builds::set_state`], and the
+ /// refusals are the point: nothing revives a `Succeeded` build, and a build
+ /// reaped back to `Queued` cannot be completed by the zombie worker that
+ /// lost its lease — that refusal is the alarm.
+ pub fn can_transition(&self, to: BuildState) -> bool {
+ use BuildState::*;
+ if *self == to {
+ return true;
+ }
+ match self {
+ // The dispatcher claims queued builds; `aur-build run <id>` runs one
+ // in the foreground without a lease, so `Running` is legal from here
+ // too. A dependency can be discovered before a worker ever starts.
+ Queued => matches!(to, Leased | Running | BlockedMissingDep | Cancelled),
+ Leased => matches!(to, Running | Queued | BlockedMissingDep | Succeeded | Failed | Cancelled),
+ Running => matches!(to, Queued | BlockedMissingDep | Succeeded | Failed | Cancelled),
+ BlockedMissingDep => matches!(to, Queued | Cancelled),
+ // A failed build is resumed on the same row — that is I5 (the step
+ // checkpoints say where to continue) and §8.4's human requeue.
+ Failed => matches!(to, Queued | Running),
+ // §8.4: requeue is a human decision, not a policy.
+ Cancelled => to == Queued,
+ Succeeded => false,
+ }
+ }
+
+ /// Every state that may legally move to `to` — the `WHERE state IN (...)`
+ /// that makes a state write a compare-and-swap.
+ pub fn predecessors(to: BuildState) -> Vec<&'static str> {
+ BuildState::ALL
+ .iter()
+ .filter(|from| from.can_transition(to))
+ .map(|from| from.as_str())
+ .collect()
+ }
+
+ /// The states the queue considers "in flight" for metrics and the index.
+ pub fn active() -> &'static [BuildState] {
+ &[
+ BuildState::Queued,
+ BuildState::Running,
+ BuildState::Leased,
+ BuildState::BlockedMissingDep,
+ ]
+ }
+}
+
+impl std::fmt::Display for BuildState {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ f.write_str(self.as_str())
+ }
}
pub mod step_state {