//! The model download as a background task the session starts, watches and cancels without //! ever blocking: progress lives in atomics, messages behind a mutex only ever `try_lock`ed //! by the session (the download thread holds it for a few instructions at a time). use std::path::PathBuf; use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; use std::sync::{Arc, Mutex}; use lightcraft_segment::fetch::{self, Options, Progress}; use crate::activity::{Activity, Cancel, Ending, Outcome, Unit}; /// What the session sees of a download. #[derive(Clone, Debug, Default, PartialEq, Eq, serde::Serialize)] pub struct Status { pub running: bool, pub done: u64, pub total: u64, /// The file being fetched. pub file: String, /// The last download's error (cleared when a new one starts). pub error: Option, /// The last download was cancelled (its `error` says so in words): decide on this, not on the text. pub cancelled: bool, /// The last download finished and the files are in place. pub finished: bool, } #[derive(Default)] struct Text { file: String, error: Option, cancelled: bool, } #[derive(Default)] struct Shared { running: AtomicBool, cancel: Arc, finished: AtomicBool, done: AtomicU64, total: AtomicU64, text: Mutex, } /// Clears `running` when the download thread ends, however it ends. struct Running(Arc); impl Drop for Running { fn drop(&mut self) { self.0.running.store(false, Ordering::SeqCst); } } #[derive(Default)] pub struct Downloader { shared: Arc, /// The last text seen (when the download thread held the lock at that moment). last: Mutex, } fn lock(m: &Mutex) -> std::sync::MutexGuard<'_, Text> { m.lock().unwrap_or_else(std::sync::PoisonError::into_inner) } impl Downloader { pub fn status(&self) -> Status { let s = &self.shared; if let Ok(t) = s.text.try_lock() && let Ok(mut last) = self.last.try_lock() { last.file.clone_from(&t.file); last.error.clone_from(&t.error); last.cancelled = t.cancelled; } // (only the session's thread locks `last`) let (file, error, cancelled) = self.last.try_lock().map(|l| (l.file.clone(), l.error.clone(), l.cancelled)).unwrap_or_default(); Status { running: s.running.load(Ordering::SeqCst), done: s.done.load(Ordering::SeqCst), total: s.total.load(Ordering::SeqCst), file, error, cancelled, finished: s.finished.load(Ordering::SeqCst), } } pub fn running(&self) -> bool { self.shared.running.load(Ordering::SeqCst) } /// Ask a running download to stop (it keeps its partial files to resume later). pub fn cancel(&self) -> bool { let running = self.running(); if running { self.shared.cancel.store(true, Ordering::SeqCst); } running } /// Start downloading `files` from `mirrors` into `dir` on a background thread, shown in the activity stack as /// `name` (its ✕ cancels like [`Downloader::cancel`]). False when one is already running. pub fn start( &self, files: &'static [fetch::FileSpec], mirrors: Vec, dir: PathBuf, opts: Options, activity: &Activity, name: &str, ) -> Result { let s = self.shared.clone(); if s.running.swap(true, Ordering::SeqCst) { return Ok(false); } s.cancel.store(false, Ordering::SeqCst); s.finished.store(false, Ordering::SeqCst); s.done.store(0, Ordering::SeqCst); s.total.store(files.iter().filter_map(|f| f.size).sum(), Ordering::SeqCst); // nothing else runs while `running` was false *lock(&s.text) = Text::default(); *lock(&self.last) = Text::default(); let task = activity.start("download", "Downloading model", Cancel::Flag(s.cancel.clone())); task.set_unit(Unit::Bytes); task.detail(name); task.progress(0, s.total.load(Ordering::SeqCst)); let guard = Running(s.clone()); let name = name.to_string(); let spawned = std::thread::Builder::new().name("sam3-download".into()).spawn(move || { let s = guard.0.clone(); let mut last_file = String::new(); let mut on_progress = |p: &Progress| { s.done.store(p.done, Ordering::SeqCst); s.total.store(p.total, Ordering::SeqCst); task.progress(p.done, p.total); if p.file != last_file { last_file.clone_from(&p.file); lock(&s.text).file.clone_from(&p.file); } }; let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| fetch::download(files, &mirrors, &dir, &opts, &s.cancel, &mut on_progress))); let cancelled = matches!(r, Ok(Err(fetch::DownloadError::Cancelled))); let (error, outcome) = match r { Ok(Ok(())) => { s.finished.store(true, Ordering::SeqCst); log::info!("SAM 3 model downloaded to {}", dir.display()); (None, Outcome::default()) } Ok(Err(e)) if s.cancel.load(Ordering::SeqCst) => (Some(e.to_string()), Outcome::ended(Ending::Stopped)), Ok(Err(e)) => (Some(e.to_string()), Outcome::failed(e.to_string())), Err(_) => (Some("the download failed unexpectedly".to_string()), Outcome::ended(Ending::Died)), }; if let Some(e) = &error { log::warn!("SAM 3 download: {e}"); } let mut text = lock(&s.text); text.cancelled = cancelled; text.error = error; drop(text); drop(guard); // last: whoever sees the row go finds the download over task.finish(outcome.about(&name)); }); match spawned { Ok(_) => Ok(true), Err(e) => { // the closure (and its guard) was dropped: `running` is false again Err(format!("could not start the download: {e}")) } } } }