fix #14740: keep calls with pending deepscript cleanup from being discarded
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -217,13 +217,14 @@ pub async fn delete_remote_jobs(store: &Store, tr: &dyn Transcriber, id: CallId)
|
|||||||
/// in this run, in case recording one failed). Forgets the recorded jobs
|
/// in this run, in case recording one failed). Forgets the recorded jobs
|
||||||
/// only if all of them were deleted, so a later run tries again.
|
/// only if all of them were deleted, so a later run tries again.
|
||||||
async fn delete_jobs(store: &Store, tr: &dyn Transcriber, id: CallId, known: &[String]) {
|
async fn delete_jobs(store: &Store, tr: &dyn Transcriber, id: CallId, known: &[String]) {
|
||||||
let mut jobs: Vec<String> = match store.remote_jobs(id) {
|
let recorded: Vec<String> = match store.remote_jobs(id) {
|
||||||
Ok(jobs) => jobs.into_iter().map(|(_, remote_id)| remote_id).collect(),
|
Ok(jobs) => jobs.into_iter().map(|(_, remote_id)| remote_id).collect(),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::error!("call {id}: could not read remote jobs: {e}");
|
log::error!("call {id}: could not read remote jobs: {e}");
|
||||||
Vec::new()
|
Vec::new()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
let mut jobs = recorded.clone();
|
||||||
for job in known {
|
for job in known {
|
||||||
if !jobs.contains(job) {
|
if !jobs.contains(job) {
|
||||||
jobs.push(job.clone());
|
jobs.push(job.clone());
|
||||||
@@ -237,10 +238,18 @@ async fn delete_jobs(store: &Store, tr: &dyn Transcriber, id: CallId, known: &[S
|
|||||||
for job in &jobs {
|
for job in &jobs {
|
||||||
if let Err(e) = tr.delete(job).await {
|
if let Err(e) = tr.delete(job).await {
|
||||||
all_deleted = false;
|
all_deleted = false;
|
||||||
|
if recorded.contains(job) {
|
||||||
log::warn!(
|
log::warn!(
|
||||||
"call {id}: deleting remote job {job} failed: {}",
|
"call {id}: deleting remote job {job} failed: {}",
|
||||||
e.user_message()
|
e.user_message()
|
||||||
);
|
);
|
||||||
|
} else {
|
||||||
|
// Not on record: nothing will try again.
|
||||||
|
log::error!(
|
||||||
|
"call {id}: deleting unrecorded remote job {job} failed, it stays at DeepScript: {}",
|
||||||
|
e.user_message()
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if all_deleted {
|
if all_deleted {
|
||||||
|
|||||||
@@ -17,7 +17,9 @@ use tauri::{AppHandle, Emitter, Manager, State};
|
|||||||
use tauri_plugin_autostart::ManagerExt;
|
use tauri_plugin_autostart::ManagerExt;
|
||||||
|
|
||||||
use crate::processing;
|
use crate::processing;
|
||||||
use crate::state::{AppState, CallBrief, DeviceLevels, Ended, PendingPrompt, RecordingState};
|
use crate::state::{
|
||||||
|
AppState, CallBrief, DeviceLevels, DiscardError, Ended, PendingPrompt, RecordingState,
|
||||||
|
};
|
||||||
|
|
||||||
const LEVELS_INTERVAL: Duration = Duration::from_millis(100);
|
const LEVELS_INTERVAL: Duration = Duration::from_millis(100);
|
||||||
|
|
||||||
@@ -118,10 +120,19 @@ pub fn get_call_brief(state: State<'_, AppState>, id: CallId) -> CmdResult<CallB
|
|||||||
|
|
||||||
/// Deletes a finished call's audio and row. Closes the prompt if no call
|
/// Deletes a finished call's audio and row. Closes the prompt if no call
|
||||||
/// waits for it any more; otherwise the prompt reloads and shows the next.
|
/// waits for it any more; otherwise the prompt reloads and shows the next.
|
||||||
|
/// While its DeepScript jobs are still on record, nothing is deleted and a
|
||||||
|
/// cleanup starts, so a later try can succeed.
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn discard_call(app: AppHandle, id: CallId) -> CmdResult<()> {
|
pub async fn discard_call(app: AppHandle, id: CallId) -> CmdResult<()> {
|
||||||
blocking(app, move |app| {
|
blocking(app, move |app| {
|
||||||
app.state::<AppState>().discard_call(id)?;
|
match app.state::<AppState>().discard_call(id) {
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(e @ DiscardError::CleanupPending) => {
|
||||||
|
processing::delete_leftover_jobs(app, vec![id]);
|
||||||
|
return Err(e.to_string());
|
||||||
|
}
|
||||||
|
Err(e @ DiscardError::Other(_)) => return Err(e.to_string()),
|
||||||
|
}
|
||||||
prompt_queue_changed(app);
|
prompt_queue_changed(app);
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
})
|
||||||
|
|||||||
+69
-9
@@ -35,6 +35,32 @@ const TEST_DURATION: Duration = Duration::from_secs(5);
|
|||||||
const TEST_POLL: Duration = Duration::from_millis(50);
|
const TEST_POLL: Duration = Duration::from_millis(50);
|
||||||
const ALREADY_PROCESSING: &str = "Wird bereits verarbeitet";
|
const ALREADY_PROCESSING: &str = "Wird bereits verarbeitet";
|
||||||
|
|
||||||
|
/// Why [`AppState::discard_call`] refused.
|
||||||
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
pub enum DiscardError {
|
||||||
|
/// DeepScript jobs of the call are still on record; their delete has to
|
||||||
|
/// succeed first, or the cascade would forget them.
|
||||||
|
CleanupPending,
|
||||||
|
Other(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Display for DiscardError {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
DiscardError::CleanupPending => f.write_str(
|
||||||
|
"Daten bei DeepScript werden noch gelöscht – bitte später erneut versuchen.",
|
||||||
|
),
|
||||||
|
DiscardError::Other(msg) => f.write_str(msg),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<String> for DiscardError {
|
||||||
|
fn from(msg: String) -> Self {
|
||||||
|
DiscardError::Other(msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Payload of the `recording-state` event.
|
/// Payload of the `recording-state` event.
|
||||||
#[derive(Serialize, Clone, Debug)]
|
#[derive(Serialize, Clone, Debug)]
|
||||||
#[serde(tag = "state", rename_all = "lowercase")]
|
#[serde(tag = "state", rename_all = "lowercase")]
|
||||||
@@ -400,18 +426,20 @@ impl AppState {
|
|||||||
|
|
||||||
/// Deletes a finished call: both audio files and the row, and takes it
|
/// 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
|
/// out of the prompt queue. Returns how many calls still wait for the
|
||||||
/// prompt. A call that is being processed is not deleted.
|
/// prompt. A call that is being processed, or whose DeepScript jobs
|
||||||
pub fn discard_call(&self, id: CallId) -> Result<usize, String> {
|
/// are still on record ([`DiscardError::CleanupPending`]), is not
|
||||||
|
/// deleted.
|
||||||
|
pub fn discard_call(&self, id: CallId) -> Result<usize, DiscardError> {
|
||||||
if lock(&self.current)
|
if lock(&self.current)
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.is_some_and(|cur| cur.call_id == id)
|
.is_some_and(|cur| cur.call_id == id)
|
||||||
{
|
{
|
||||||
return Err("Das Gespräch wird gerade aufgenommen".to_string());
|
return Err("Das Gespräch wird gerade aufgenommen".to_string().into());
|
||||||
}
|
}
|
||||||
// Held until the call is gone, so no run can claim it meanwhile.
|
// Held until the call is gone, so no run can claim it meanwhile.
|
||||||
let processing = lock(&self.processing);
|
let processing = lock(&self.processing);
|
||||||
if processing.contains(&id) {
|
if processing.contains(&id) {
|
||||||
return Err(ALREADY_PROCESSING.to_string());
|
return Err(ALREADY_PROCESSING.to_string().into());
|
||||||
}
|
}
|
||||||
let call = self
|
let call = self
|
||||||
.store
|
.store
|
||||||
@@ -419,7 +447,11 @@ impl AppState {
|
|||||||
.map_err(|e| e.to_string())?
|
.map_err(|e| e.to_string())?
|
||||||
.ok_or_else(|| "Gespräch nicht gefunden".to_string())?;
|
.ok_or_else(|| "Gespräch nicht gefunden".to_string())?;
|
||||||
if call.status == CallStatus::Processing {
|
if call.status == CallStatus::Processing {
|
||||||
return Err(ALREADY_PROCESSING.to_string());
|
return Err(ALREADY_PROCESSING.to_string().into());
|
||||||
|
}
|
||||||
|
let remote_jobs = self.store.remote_jobs(id).map_err(|e| e.to_string())?;
|
||||||
|
if !remote_jobs.is_empty() {
|
||||||
|
return Err(DiscardError::CleanupPending);
|
||||||
}
|
}
|
||||||
// Files first: if one cannot be deleted, the row stays so the user
|
// Files first: if one cannot be deleted, the row stays so the user
|
||||||
// can try again instead of leaving orphaned audio behind.
|
// can try again instead of leaving orphaned audio behind.
|
||||||
@@ -430,7 +462,9 @@ impl AppState {
|
|||||||
match fs::remove_file(path) {
|
match fs::remove_file(path) {
|
||||||
Ok(()) => {}
|
Ok(()) => {}
|
||||||
Err(e) if e.kind() == ErrorKind::NotFound => {}
|
Err(e) if e.kind() == ErrorKind::NotFound => {}
|
||||||
Err(e) => return Err(format!("Audiodatei konnte nicht gelöscht werden: {e}")),
|
Err(e) => {
|
||||||
|
return Err(format!("Audiodatei konnte nicht gelöscht werden: {e}").into())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Unqueue before deleting the row, so the prompt never sees a queued
|
// Unqueue before deleting the row, so the prompt never sees a queued
|
||||||
@@ -444,7 +478,7 @@ impl AppState {
|
|||||||
queue.insert(index, id);
|
queue.insert(index, id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Err(format!("Gespräch konnte nicht gelöscht werden: {e}"));
|
return Err(format!("Gespräch konnte nicht gelöscht werden: {e}").into());
|
||||||
}
|
}
|
||||||
drop(processing);
|
drop(processing);
|
||||||
Ok(self.prompts_waiting())
|
Ok(self.prompts_waiting())
|
||||||
@@ -733,7 +767,7 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
f.state.discard_call(id),
|
f.state.discard_call(id),
|
||||||
Err("Wird bereits verarbeitet".to_string())
|
Err(DiscardError::Other("Wird bereits verarbeitet".to_string()))
|
||||||
);
|
);
|
||||||
f.state.release_processing(id);
|
f.state.release_processing(id);
|
||||||
f.state
|
f.state
|
||||||
@@ -742,7 +776,7 @@ mod tests {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
f.state.discard_call(id),
|
f.state.discard_call(id),
|
||||||
Err("Wird bereits verarbeitet".to_string())
|
Err(DiscardError::Other("Wird bereits verarbeitet".to_string()))
|
||||||
);
|
);
|
||||||
|
|
||||||
assert!(mic.exists());
|
assert!(mic.exists());
|
||||||
@@ -751,6 +785,32 @@ mod tests {
|
|||||||
assert_eq!(f.state.prompts_waiting(), 1);
|
assert_eq!(f.state.prompts_waiting(), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn a_call_with_recorded_remote_jobs_is_not_discarded() {
|
||||||
|
let f = fixture();
|
||||||
|
let (id, mic, out) = finished_call(&f, "a");
|
||||||
|
f.state.queue_prompt(id);
|
||||||
|
f.state
|
||||||
|
.store
|
||||||
|
.set_status(id, CallStatus::Failed, Some("Fehler"))
|
||||||
|
.unwrap();
|
||||||
|
f.state.store.record_remote_job(id, "mic", "job-1").unwrap();
|
||||||
|
|
||||||
|
assert_eq!(f.state.discard_call(id), Err(DiscardError::CleanupPending));
|
||||||
|
assert_eq!(
|
||||||
|
DiscardError::CleanupPending.to_string(),
|
||||||
|
"Daten bei DeepScript werden noch gelöscht – bitte später erneut versuchen."
|
||||||
|
);
|
||||||
|
|
||||||
|
assert!(mic.exists());
|
||||||
|
assert!(out.exists());
|
||||||
|
assert!(f.state.store.get_call(id).unwrap().is_some());
|
||||||
|
assert_eq!(f.state.store.remote_jobs(id).unwrap().len(), 1);
|
||||||
|
assert_eq!(f.state.prompts_waiting(), 1);
|
||||||
|
// Not reserved: the cleanup the command starts can take the call.
|
||||||
|
assert!(f.state.reserve_processing(id));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn a_cleanup_reserves_a_call_of_any_status() {
|
fn a_cleanup_reserves_a_call_of_any_status() {
|
||||||
let f = fixture();
|
let f = fixture();
|
||||||
|
|||||||
Reference in New Issue
Block a user