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) { fn persist(&self) {
let g = self.inner.lock().unwrap(); let g = self.inner.lock().unwrap();
self.persist_locked(&g); self.persist_locked(&g);
@ -203,16 +230,13 @@ impl DownloadMgr {
it.status = status; it.status = status;
it.bytes = bytes; it.bytes = bytes;
it.msg = msg; 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) { fn worker_loop(mgr: SharedDownloadMgr) {
@ -318,10 +342,10 @@ fn run_download(mgr: &DownloadMgr, asin: &str) {
let mut g = mgr.inner.lock().unwrap(); let mut g = mgr.inner.lock().unwrap();
if let Some(it) = g.items.get_mut(asin) { if let Some(it) = g.items.get_mut(asin) {
it.bytes = size; 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 // Abbruch vermerken
{ {
let mut g = mgr.inner.lock().unwrap(); let g = mgr.inner.lock().unwrap();
if g.cancel_requested { if g.cancel_requested {
// Abbruch wurde vorgemerkt — Status bleibt Cancelled // Abbruch wurde vorgemerkt — Status bleibt Cancelled
g.cancel_requested = false; drop(g); let mut g = g;
let it = { g.cancel_requested = false;
let mut g = mgr.inner.lock().unwrap(); if let Some(it) = g.items.get_mut(asin) {
if let Some(it) = g.items.get_mut(asin) { it.status = DlStatus::Cancelled;
it.status = DlStatus::Cancelled; it.msg = "abgebrochen".into();
it.msg = "abgebrochen".into(); let snapshot = it.clone();
} drop(g);
mgr.item_locked(&g, asin) mgr.emit(&snapshot);
}; return;
mgr.emit(&it); }
return;
} }
} }

View File

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

View File

@ -7,6 +7,7 @@
downloadList, downloadList,
downloadEnqueue, downloadEnqueue,
downloadCancel, downloadCancel,
downloadDelete,
onDownloadUpdate, onDownloadUpdate,
type Book, type Book,
type DlItem, type DlItem,
@ -106,6 +107,22 @@
} }
} }
async function loeschen(b: Book): Promise<void> {
error = ''
info = ''
try {
await downloadDelete(b.asin)
const rest = { ...downloads }
delete rest[b.asin]
downloads = rest
const ro = { ...loadedOverride }
delete ro[b.asin]
loadedOverride = ro
} catch (e) {
error = String(e)
}
}
function hideBrokenCover(e: Event): void { function hideBrokenCover(e: Event): void {
;(e.currentTarget as HTMLElement).style.display = 'none' ;(e.currentTarget as HTMLElement).style.display = 'none'
} }
@ -323,7 +340,13 @@
{#if dlg === 'queued'}<span class="muted small">wartet…</span>{/if} {#if dlg === 'queued'}<span class="muted small">wartet…</span>{/if}
{:else if d && d.status === 'failed'} {:else if d && d.status === 'failed'}
<button class="ghost" onclick={() => laden(b)}>Nochmal laden</button> <button class="ghost" onclick={() => laden(b)}>Nochmal laden</button>
<button class="ghost" onclick={() => loeschen(b)}>Löschen</button>
<span class="muted small" title={d.msg}>{d.msg.slice(0, 60)}</span> <span class="muted small" title={d.msg}>{d.msg.slice(0, 60)}</span>
{:else if d && d.status === 'done'}
<button class="ghost" onclick={() => loeschen(b)}>Löschen</button>
{:else if d && d.status === 'cancelled'}
<button class="ghost" onclick={() => laden(b)}>Laden</button>
<button class="ghost" onclick={() => loeschen(b)}>Löschen</button>
{:else if !isLocal(b)} {:else if !isLocal(b)}
<button class="primary" onclick={() => laden(b)}>Laden</button> <button class="primary" onclick={() => laden(b)}>Laden</button>
{/if} {/if}

View File

@ -35,5 +35,6 @@ export interface DlItem {
export const downloadList = () => invoke<DlItem[]>('download_list') export const downloadList = () => invoke<DlItem[]>('download_list')
export const downloadEnqueue = (asin: string) => invoke<DlItem>('download_enqueue', { asin }) export const downloadEnqueue = (asin: string) => invoke<DlItem>('download_enqueue', { asin })
export const downloadCancel = (asin: string) => invoke<DlItem>('download_cancel', { asin }) export const downloadCancel = (asin: string) => invoke<DlItem>('download_cancel', { asin })
export const downloadDelete = (asin: string) => invoke<void>('download_delete', { asin })
export const onDownloadUpdate = (cb: (it: DlItem) => void) => export const onDownloadUpdate = (cb: (it: DlItem) => void) =>
listen<DlItem>('download-update', (ev) => cb(ev.payload)) listen<DlItem>('download-update', (ev) => cb(ev.payload))