diff options
| author | aur-build <aur-build@localhost> | 2026-11-18 16:31:58 +0000 |
|---|---|---|
| committer | aur-build <aur-build@localhost> | 2026-11-18 16:31:58 +0000 |
| commit | 101f077200e80c7869e4549db645ed94b01acb1b (patch) | |
| tree | f8560d143ef2afe1a3d675a203c693a09cbc451b /src/db/model.rs | |
| parent | f8370b48f3e4464a1454d0ff7af1819b5448c619 (diff) | |
| download | aur-build-101f077200e80c7869e4549db645ed94b01acb1b.tar.gz aur-build-101f077200e80c7869e4549db645ed94b01acb1b.zip | |
db: the build state machine is an enum, and a state write is a compare-and-swap
`build_state` was seven `&str` constants and `set_state(&str)` would write any
string a caller invented, so the machine existed only as `WHERE state IN (...)`
clauses scattered across six modules and nothing could say that
`succeeded → running` is nonsense. It is `db::model::BuildState` now: parsed
where a row is read (a state the code cannot name is an error, not an empty
string — which also retires m-12's `current_state` returning `""`), and
`set_state` takes the variant and writes only if the row's current state is one
of `to`'s legal predecessors. A refused transition writes nothing and says why.
Writing the machine down surfaced three things that were true of the flows but
of nobody's documentation. The step loop re-asserts `Running` at every step
boundary, so a self-write is a no-op and not a move — every state accepts its
own name. A failed build is resumed *on its own row* (I5: the step checkpoints
say where to continue), so `failed → running` is a real transition. And
`builds::retry` — the UI's requeue button — moved any row to `queued`
unconditionally, including one that had already published, re-queueing a build
whose every step was checkpointed as done; it goes through the machine now and
is refused with a reason. `requeue_transient` likewise.
What the CAS buys is the zombie case, which no test covered: a worker's lease
expires mid-step, the reaper returns the build to the queue, and the original
worker — still running — comes back and writes `succeeded` over whatever the new
owner has since done. That write now fails loudly and the queue keeps ownership.
`builds_for_pkgbase` had a second, hand-copied row mapper that dropped unreadable
rows with `filter_map(|r| r.ok())`; it shares `row_to_build` now and a bad row
fails the query instead of vanishing from the package page (m-9's policy,
applied where it bit).
`tests/state_machine.rs` pins the whole table, the zombie refusal, the unknown
state, the terminal build that does not move, and the requeue-of-a-published-
build refusal. `tests/live.rs` walks a build through `running` before succeeding
instead of teleporting it.
Diffstat (limited to 'src/db/model.rs')
| -rw-r--r-- | src/db/model.rs | 132 |
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 { |
