Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
84 changes: 70 additions & 14 deletions crates/devstack-daemon/src/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = std::thread::scope(|scope| {
let handles: Vec<_> = running
.iter()
Expand Down Expand Up @@ -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"]
Expand All @@ -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()
}
Expand All @@ -2355,6 +2374,7 @@ pub fn readiness(view: &serde_json::Value, expected: &[String]) -> Option<Result
continue;
}
match s["status"]["state"].as_str().unwrap_or("") {
"running" if job_running(s) => pending = true,
"healthy" | "running" => {}
"completed" if s["status"]["exit_code"] == 0 => {}
"failed" | "exited" | "completed" => {
Expand Down Expand Up @@ -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;
Expand Down
29 changes: 25 additions & 4 deletions crates/devstack-daemon/src/supervisor/candidate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
};
Expand Down Expand Up @@ -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.
Expand Down
Loading