From 101f077200e80c7869e4549db645ed94b01acb1b Mon Sep 17 00:00:00 2001 From: aur-build Date: Wed, 18 Nov 2026 16:31:58 +0000 Subject: db: the build state machine is an enum, and a state write is a compare-and-swap MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `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. --- src/pipeline/mod.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/pipeline') diff --git a/src/pipeline/mod.rs b/src/pipeline/mod.rs index 279ec0f..01c9bc5 100644 --- a/src/pipeline/mod.rs +++ b/src/pipeline/mod.rs @@ -199,7 +199,7 @@ pub fn run_with(ctx: &Ctx) -> Result { std::fs::create_dir_all(&ctx.staging)?; for step in crate::db::model::BUILD_STEPS { if crate::db::builds::cancel_requested(&ctx.db, &ctx.build.id)? { - builds::set_state(&ctx.db, &ctx.build.id, build_state::CANCELLED, Some("cancelled"))?; + builds::set_state(&ctx.db, &ctx.build.id, BuildState::Cancelled, Some("cancelled"))?; return Ok(Outcome::Cancelled); } let status = builds::step_status(&ctx.db, &ctx.build.id, step)?; @@ -207,7 +207,7 @@ pub fn run_with(ctx: &Ctx) -> Result { continue; // checkpoint says we already did this } builds::begin_step(&ctx.db, &ctx.build.id, step, ctx.attempt)?; - builds::set_state(&ctx.db, &ctx.build.id, build_state::RUNNING, None)?; + builds::set_state(&ctx.db, &ctx.build.id, BuildState::Running, None)?; ctx.log_line(&format!("==> step {step} (attempt {})", ctx.attempt)); match steps::run_step(ctx, step) { Ok(StepResult::Done(digest)) => { @@ -230,7 +230,7 @@ pub fn run_with(ctx: &Ctx) -> Result { builds::set_state( &ctx.db, &ctx.build.id, - build_state::FAILED, + BuildState::Failed, Some(&format!("{step}: {e}")), )?; ctx.log_line(&format!("!! {step} failed: {e}")); @@ -248,7 +248,7 @@ pub fn run_with(ctx: &Ctx) -> Result { let release_id = ctx .release_id() .ok_or_else(|| Error::other("publish produced no release"))?; - builds::set_state(&ctx.db, &ctx.build.id, build_state::SUCCEEDED, None)?; + builds::set_state(&ctx.db, &ctx.build.id, BuildState::Succeeded, None)?; db::audit_system( &ctx.db.writer, "build.succeeded", -- cgit v1.2.3