mirror of
https://github.com/storytold/lightcraft.git
synced 2026-12-01 10:56:23 +01:00
Jobs that must not run side by side share a lane (`activity::Lane`; the preview lane first: Build Previews and background Build / Discard Smart Previews). With `queue: true` a build asked for while its lane is busy waits in line instead of being refused: it keeps what it was asked, with the photos written out, and `Session::start_waiting` (after every command, and every frame in the app) runs it under the id it had in line once the lane is free. `activity.list` lists waiting tasks after the running ones (`waiting`, `place`, `lane`); `activity.cancel` takes one out of the line, `all` empties it, and opening another library drops it. The line holds at most 100. Without `queue` the call is refused as before. The activity stack draws running tasks only. Co-Authored-By: Claude <noreply@anthropic.com>
66 lines
2.8 KiB
Rust
66 lines
2.8 KiB
Rust
//! `activity.list` / `activity.cancel`: the background tasks in flight (see [`crate::activity`]).
|
|
|
|
use serde_json::{Value, json};
|
|
|
|
use super::{CommandSpec, always, bad, cmd};
|
|
|
|
pub fn specs() -> Vec<CommandSpec> {
|
|
vec![
|
|
cmd!(
|
|
query "activity.list",
|
|
"Background Tasks",
|
|
[],
|
|
None,
|
|
"{} → {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 a task of their lane (waiting: true, place: 1 = next in its lane, lane: previews; done and total 0); total 0 = not known yet",
|
|
always,
|
|
|s, _| Ok(json!({"tasks": s.activity.list()}))
|
|
),
|
|
cmd!(
|
|
"activity.cancel",
|
|
"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). 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";
|
|
if p.get("all").and_then(Value::as_bool) == Some(true) {
|
|
return Ok(json!({"cancelled": s.activity.cancel_all()}));
|
|
}
|
|
let id = p.get("id").and_then(Value::as_u64).ok_or_else(|| bad(C, "id or all"))?;
|
|
s.activity.cancel(id).map_err(|e| bad(C, e))?;
|
|
Ok(json!({"cancelled": 1}))
|
|
}
|
|
),
|
|
]
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use serde_json::json;
|
|
|
|
use crate::Session;
|
|
use crate::activity::Cancel;
|
|
|
|
#[test]
|
|
fn list_and_cancel_through_execute() {
|
|
let mut s = Session::new();
|
|
let g = s.activity.start("export", "Exporting", Cancel::Yes);
|
|
let r = s.execute("activity.list", &json!({})).unwrap();
|
|
assert_eq!(r["tasks"][0]["kind"], "export");
|
|
assert_eq!(r["tasks"][0]["cancellable"], true);
|
|
let r = s.execute("activity.cancel", &json!({"id": g.id()})).unwrap();
|
|
assert_eq!(r["cancelled"], 1);
|
|
assert!(g.is_cancelled());
|
|
}
|
|
|
|
#[test]
|
|
fn cancel_all_and_errors() {
|
|
let mut s = Session::new();
|
|
let (_a, _b) = (s.activity.start("export", "E", Cancel::Yes), s.activity.start("faces", "F", Cancel::No));
|
|
assert_eq!(s.execute("activity.cancel", &json!({"all": true})).unwrap()["cancelled"], 1);
|
|
assert!(s.execute("activity.cancel", &json!({"id": 999})).is_err());
|
|
assert!(s.execute("activity.cancel", &json!({})).is_err());
|
|
}
|
|
}
|