Skip to content
Open
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
316 changes: 305 additions & 11 deletions crates/engine/src/activity.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions crates/engine/src/cmd/activity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Background Tasks",
[],
None,
"{} → {tasks: [{id, kind, label, done, total, unit: count|bytes|percent, detail, cancellable, cancelling, ageMs}]} — the long-running tasks in flight (imports, exports, preview builds, downloads, the face scan…), oldest first; total 0 = not known yet",
"{} → {tasks: [{id, kind, label, done, total, unit: count|bytes|percent, detail, cancellable, cancelling, ageMs, waiting, place, lane}]} — the long-running tasks in flight (imports, exports, preview builds, downloads, the face scan…), oldest first, then those waiting in line for their lane (waiting: true, place 1 = next in its lane, lane: previews or library (imports and Synchronize Folder, which the app starts); they start by themselves when the tasks ahead end, keeping their id), in the order they were asked for; total 0 = not known yet",
always,
|s, _| Ok(json!({"tasks": s.activity.list()}))
),
Expand All @@ -20,7 +20,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Cancel Background Task",
[],
None,
"{id} | {all: true} → {cancelled: n} — ask a task (or every cancellable task) to stop; it stops at its next file or photo (a preview build also stops between a photo's own previews). An unknown id (it may have just finished) or a task that can't be cancelled is an error",
"{id} | {all: true} → {cancelled: n} — ask a task (or every cancellable task) to stop; it stops at its next file or photo (a preview build also stops between a photo's own previews). A task waiting in line is taken out of it and never runs; all also empties the line. An unknown id (it may have just finished) or a task that can't be cancelled is an error",
always,
|s, p| {
const C: &str = "activity.cancel";
Expand Down
170 changes: 147 additions & 23 deletions crates/engine/src/cmd/previews.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use serde_json::{Value, json};

use super::{CommandSpec, always, bad, cmd, str_param};
use crate::activity::{Cancel, TaskGuard};
use crate::activity::{Cancel, Lane, Request, TaskGuard};
use crate::media::{RenderJob, THUMB_SIZES};
use crate::{Result, Session};

Expand All @@ -25,6 +25,9 @@ pub struct PreviewBuild {
/// Shared with the build's row in the activity stack (its ✕ sets it).
pub cancel: Arc<AtomicBool>,
pub finished: AtomicBool,
/// A frontend announced how it ended (the app's toast); one that ended and was not announced is kept when the next
/// build replaces it ([`Session::take_unreported_builds`]).
pub reported: AtomicBool,
/// Damaged smart previews rebuilt (Build Smart Previews only).
pub repaired: AtomicUsize,
/// What is built ("" = previews; "smart previews" — Build Smart Previews runs the same way).
Expand Down Expand Up @@ -69,18 +72,97 @@ pub(crate) fn jobs_for(s: &mut Session, id: lightcraft_catalog::PhotoId, edge: O
if e >= thumb { view.into_iter().chain(grid).collect() } else { grid.into_iter().chain(view).collect() }
}

/// The build's row in the activity stack (`what` as in [`PreviewBuild::what`]).
fn task_for(s: &Session, state: &PreviewBuild) -> TaskGuard {
let (kind, label) = match state.what {
/// The row in the activity stack of a build of `what` (as in [`PreviewBuild::what`]): its kind and its label.
fn row_of(what: &str) -> (&'static str, &'static str) {
match what {
"" => ("previews", "Building previews"),
"smart previews" => ("smartPreviews", "Building smart previews"),
_ => ("smartPreviews", "Discarding smart previews"),
};
}
}

/// The build's row in the activity stack.
fn task_for(s: &Session, state: &PreviewBuild) -> TaskGuard {
let (kind, label) = row_of(state.what);
let task = s.activity.start(kind, label, Cancel::Flag(state.cancel.clone()));
task.progress(0, state.total as u64);
task
}

/// A build's state and its row, which go together. However the build ends (it finishes, its worker panics, or the
/// worker can't be started) the state reads as finished first, and then the row goes: a build waiting in line starts
/// when the row is gone, and is refused if the state still said a build runs. A build that did not mark itself
/// finished did not end well: it says so, instead of reading as one that built nothing.
struct Running {
state: Arc<PreviewBuild>,
task: TaskGuard,
}

impl Drop for Running {
fn drop(&mut self) {
if !self.state.finished.load(Ordering::Relaxed) {
self.state
.error
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.get_or_insert_with(|| "the build stopped unexpectedly".to_string());
}
self.state.finished.store(true, Ordering::Relaxed);
}
}

/// Make `state` the session's current build. A build that ended and was not announced yet ([`PreviewBuild::reported`])
/// is kept for the frontend to announce ([`Session::take_unreported_builds`]), so that starting the next one right
/// after it (as a build waiting in line does) does not lose its result, its failed count or its error.
fn set_current(s: &mut Session, state: Arc<PreviewBuild>) {
if let Some(old) = s.preview_build.replace(state)
&& old.finished.load(Ordering::Relaxed)
&& !old.reported.load(Ordering::Relaxed)
{
s.keep_unreported_build(old);
}
}

/// What a build of `ids` was asked, as it runs later (from the line): the photos written out (the selection or the
/// view may have changed by then), in the background (nothing waits for it then), and not queued again.
fn request_for(command: &str, p: &Value, ids: &[lightcraft_catalog::PhotoId]) -> Request {
let mut params = p.as_object().cloned().unwrap_or_default();
for key in ["id", "queue", "wait"] {
params.remove(key);
}
params.insert("ids".into(), ids.iter().map(|id| id.0).collect());
Request { command: command.into(), params: Value::Object(params) }
}

/// 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)
}

fn build_running(s: &Session) -> bool {
s.preview_build.as_ref().is_some_and(|b| !b.finished.load(Ordering::Relaxed))
}

/// A build asked for with `queue`: while one runs (or others already wait) this one waits in line behind them →
/// `{queued: id, place}`. `None`: nothing is ahead of it, so it starts now.
fn wait_in_line(s: &Session, c: &str, p: &Value, what: &str, request: &Request) -> Option<Result<Value>> {
if !build_running(s) && !s.activity.lane_waiting(Lane::Previews) {
return None;
}
if p.get("wait").and_then(Value::as_bool) == Some(true) {
return Some(Err(bad(c, "a build that waits in line can't also be waited for: leave out wait")));
}
let (kind, label) = row_of(what);
Some(s.activity.enqueue(kind, label, request.clone()).map(|(id, place)| json!({"queued": id, "place": place})).map_err(|e| bad(c, e)))
}

/// Render one photo's jobs in order. The source the first job decodes serves the ones after it
/// (the grid thumbnail is made from the view's source instead of decoding the file again).
/// `None` when `cancel` was asked for before a job started: that photo is not counted as done.
Expand Down Expand Up @@ -282,31 +364,36 @@ fn build(s: &mut Session, p: &Value) -> Result<Value> {
"full" | "1:1" => None,
other => return Err(bad(C, format!("unknown size `{other}` (standard|full)"))),
};
if s.preview_build.as_ref().is_some_and(|b| !b.finished.load(Ordering::Relaxed)) {
return Err(bad(C, "a preview build is already running"));
let queue = queue_param(p);
// (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 && (build_running(s) || line_ahead(s)) {
return Err(bad(C, REFUSED));
}
// explicit ids, else the selection, else everything in view
let ids = if p.get("ids").is_some() || p.get("id").is_some() || !s.selection.ids.is_empty() { s.targets(p)? } else { s.visible_cloned() };
if queue && let Some(queued) = wait_in_line(s, C, p, "", &request_for(C, p, &ids)) {
return queued;
}
let jobs: Vec<Vec<RenderJob>> = ids.iter().map(|id| jobs_for(s, *id, edge)).filter(|j| !j.is_empty()).collect();
#[cfg(test)]
let state = Arc::new(PreviewBuild { total: jobs.len(), gate, ..Default::default() });
#[cfg(not(test))]
let state = Arc::new(PreviewBuild { total: jobs.len(), ..Default::default() });
s.preview_build = Some(state.clone());
let task = task_for(s, &state);
set_current(s, state.clone());
let run = Running { task: task_for(s, &state), state: state.clone() };
let lanes = lanes(jobs.len(), edge.is_none());
let wait = p.get("wait").and_then(Value::as_bool).unwrap_or(false) || cfg!(target_arch = "wasm32");
if wait {
run_all(jobs, &state, &task, lanes);
run_all(jobs, &run.state, &run.task, lanes);
return Ok(state.json());
}
#[cfg(not(target_arch = "wasm32"))]
{
// the row stays while the worker runs (it is dropped with the closure, however that ends)
let st = state.clone();
std::thread::Builder::new()
.name("lc-build-previews".into())
.spawn(move || run_all(jobs, &st, &task, lanes))
.spawn(move || run_all(jobs, &run.state, &run.task, lanes))
.map_err(|e| bad(C, format!("could not start: {e}")))?;
}
Ok(state.json())
Expand Down Expand Up @@ -471,10 +558,15 @@ fn smart(s: &mut Session, p: &Value) -> Result<Value> {
let dir = s.media.smart_dir.clone().ok_or_else(|| bad(C, "smart previews need a library on disk"))?;
let discard = p.get("discard").and_then(Value::as_bool).unwrap_or(false);
let background = p.get("background").and_then(Value::as_bool).unwrap_or(false);
if background && s.preview_build.as_ref().is_some_and(|b| !b.finished.load(Ordering::Relaxed)) {
return Err(bad(C, "a preview build is already running"));
let queue = background && queue_param(p);
if background && !queue && (build_running(s) || line_ahead(s)) {
return Err(bad(C, REFUSED));
}
let ids = if p.get("ids").is_some() || !s.selection.ids.is_empty() { s.targets(p)? } else { s.visible_cloned() };
let what = if discard { "discarding smart previews" } else { "smart previews" };
if queue && let Some(queued) = wait_in_line(s, C, p, what, &request_for(C, p, &ids)) {
return queued;
}
let mut jobs: Vec<SmartJob> = Vec::new();
for id in ids {
let Some(ph) = s.catalog.photo(id).cloned() else { continue };
Expand All @@ -487,19 +579,17 @@ fn smart(s: &mut Session, p: &Value) -> Result<Value> {
}
let custom = s.smart_previews_dir.is_some();
if background {
let what = if discard { "discarding smart previews" } else { "smart previews" };
let state = Arc::new(PreviewBuild { total: jobs.len(), what, ..Default::default() });
s.preview_build = Some(state.clone());
let task = task_for(s, &state);
let st = state.clone();
set_current(s, state.clone());
let run = Running { task: task_for(s, &state), state: state.clone() };
std::thread::Builder::new()
.name("lc-smart-previews".into())
.spawn(move || {
if let Err(e) = smart_run(&dir, custom, discard, jobs, Some((&st, &task))) {
if let Err(e) = smart_run(&dir, custom, discard, jobs, Some((&run.state, &run.task))) {
log::warn!("{C}: {e}");
*st.error.lock().unwrap_or_else(std::sync::PoisonError::into_inner) = Some(e);
*run.state.error.lock().unwrap_or_else(std::sync::PoisonError::into_inner) = Some(e);
}
st.finished.store(true, Ordering::Relaxed);
run.state.finished.store(true, Ordering::Relaxed);
})
.map_err(|e| bad(C, format!("could not start: {e}")))?;
return Ok(state.json());
Expand Down Expand Up @@ -640,7 +730,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Build Smart Previews",
[],
None,
"{ids?, discard?: bool, background?: bool} — build (or discard) the smart previews of the selected photos (else all in view): compact proxies in the library that keep photos editable and exportable (at proxy size) while their originals are offline; a damaged proxy (cut short) is rebuilt → {built, repaired, removed, failed}; with background (the app's menu) the files are read and written on a worker thread → {total, done, failed, repaired, running, what} like library.buildPreviews (progress: library.previewProgress, stop: library.cancelPreviews)",
"{ids?, discard?: bool, background?: bool, queue?: bool} — build (or discard) the smart previews of the selected photos (else all in view): compact proxies in the library that keep photos editable and exportable (at proxy size) while their originals are offline; a damaged proxy (cut short) is rebuilt → {built, repaired, removed, failed}; with background (the app's menu) the files are read and written on a worker thread → {total, done, failed, repaired, running, what} like library.buildPreviews (progress: library.previewProgress, stop: library.cancelPreviews). A background one while a preview build runs is refused, or with queue waits in line for it, as library.buildPreviews does → {queued, place}",
always,
smart
),
Expand All @@ -667,7 +757,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Build Previews",
[],
None,
"{size?: standard (2048 px, or `edge`) | full (1:1), ids?, wait?: bool} — render the grid thumbnail and loupe view of the selected photos (else all in view) into the preview cache, in the background unless `wait` → {total, done, failed, running}",
"{size?: standard (2048 px, or `edge`) | full (1:1), ids?, wait?: bool, queue?: bool} — render the grid thumbnail and loupe view of the selected photos (else all in view) into the preview cache, in the background unless `wait` → {total, done, failed, running}. One build runs at a time (this, or Build / Discard Smart Previews in the background): while one runs another is refused, or with queue waits in line for it and starts by itself when the builds ahead of it end → {queued: its activity id, place: 1 = next}. It builds the photos chosen when it was asked; activity.list shows it waiting, activity.cancel takes it out of the line. Not with wait while one runs",
always,
build
),
Expand Down Expand Up @@ -716,4 +806,38 @@ mod tests {
assert_eq!(r["total"], json!(ids.len()));
assert_eq!(r["failed"], json!(0));
}
// Given a build whose worker dies (or can't start), the build reads as finished and its row goes, so that a
// build waiting in line starts instead of being refused as "already running"
#[test]
fn a_build_whose_worker_dies_reads_as_finished_before_its_row_goes() {
use std::sync::Arc;
use std::sync::atomic::Ordering;

let s = Session::with_demo();
let state = Arc::new(super::PreviewBuild::default());
let run = super::Running { task: super::task_for(&s, &state), state: state.clone() };
assert!(s.activity.lane_busy(crate::activity::Lane::Previews));
let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(move || {
let _run = run;
panic!("synthetic worker panic");
}));
assert!(r.is_err());
assert!(state.finished.load(Ordering::Relaxed));
assert_eq!(state.error().as_deref(), Some("the build stopped unexpectedly"), "a crash is not a success that built nothing");
assert!(!s.activity.lane_busy(crate::activity::Lane::Previews));
}

// Given a build that marked itself finished, its row going leaves no error
#[test]
fn a_build_that_finished_leaves_no_error() {
use std::sync::Arc;
use std::sync::atomic::Ordering;

let s = Session::with_demo();
let state = Arc::new(super::PreviewBuild::default());
let run = super::Running { task: super::task_for(&s, &state), state: state.clone() };
state.finished.store(true, Ordering::Relaxed);
drop(run);
assert!(state.error().is_none());
}
}
Loading
Loading