diff --git a/CHANGELOG.md b/CHANGELOG.md index 74541260..37ed3563 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,13 @@ and listed with their assets on the start), writes it anew, and a running daemon does so with its shift before it writes its claims. An entry that is no JSON object is now said in the log, too (#570). +- **A running job is no longer counted as ready or live.** Since a job is + `running` while it runs (#544), the hub's summary counted a stack whose + migration still ran as ready on the start page; it now counts as starting + until the job completed (#575). A job's build showed as live in the build + timeline from its start, and stayed live when the job then failed; it is + `running` while the job runs, live once it completed, and `failed` with + its reason when the job failed (#571). - **The MCP server no longer promises to follow an agent into a worktree.** It acts on the tree it was started in; its instructions and the `up` and `lanes` tools said that in a worktree every tool acts on its lane, so an diff --git a/crates/devstack-daemon/src/api.rs b/crates/devstack-daemon/src/api.rs index 9f1a49cd..7673c4d7 100644 --- a/crates/devstack-daemon/src/api.rs +++ b/crates/devstack-daemon/src/api.rs @@ -1598,6 +1598,7 @@ async fn builds( } let view = s.sup.view().services.into_iter().find(|v| v.name == name); let current = view.as_ref().and_then(|v| v.build_id.clone()); + let job = view.as_ref().is_some_and(|v| v.kind == "job"); // The build on its way now, if any: the others the log leaves on their // way were interrupted. let on_way = view @@ -1624,6 +1625,7 @@ async fn builds( current.as_deref(), on_way.as_deref(), limit, + job, ); // What of each is still there: older builds outlive their artifact // and their log in the list. diff --git a/crates/devstack-daemon/src/timeline.rs b/crates/devstack-daemon/src/timeline.rs index 3877b411..614c3421 100644 --- a/crates/devstack-daemon/src/timeline.rs +++ b/crates/devstack-daemon/src/timeline.rs @@ -36,7 +36,9 @@ fn at_ms(v: &Value) -> Option { /// The builds of `service` in the diagnostic log lines, newest first, at most /// `limit`. `on_way` is the build the service's candidate has now while it -/// builds, starts or switches: only that one is still on its way. +/// builds, starts or switches: only that one is still on its way. `job`: the +/// service is a job, whose build is live once it completed (it is `running` +/// while it runs, #544, #571). pub fn builds( lines: &[String], service: &str, @@ -44,6 +46,7 @@ pub fn builds( current: Option<&str>, on_way: Option<&str>, limit: usize, + job: bool, ) -> Vec { let mut out: Vec = Vec::new(); // A build starts before its id is known (`building` without one). @@ -121,24 +124,35 @@ pub fn builds( if state == "building" && v["status"]["build_id"].is_null() { pending_start.get_or_insert(at); } - // The main instance runs a build: it is live. - if let Some(id) = v["build_id"] - .as_str() - .filter(|_| state == "healthy" || state == "running") - { + // The main instance runs a build: it is live. A job's build + // runs, then is live once the job completed, or failed. + let outcome = match (job, state) { + (false, "healthy" | "running") => Some("live"), + (true, "running") => Some("running"), + (true, "completed") if v["status"]["exit_code"] == 0 => Some("live"), + (true, "failed" | "completed") => Some("failed"), + _ => None, + }; + if let (Some(id), Some(outcome)) = (v["build_id"].as_str(), outcome) { + let reason = (outcome == "failed") + .then(|| v["status"]["reason"].as_str().map(str::to_string)) + .flatten(); match out.iter_mut().find(|e| e.build == id) { Some(e) => { - if e.outcome != "live" { - e.outcome = "live".into(); + if e.outcome != outcome { + e.outcome = outcome.into(); e.updated_ms = at; } + if reason.is_some() { + e.reason = reason; + } } None => out.push(BuildEntry { build: id.into(), started_ms: pending_start.take().unwrap_or(at), updated_ms: at, - outcome: "live".into(), - reason: None, + outcome: outcome.into(), + reason, current: false, artifact: false, log: false, @@ -158,6 +172,10 @@ pub fn builds( { e.outcome = "interrupted".into(); } + // A job's run that is not the one running now. + if e.outcome == "running" && !e.current { + e.outcome = "interrupted".into(); + } } out.reverse(); out.truncate(limit); @@ -172,10 +190,11 @@ pub fn from_log( current: Option<&str>, on_way: Option<&str>, limit: usize, + job: bool, ) -> Vec { let needle = format!("\"service\":\"{service}\""); let lines = crate::diag::tail(path, 50_000, Some(&needle)); - builds(&lines, service, lane, current, on_way, limit) + builds(&lines, service, lane, current, on_way, limit, job) } #[cfg(test)] @@ -229,9 +248,9 @@ mod tests { r#""event":"service","service":"web","build_id":"zzz","status":{"state":"healthy"}"#, ), ]; - let b = builds(&lines, "api", "", Some("bbb"), None, 10); + let b = builds(&lines, "api", "", Some("bbb"), None, 10, false); assert!( - builds(&lines, "api", "feat", None, None, 10).is_empty(), + builds(&lines, "api", "feat", None, None, 10, false).is_empty(), "another lane stays out" ); let got: Vec<(&str, &str, bool)> = b @@ -254,7 +273,7 @@ mod tests { "from the build start to the switch" ); assert_eq!(b[0].reason.as_deref(), Some("exited with code 3")); - assert_eq!(builds(&lines, "api", "", None, None, 2).len(), 2); + assert_eq!(builds(&lines, "api", "", None, None, 2, false).len(), 2); } #[test] @@ -276,13 +295,83 @@ mod tests { ), ]; for lane in ["", "main"] { - let ids: Vec = builds(&lines, "api", lane, None, None, 10) + let ids: Vec = builds(&lines, "api", lane, None, None, 10, false) .into_iter() .map(|e| e.build) .collect(); assert_eq!(ids, ["bbb", "aaa"], "lane {lane:?}"); } - assert_eq!(builds(&lines, "api", "feat", None, None, 10).len(), 1); + assert_eq!( + builds(&lines, "api", "feat", None, None, 10, false).len(), + 1 + ); + } + + /// #571: a job is `running` while it runs (#544); its build is live once + /// it completed, `running` while it runs, and `failed` when it failed + /// after. A rebuild of a job that does not run goes through the + /// candidate, which is gone before the job starts (code review before + /// 0.51.0, finding 2). + #[test] + fn a_jobs_build_is_live_once_the_job_completed() { + let svc = |t, id: &str, status: &str| { + ev( + t, + &format!( + "\"event\":\"service\",\"service\":\"migrate\",\"build_id\":\"{id}\",\"status\":{status}" + ), + ) + }; + let cand = |t, c: &str| { + ev( + t, + &format!("\"event\":\"candidate\",\"service\":\"migrate\",\"candidate\":{c}"), + ) + }; + let lines = vec![ + // up: the first build, run once + ev( + 0, + r#""event":"service","service":"migrate","build_id":null,"status":{"state":"building","build_id":null}"#, + ), + svc(1, "aaa", r#"{"state":"running"}"#), + svc(2, "aaa", r#"{"state":"completed","exit_code":0}"#), + // a change: built as a candidate, then run, and it fails + cand(3, r#"{"state":"building","build_id":null}"#), + cand(4, r#"{"state":"building","build_id":"bbb"}"#), + cand(5, "null"), + svc(6, "bbb", r#"{"state":"running"}"#), + svc( + 7, + "bbb", + r#"{"state":"failed","reason":"job exited with code 1"}"#, + ), + // another change, and the job runs now + cand(8, r#"{"state":"building","build_id":"ccc"}"#), + cand(9, "null"), + svc(10, "ccc", r#"{"state":"running"}"#), + ]; + let got = |current| -> Vec<(String, String)> { + builds(&lines, "migrate", "", current, None, 10, true) + .into_iter() + .map(|e| (e.build, e.outcome)) + .collect() + }; + let s = |b: &str, o: &str| (b.to_string(), o.to_string()); + assert_eq!( + got(Some("ccc")), + [s("ccc", "running"), s("bbb", "failed"), s("aaa", "live")] + ); + let b = builds(&lines, "migrate", "", Some("ccc"), None, 10, true); + assert_eq!(b[1].reason.as_deref(), Some("job exited with code 1")); + // The daemon stopped while it ran: the log says no more. + assert_eq!(got(None)[0], s("ccc", "interrupted")); + // A long-running service's `running` is live, as before. + let b = builds(&lines, "migrate", "", None, None, 10, false); + assert_eq!( + (b[0].build.as_str(), b[0].outcome.as_str()), + ("ccc", "live") + ); } #[test] @@ -305,7 +394,7 @@ mod tests { c(3, "building", "bbb"), ]; let outcomes = |on_way| { - builds(&lines, "api", "", Some("zzz"), on_way, 10) + builds(&lines, "api", "", Some("zzz"), on_way, 10, false) .into_iter() .map(|e| (e.build, e.outcome)) .collect::>() diff --git a/crates/devstack-hub/src/notify/rules.rs b/crates/devstack-hub/src/notify/rules.rs index 2a2a9027..9373b35f 100644 --- a/crates/devstack-hub/src/notify/rules.rs +++ b/crates/devstack-hub/src/notify/rules.rs @@ -89,6 +89,8 @@ const CASCADE_PREFIX: &str = "not started: "; #[derive(Debug, Default, Clone)] struct Svc { + /// `kind`, from the snapshot: a service event carries none. + kind: String, /// `status.state`. state: String, /// `status.reason` and `status.exit_code`, for what the tray shows. @@ -120,6 +122,7 @@ impl Scope { Some(( s["name"].as_str()?.to_string(), Svc { + kind: s["kind"].as_str().unwrap_or("").to_string(), state: state_of(&s["status"]), reason: reason_of(&s["status"]), exit_code: s["status"]["exit_code"].as_i64(), @@ -174,6 +177,8 @@ impl Scope { for name in names { let svc = &self.services[name]; match svc.state.as_str() { + // A job runs until it ends (#544): that is its start (#575). + "running" if svc.kind == "job" => g.starting = true, "running" | "healthy" => g.ready += 1, "completed" if svc.exit_code == Some(0) => g.ready += 1, "building" | "starting" => g.starting = true, @@ -521,6 +526,27 @@ mod tests { assert_eq!(g.attention[1].level, Level::Error); } + /// #575: a job is `running` while it runs (#544), which is its start, + /// not a service that is ready; a long-running service's `running` is. + #[test] + fn a_running_job_counts_as_starting_not_ready() { + let mut s = Scope::default(); + s.apply(&snapshot(json!([ + { "name": "api", "kind": "long-running", "status": { "state": "running" } }, + { "name": "migrate", "kind": "job", "status": { "state": "starting" } } + ]))); + // The service event carries no kind: the snapshot's stays. + s.apply(&service("migrate", json!({ "state": "running" }))); + let g = s.glance(); + assert_eq!((g.ready, g.total, g.starting), (1, 2, true)); + s.apply(&service( + "migrate", + json!({ "state": "completed", "exit_code": 0 }), + )); + let g = s.glance(); + assert_eq!((g.ready, g.starting), (2, false)); + } + #[test] fn what_the_snapshot_shows_is_the_baseline() { let mut s = Scope::default(); diff --git a/docs/dashboard-api.md b/docs/dashboard-api.md index 258b2976..2e4c81dc 100644 --- a/docs/dashboard-api.md +++ b/docs/dashboard-api.md @@ -488,7 +488,7 @@ type StoreView = { | `POST /v1/lanes/{lane}/main` | that lane serves the main ports (`main`: the main tree again), workers move with them. A lane on a node only on a dedicated one (ADR-0025 point 4): the fixed ports forward to its ports here, its workers run on the node, and the main tree takes the ports back by itself when the lane is gone (its lease over, its node silent for longer than a lease lives). The main ports are the fixed ports of `[services.*]`, not a Compose container's published ones: `409` for a stack with no service on a port and none with workers, which would move nothing (#184) | | `POST /v1/main-slot {"owns"}` | only the daemon in a lane container on a node: it holds its requester's main slot, or no longer; its worker services restart with the switch. The requester says it again with each renewal. Answers `{"owns"}` | | `/v1/lanes/{lane}/v1/...` | the lane's own API, every route of this table; `StackView` then carries `lane`, the main one `main_served_by` while a lane serves its ports. `/v1/lanes/main/v1/...` is the main working tree's own `/v1/...` (since 0.33.0, [ADR-0080](adr/0080-the-main-tree-answers-as-a-lane-shared-is-deprecated.md)), so a client can address every tree the same way; no lane is ever named `main` | -| `GET /v1/services/{name}/builds?limit=50` | JSON, the builds of the service newest first, read from the diagnostic log (so across daemon restarts): `[{build, started_ms, updated_ms, outcome, reason, current, artifact, log}]` (`artifact`: the store still has it, so it can be gone back to; `log`: its build log is still kept; older builds outlive both in the list); `outcome` is `live`, `failed`, `superseded` (a newer change replaced it before it went live), `held`, `interrupted` (it was building, starting or switching and is no longer: the daemon stopped, or a stop or restart came between), or the state it is in now (only the candidate's build) | +| `GET /v1/services/{name}/builds?limit=50` | JSON, the builds of the service newest first, read from the diagnostic log (so across daemon restarts): `[{build, started_ms, updated_ms, outcome, reason, current, artifact, log}]` (`artifact`: the store still has it, so it can be gone back to; `log`: its build log is still kept; older builds outlive both in the list); `outcome` is `live`, `failed`, `superseded` (a newer change replaced it before it went live), `held`, `interrupted` (it was building, starting or switching and is no longer: the daemon stopped, or a stop or restart came between), `running` (a job's build while the job runs; live once it completed, `failed` with its reason when it failed), or the state it is in now (only the candidate's build) | | `GET /v1/services/{name}/logs/files` | JSON, the kept logs newest first: `[{file, kind: "run"\|"build", started_ms, modified_ms, build, port, bytes, current}]` | | `GET /v1/logs?grep=…&tail=50` | text, matching lines of every service's current log as `service \| line` (`grep` is required, `400` without it; `tail` counts per service); with `since=` every instance written to within that time | | `GET /v1/services/{name}/logs/stream?tail=200` | SSE: `{"type":"line","text":"..."}` for the last lines (`tail` at most 5000), then every new one; `{"type":"instance","build_id":...}` when another instance takes over (the lines that follow are the new one's). With `build=` it follows the newest instance of that build instead (a candidate's console), from before it exists to after it takes over |