fix #14740: always clean up deepscript jobs and upload copies

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
hauke
2026-11-06 09:20:57 +01:00
co-authored by Claude Opus 5.5
parent e2828791da
commit b0da912381
6 changed files with 427 additions and 47 deletions
+7 -1
View File
@@ -62,10 +62,16 @@ pub fn run() {
config_dir.join("settings.json"),
data_dir.join("recordings"),
);
let leftover_jobs = state.store.calls_with_remote_jobs().unwrap_or_else(|e| {
log::error!("could not read leftover remote jobs: {e}");
Vec::new()
});
let autostart = state.settings().autostart;
// Before anything can start a run.
processing::clear_upload_dir(app.handle());
app.manage(state);
commands::apply_autostart(app.handle(), autostart);
processing::delete_leftover_jobs(app.handle(), recovery.failed);
processing::delete_leftover_jobs(app.handle(), leftover_jobs);
tray::create(app.handle())?;
detection::spawn(app.handle().clone());
+43 -11
View File
@@ -3,6 +3,9 @@
//! system notification when a run ends. Notifications never contain
//! transcript text, and the API key is never logged.
use std::io::ErrorKind;
use std::path::PathBuf;
use recorder_core::model::{CallId, CallStatus};
use recorder_core::pipeline::{self, PipelineOptions};
use recorder_core::secrets::Service;
@@ -36,6 +39,16 @@ impl Claim {
id,
})
}
/// Reserves the call for a cleanup ([`AppState::reserve_processing`]).
fn for_cleanup(app: &AppHandle, id: CallId) -> Option<Self> {
app.state::<AppState>()
.reserve_processing(id)
.then(|| Self {
app: app.clone(),
id,
})
}
}
impl Drop for Claim {
@@ -60,11 +73,7 @@ pub fn start(app: &AppHandle, id: CallId, retry: bool) -> Result<(), String> {
emit_updated(app, id);
return Err(msg);
};
let work_dir = app
.path()
.app_cache_dir()
.map_err(|e| format!("Cache-Ordner nicht gefunden: {e}"))?
.join("upload");
let work_dir = upload_dir(app)?;
let settings = state.settings();
let opts = PipelineOptions {
language: settings.language,
@@ -95,11 +104,34 @@ pub fn start(app: &AppHandle, id: CallId, retry: bool) -> Result<(), String> {
Ok(())
}
/// Best-effort deletes the DeepScript jobs of calls whose processing the
/// last run of the app did not finish ([`RecoveryReport::failed`]), in the
/// Where the pipeline puts the FLAC copies it uploads.
fn upload_dir(app: &AppHandle) -> Result<PathBuf, String> {
app.path()
.app_cache_dir()
.map(|dir| dir.join("upload"))
.map_err(|e| format!("Cache-Ordner nicht gefunden: {e}"))
}
/// Removes FLAC copies left in the upload directory by a run the app did
/// not finish. Call before any run can start.
pub fn clear_upload_dir(app: &AppHandle) {
let dir = match upload_dir(app) {
Ok(dir) => dir,
Err(e) => {
log::warn!("{e}");
return;
}
};
match std::fs::remove_dir_all(&dir) {
Ok(()) => log::info!("removed leftover uploads in {}", dir.display()),
Err(e) if e.kind() == ErrorKind::NotFound => {}
Err(e) => log::warn!("could not empty {}: {e}", dir.display()),
}
}
/// Best-effort deletes the DeepScript jobs still on record for `calls`
/// (from runs the app did not finish, or whose delete failed), in the
/// background. Skipped without a key.
///
/// [`RecoveryReport::failed`]: recorder_core::store::RecoveryReport::failed
pub fn delete_leftover_jobs(app: &AppHandle, calls: Vec<CallId>) {
if calls.is_empty() {
return;
@@ -117,8 +149,8 @@ pub fn delete_leftover_jobs(app: &AppHandle, calls: Vec<CallId>) {
let state = app.state::<AppState>();
let transcriber = DeepScript::new(state.transport.clone(), key);
for id in calls {
// A retry the user started meanwhile cleans up itself.
let Ok(_claim) = Claim::take(&app, id, true) else {
// A run the user started meanwhile cleans up itself.
let Some(_claim) = Claim::for_cleanup(&app, id) else {
continue;
};
pipeline::delete_remote_jobs(&state.store, &transcriber, id).await;
+69 -2
View File
@@ -400,7 +400,7 @@ impl AppState {
/// Deletes a finished call: both audio files and the row, and takes it
/// out of the prompt queue. Returns how many calls still wait for the
/// prompt.
/// prompt. A call that is being processed is not deleted.
pub fn discard_call(&self, id: CallId) -> Result<usize, String> {
if lock(&self.current)
.as_ref()
@@ -408,11 +408,19 @@ impl AppState {
{
return Err("Das Gespräch wird gerade aufgenommen".to_string());
}
// Held until the call is gone, so no run can claim it meanwhile.
let processing = lock(&self.processing);
if processing.contains(&id) {
return Err(ALREADY_PROCESSING.to_string());
}
let call = self
.store
.get_call(id)
.map_err(|e| e.to_string())?
.ok_or_else(|| "Gespräch nicht gefunden".to_string())?;
if call.status == CallStatus::Processing {
return Err(ALREADY_PROCESSING.to_string());
}
// Files first: if one cannot be deleted, the row stays so the user
// can try again instead of leaving orphaned audio behind.
for path in [&call.mic_path, &call.out_path] {
@@ -438,6 +446,7 @@ impl AppState {
}
return Err(format!("Gespräch konnte nicht gelöscht werden: {e}"));
}
drop(processing);
Ok(self.prompts_waiting())
}
@@ -470,7 +479,15 @@ impl AppState {
Ok(())
}
/// Ends the pipeline run claimed by [`AppState::claim_processing`].
/// Reserves `id` for a cleanup of its remote jobs, whatever its status;
/// `false` if a run or cleanup already has it. Release it with
/// [`AppState::release_processing`].
pub fn reserve_processing(&self, id: CallId) -> bool {
lock(&self.processing).insert(id)
}
/// Ends the pipeline run claimed by [`AppState::claim_processing`] (or
/// the cleanup reserved by [`AppState::reserve_processing`]).
pub fn release_processing(&self, id: CallId) {
lock(&self.processing).remove(&id);
}
@@ -707,6 +724,56 @@ mod tests {
assert_eq!(f.state.prompts_waiting(), 0);
}
#[test]
fn a_call_being_processed_is_not_discarded() {
let f = fixture();
let (id, mic, out) = finished_call(&f, "a");
f.state.queue_prompt(id);
f.state.claim_processing(id, false).unwrap();
assert_eq!(
f.state.discard_call(id),
Err("Wird bereits verarbeitet".to_string())
);
f.state.release_processing(id);
f.state
.store
.set_status(id, CallStatus::Processing, None)
.unwrap();
assert_eq!(
f.state.discard_call(id),
Err("Wird bereits verarbeitet".to_string())
);
assert!(mic.exists());
assert!(out.exists());
assert!(f.state.store.get_call(id).unwrap().is_some());
assert_eq!(f.state.prompts_waiting(), 1);
}
#[test]
fn a_cleanup_reserves_a_call_of_any_status() {
let f = fixture();
let (id, _, _) = finished_call(&f, "a");
f.state
.store
.set_status(id, CallStatus::Done, None)
.unwrap();
assert!(f.state.reserve_processing(id));
assert!(!f.state.reserve_processing(id));
f.state
.store
.set_status(id, CallStatus::Failed, Some("Fehler"))
.unwrap();
assert_eq!(
f.state.claim_processing(id, true),
Err("Wird bereits verarbeitet".to_string())
);
f.state.release_processing(id);
assert!(f.state.reserve_processing(id));
}
#[test]
fn a_call_is_processed_once_at_a_time() {
let f = fixture();