diff --git a/crates/engine/src/cmd/previews.rs b/crates/engine/src/cmd/previews.rs index 6eba9f477f..1c73fd22c9 100644 --- a/crates/engine/src/cmd/previews.rs +++ b/crates/engine/src/cmd/previews.rs @@ -265,6 +265,11 @@ fn request_for(command: &str, p: &Value, ids: &[lightcraft_catalog::PhotoId]) -> /// The refusal of a build while another runs, unless the call asks to `queue`. const REFUSED: &str = "a preview build is already running"; +/// Builds wait in line ahead of one asked for now (not counting the line a build is being started from). +fn line_ahead(s: &Session) -> bool { + !s.starting_waiting && s.activity.lane_waiting(Lane::Previews) +} + fn queue_param(p: &Value) -> bool { p.get("queue").and_then(Value::as_bool).unwrap_or(false) } @@ -585,7 +590,9 @@ fn build(s: &mut Session, p: &Value) -> Result { busy = true; } } - if !queue && busy { + // (nor ahead of builds already waiting: the line starts them after this command, in the order asked; the build + // `start_waiting` is starting itself is not held back by the line it came out of) + if !queue && (busy || line_ahead(s)) { return Err(bad(C, REFUSED)); } s.fit_disk_cache(); @@ -833,7 +840,7 @@ fn smart(s: &mut Session, p: &Value) -> Result { busy = true; } } - if background && !queue && busy { + if background && !queue && (busy || line_ahead(s)) { return Err(bad(C, REFUSED)); } let what = if discard { "discarding smart previews" } else { "smart previews" }; diff --git a/crates/engine/src/tests/task_queue.rs b/crates/engine/src/tests/task_queue.rs index 1eade86344..939c2d3042 100644 --- a/crates/engine/src/tests/task_queue.rs +++ b/crates/engine/src/tests/task_queue.rs @@ -247,3 +247,72 @@ fn another_library_drops_what_waits() { s.close_library().unwrap(); let _ = std::fs::remove_dir_all(&dir); } + +/// Given a build that ended and a build waiting behind it, when the next command starts the waiting one before the +/// app announced the first, then the first is kept (once, with its counts) for the frontend to announce, and the +/// new build is the current one. +#[test] +fn a_build_replaced_by_the_one_waiting_behind_it_is_kept_until_it_is_announced() { + let mut s = Session::with_demo(); + let ids = build_ids(&s, 1); + let h = hold(&mut s); + let first = h.build.clone(); + first.done.store(2, Ordering::Relaxed); + first.failed.store(1, Ordering::Relaxed); + s.execute("library.buildPreviews", &json!({"ids": ids, "edge": 256, "queue": true})).unwrap(); + release(h); + tasks(&mut s); // the command that ends up starting the waiting build + assert!(s.preview_build.as_ref().is_some_and(|b| !Arc::ptr_eq(b, &first)), "the waiting build is the current one"); + let kept = s.take_unreported_builds(); + assert_eq!(kept.len(), 1); + assert!(Arc::ptr_eq(&kept[0], &first)); + assert_eq!((kept[0].done.load(Ordering::Relaxed), kept[0].failed.load(Ordering::Relaxed)), (2, 1)); + assert!(s.take_unreported_builds().is_empty(), "once"); + settle(&mut s); +} + +/// A build the frontend announced already is not kept when the next one replaces it. +#[test] +fn a_build_that_was_announced_is_not_kept_when_the_next_one_replaces_it() { + let mut s = Session::with_demo(); + let ids = build_ids(&s, 1); + let h = hold(&mut s); + h.build.reported.store(true, Ordering::Relaxed); + s.execute("library.buildPreviews", &json!({"ids": ids, "edge": 256, "queue": true})).unwrap(); + release(h); + tasks(&mut s); + assert!(s.take_unreported_builds().is_empty()); + settle(&mut s); +} + +/// Given a build that ended and a build waiting in line behind it that has not been started yet, when Build Previews is +/// asked for without `queue`, then it is refused, as it is while a build runs: it does not start ahead of the one +/// waiting, which the line starts after this command. +#[test] +fn a_build_asked_for_without_queue_does_not_go_ahead_of_one_waiting_in_line() { + let mut s = Session::with_demo(); + let ids = build_ids(&s, 1); + let running = hold(&mut s); + s.execute("library.buildPreviews", &json!({"ids": ids, "edge": 256, "queue": true})).unwrap()["queued"].as_u64().expect("it waits"); + // the build ends, and nothing has started the one waiting yet (no command or frame since) + release(running); + let refused = s.execute("library.buildPreviews", &json!({"ids": ids, "edge": 256})).unwrap_err().to_string(); + assert!(refused.contains("preview build"), "{refused}"); + settle(&mut s); +} + +/// Given a build running and two builds waiting behind it, when the build ends, then both start in turn, each one when +/// the lane is free (the line is not "ahead" of the build it is starting). +#[test] +fn builds_waiting_in_line_all_start_in_turn() { + let mut s = Session::with_demo(); + let ids = build_ids(&s, 1); + let running = hold(&mut s); + for _ in 0..2 { + s.execute("library.buildPreviews", &json!({"ids": ids, "edge": 256, "queue": true})).unwrap()["queued"].as_u64().expect("it waits"); + } + release(running); + settle(&mut s); + // the held build, and the first of the two, were replaced by the next: three builds ran in all + assert_eq!(s.take_unreported_builds().len(), 2, "both waiting builds started"); +} diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index 6db2c0c04c..0cc84ddcaa 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -405,7 +405,7 @@ impl LightcraftApp { /// A task waiting in line whose lane freed while nothing was asked starts now (commands start them too); then the /// run in progress is looked at, so that one started here is polled for its end from this frame on. - fn tick_preview_builds(&mut self, ctx: &egui::Context) { + pub(crate) fn tick_preview_builds(&mut self, ctx: &egui::Context) { self.session.start_waiting(); self.preview_build_status(ctx); }