T-013: 'Löschen' für geladene/fehlerhafte Jobs (Verzeichnis+Queue-Eintrag), Worker-Guards gegen Löschung während laufendem Download

This commit is contained in:
2026-11-06 19:58:56 +01:00
parent 7095f06873
commit 255a0372ca
4 changed files with 76 additions and 23 deletions

View File

@ -176,6 +176,33 @@ impl DownloadMgr {
}
}
/// Geladene/gebrochene Rohdaten eines Buchs entfernen (Job-Verzeichnis
/// + Eintrag). Läuft gerade ein Job dafür, wird er gestoppt.
pub fn delete(&self, asin: &str) -> Result<(), String> {
let asin = asin.trim().to_string();
let mut g = self.inner.lock().unwrap();
g.queue.retain(|a| a != &asin);
if g.items.get(&asin).map(|i| i.status == DlStatus::Running).unwrap_or(false) {
let mut child = g.child.lock().unwrap();
if let Some(c) = child.as_mut() {
let _ = c.kill();
let _ = c.wait();
}
*child = None;
}
let removed = g.items.remove(&asin).is_some();
self.persist_locked(&g);
drop(g);
let dir = download_root().join(&asin);
if dir.exists() {
fs::remove_dir_all(&dir).map_err(|e| format!("Verzeichnis löschen: {e}"))?;
}
if !removed {
return Err(format!("{} hat keine geladenen Daten", asin));
}
Ok(())
}
fn persist(&self) {
let g = self.inner.lock().unwrap();
self.persist_locked(&g);
@ -203,16 +230,13 @@ impl DownloadMgr {
it.status = status;
it.bytes = bytes;
it.msg = msg;
let snapshot = it.clone();
self.persist_locked(&g);
drop(g);
self.emit(&snapshot);
}
self.persist_locked(&g);
let it = self.item_locked(&g, asin);
drop(g);
self.emit(&it);
}
fn item_locked(&self, g: &Inner, asin: &str) -> DlItem {
g.items.get(asin).cloned().expect("Item vorhanden")
}
}
fn worker_loop(mgr: SharedDownloadMgr) {
@ -318,10 +342,10 @@ fn run_download(mgr: &DownloadMgr, asin: &str) {
let mut g = mgr.inner.lock().unwrap();
if let Some(it) = g.items.get_mut(asin) {
it.bytes = size;
let snapshot = it.clone();
drop(g);
mgr.emit(&snapshot);
}
let it = mgr.item_locked(&g, asin);
drop(g);
mgr.emit(&it);
}
}
@ -348,20 +372,19 @@ fn run_download(mgr: &DownloadMgr, asin: &str) {
// Abbruch vermerken
{
let mut g = mgr.inner.lock().unwrap();
let g = mgr.inner.lock().unwrap();
if g.cancel_requested {
// Abbruch wurde vorgemerkt — Status bleibt Cancelled
g.cancel_requested = false; drop(g);
let it = {
let mut g = mgr.inner.lock().unwrap();
if let Some(it) = g.items.get_mut(asin) {
it.status = DlStatus::Cancelled;
it.msg = "abgebrochen".into();
}
mgr.item_locked(&g, asin)
};
mgr.emit(&it);
return;
let mut g = g;
g.cancel_requested = false;
if let Some(it) = g.items.get_mut(asin) {
it.status = DlStatus::Cancelled;
it.msg = "abgebrochen".into();
let snapshot = it.clone();
drop(g);
mgr.emit(&snapshot);
return;
}
}
}

View File

@ -36,6 +36,11 @@ fn download_cancel(dl: tauri::State<SharedDownloadMgr>, asin: String) -> Result<
dl.cancel(&asin)
}
#[tauri::command]
fn download_delete(dl: tauri::State<SharedDownloadMgr>, asin: String) -> Result<(), String> {
dl.delete(&asin)
}
/// Bibliothek laden: externer CLI-Export dauert (Netzwerk) — läuft deshalb
/// auf einem Blocking-Thread, damit die UI nicht friert.
#[tauri::command]
@ -65,7 +70,8 @@ pub fn run() {
get_library,
download_list,
download_enqueue,
download_cancel
download_cancel,
download_delete
])
.run(tauri::generate_context!())
.expect("error while running tauri application");