diff --git a/CHANGELOG.md b/CHANGELOG.md index 57e281fb..a1182e15 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,14 @@ and listed with their assets on the lease as soon as the node admits it; the next daemon releases it when it starts, and so does the lane's next start, so `devstack up` runs it again. +- **A lane started on a pool node waits for a job that is still running.** + Since #544 a job shows `running` while it runs; the start on a node + counted that as done, so it could finish before a migration ended, and + a job that failed afterwards was not reported. It now waits until the + job completed, and its progress counts a completed job. A warm restart + of a lane on a node no longer stops a running job and runs it again (the + local restart already left it alone), and a change to a job's files + while it runs is kept for after its start again, as before #544. ## [0.49.3] - 2026-11-17 diff --git a/crates/devstack-daemon/src/remote.rs b/crates/devstack-daemon/src/remote.rs index 9c039e63..65024476 100644 --- a/crates/devstack-daemon/src/remote.rs +++ b/crates/devstack-daemon/src/remote.rs @@ -1373,19 +1373,7 @@ impl RemoteLane { )); } let v: serde_json::Value = serde_json::from_str(&body).map_err(|e| e.to_string())?; - let running: Vec<(String, bool)> = v["services"] - .as_array() - .cloned() - .unwrap_or_default() - .iter() - .filter(|s| matches!(s["status"]["state"].as_str(), Some("healthy" | "running"))) - .filter_map(|s| { - let name = s["name"].as_str()?.to_string(); - // A service that runs a build has its id; one without a - // `build` cannot be rebuilt, only restarted. - Some((name, s["build_id"].as_str().is_some())) - }) - .collect(); + let running = restartable(&v); let failed: Vec = std::thread::scope(|scope| { let handles: Vec<_> = running .iter() @@ -2330,6 +2318,32 @@ pub fn tunnel_error(what: &str, body: &str, node: &NodeRef) -> String { out } +/// The services a warm restart of a lane on a node restarts, from its stack +/// view (`GET /v1/stack`): those that run now, each with whether it runs a +/// build (one without a `build` cannot be rebuilt, only restarted). +fn restartable(view: &serde_json::Value) -> Vec<(String, bool)> { + view["services"] + .as_array() + .into_iter() + .flatten() + .filter(|s| matches!(s["status"]["state"].as_str(), Some("healthy" | "running"))) + // A job's run is its start, not an instance: the local restart + // leaves it alone too (it has no main). + .filter(|s| !job_running(s)) + .filter_map(|s| { + let name = s["name"].as_str()?.to_string(); + Some((name, s["build_id"].as_str().is_some())) + }) + .collect() +} + +/// A job that runs on the node (`running` until it ends, #544): not ready +/// for what comes after it until it `completed`, as the local +/// `start_and_wait` and `ensure_after` count it. +fn job_running(s: &serde_json::Value) -> bool { + s["kind"] == "job" && s["status"]["state"] == "running" +} + /// How many of `expected` are ready in a stack view. pub fn ready_count(view: &serde_json::Value, expected: &[String]) -> usize { view["services"] @@ -2340,7 +2354,12 @@ pub fn ready_count(view: &serde_json::Value, expected: &[String]) -> usize { expected .iter() .any(|e| Some(e.as_str()) == s["name"].as_str()) - && matches!(s["status"]["state"].as_str(), Some("healthy" | "running")) + && match s["status"]["state"].as_str() { + Some("healthy" | "running") => !job_running(s), + // A job that ran is ready, as `readiness` counts it. + Some("completed") => s["status"]["exit_code"] == 0, + _ => false, + } }) .count() } @@ -2355,6 +2374,7 @@ pub fn readiness(view: &serde_json::Value, expected: &[String]) -> Option pending = true, "healthy" | "running" => {} "completed" if s["status"]["exit_code"] == 0 => {} "failed" | "exited" | "completed" => { @@ -2645,6 +2665,42 @@ mod tests { ); } + /// A job on the node is `running` while it runs (#544): not ready yet, + /// so its dependants and the start wait for it, and a failure it ends + /// in is reported. Ready once it completed. A warm restart leaves it + /// alone, as the local one does (it has no main instance). + #[test] + fn a_running_job_on_a_node_is_not_ready_yet() { + let v = |job: serde_json::Value| { + serde_json::json!({ "services": [ + { "name": "api", "kind": "service", "status": { "state": "running" }, "build_id": "b1" }, + { "name": "migrate", "kind": "job", "status": job, "build_id": "b2" }, + ]}) + }; + let exp = ["api".to_string(), "migrate".to_string()]; + let running = v(serde_json::json!({ "state": "running" })); + assert_eq!( + super::readiness(&running, &exp), + None, + "a running job is pending" + ); + assert_eq!(super::ready_count(&running, &exp), 1, "only api is ready"); + assert_eq!( + super::restartable(&running), + [("api".to_string(), true)], + "a running job is not restarted" + ); + let done = v(serde_json::json!({ "state": "completed", "exit_code": 0 })); + assert_eq!(super::readiness(&done, &exp), Some(Ok(()))); + assert_eq!(super::ready_count(&done, &exp), 2); + let failed = + v(serde_json::json!({ "state": "failed", "reason": "job exited with code 1" })); + assert!( + matches!(super::readiness(&failed, &exp), Some(Err(e)) if e.contains("code 1")), + "a job that failed after it ran is reported" + ); + } + #[test] fn a_request_that_names_containers_needs_a_node_that_acts_on_them() { use axum::http::Method; diff --git a/crates/devstack-daemon/src/supervisor/candidate.rs b/crates/devstack-daemon/src/supervisor/candidate.rs index 3f66f592..72a55887 100644 --- a/crates/devstack-daemon/src/supervisor/candidate.rs +++ b/crates/devstack-daemon/src/supervisor/candidate.rs @@ -148,10 +148,12 @@ impl Supervisor { let (serving, coming, failed) = { let slots = self.slots.lock().unwrap(); let s = &slots[name]; - let coming = matches!( - s.view.status, - Status::Exited { .. } | Status::Building { .. } | Status::Starting - ); + // A job is `running` while its start waits for it to end (#544). + let coming = match s.view.status { + Status::Exited { .. } | Status::Building { .. } | Status::Starting => true, + Status::Running => s.view.kind == "job", + _ => false, + }; let failed = matches!(&s.view.status, Status::Failed { reason } if reason != STOPPED); (s.main.is_some(), coming, failed) }; @@ -661,6 +663,25 @@ mod tests { ); } + /// A job is `running` while it runs (#544), with no main instance: a + /// change to its files then waits for that start as it did while it was + /// `starting`, its paths kept for `resume_changes`, not dropped. + #[test] + fn a_change_while_a_job_runs_is_kept() { + let (_d, _rt, sup) = two_after_a_job(); + sup.set_status("seed", Status::Running); + sup.changed("seed", PATH); + let t0 = Instant::now(); + while sup.changes.lock().unwrap()["seed"].worker && t0.elapsed() < DEBOUNCE * 3 { + std::thread::sleep(Duration::from_millis(10)); + } + assert!( + !sup.changes.lock().unwrap()["seed"].worker, + "seed's pipeline ended" + ); + assert!(!taken(&sup, "seed"), "the change waits for the job's start"); + } + /// A service that failed and does not run builds no candidate on a /// change; the status says that it waits for a restart (#513), until /// it is something else than failed.