feat #14740: scheduled retention
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -241,7 +241,7 @@ pub async fn discard_call(app: AppHandle, id: CallId) -> CmdResult<()> {
|
|||||||
processing::delete_leftover_jobs(app, vec![id]);
|
processing::delete_leftover_jobs(app, vec![id]);
|
||||||
return Err(e.to_string());
|
return Err(e.to_string());
|
||||||
}
|
}
|
||||||
Err(e @ DiscardError::Other(_)) => return Err(e.to_string()),
|
Err(e) => return Err(e.to_string()),
|
||||||
}
|
}
|
||||||
prompt_queue_changed(app);
|
prompt_queue_changed(app);
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ mod commands;
|
|||||||
mod detection;
|
mod detection;
|
||||||
mod playback;
|
mod playback;
|
||||||
mod processing;
|
mod processing;
|
||||||
|
mod retention;
|
||||||
mod state;
|
mod state;
|
||||||
mod tray;
|
mod tray;
|
||||||
mod updater;
|
mod updater;
|
||||||
@@ -129,6 +130,9 @@ pub fn run() {
|
|||||||
create_windows(app.handle())?;
|
create_windows(app.handle())?;
|
||||||
commands::apply_autostart(app.handle(), autostart);
|
commands::apply_autostart(app.handle(), autostart);
|
||||||
processing::delete_leftover_jobs(app.handle(), leftover_jobs);
|
processing::delete_leftover_jobs(app.handle(), leftover_jobs);
|
||||||
|
// After the startup recovery and the cache clears, with the
|
||||||
|
// windows there to hear what it deleted.
|
||||||
|
retention::spawn(app.handle().clone());
|
||||||
|
|
||||||
tray::create(app.handle())?;
|
tray::create(app.handle())?;
|
||||||
detection::spawn(app.handle().clone());
|
detection::spawn(app.handle().clone());
|
||||||
|
|||||||
@@ -0,0 +1,364 @@
|
|||||||
|
//! The scheduled retention job: once at startup and then every hour (by the
|
||||||
|
//! wall clock, so also right after standby) it deletes what the retention
|
||||||
|
//! period (`retention_days` in the settings, read anew for every pass) has
|
||||||
|
//! run out for, as planned by [`plan_retention`]: the audio of transcribed
|
||||||
|
//! calls N days after the transcription (transcript, title, speaker names
|
||||||
|
//! and merges, CRM upload stay), and undecided recordings with their row N
|
||||||
|
//! days after the call ended. Failed calls keep their audio. A call that is
|
||||||
|
//! in use (recorded, processed, uploaded, played, or its DeepScript jobs
|
||||||
|
//! still on record) is skipped and tried again by the next pass; errors are
|
||||||
|
//! logged, never fatal.
|
||||||
|
|
||||||
|
use std::panic::{self, AssertUnwindSafe};
|
||||||
|
use std::thread;
|
||||||
|
use std::time::{Duration, SystemTime};
|
||||||
|
|
||||||
|
use chrono::{DateTime, Local};
|
||||||
|
use recorder_core::model::CallId;
|
||||||
|
use recorder_core::retention::{plan_retention, RetentionAction};
|
||||||
|
use tauri::{AppHandle, Manager};
|
||||||
|
|
||||||
|
use crate::commands;
|
||||||
|
use crate::processing;
|
||||||
|
use crate::state::{AppState, AudioDeleteError, DiscardError};
|
||||||
|
|
||||||
|
/// How often a pass runs.
|
||||||
|
const PASS_INTERVAL: Duration = Duration::from_secs(60 * 60);
|
||||||
|
/// How often the job looks at the wall clock to see whether a pass is due.
|
||||||
|
const WAKE_INTERVAL: Duration = Duration::from_secs(60);
|
||||||
|
|
||||||
|
/// What one pass did, per call.
|
||||||
|
#[derive(Debug, Default, PartialEq)]
|
||||||
|
pub struct PassReport {
|
||||||
|
pub audio_deleted: Vec<CallId>,
|
||||||
|
pub calls_deleted: Vec<CallId>,
|
||||||
|
/// In use; tried again by the next pass.
|
||||||
|
pub skipped: Vec<CallId>,
|
||||||
|
pub failed: Vec<CallId>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PassReport {
|
||||||
|
/// The calls whose row or audio changed.
|
||||||
|
fn changed(&self) -> impl Iterator<Item = CallId> + '_ {
|
||||||
|
self.audio_deleted
|
||||||
|
.iter()
|
||||||
|
.chain(&self.calls_deleted)
|
||||||
|
.copied()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_empty(&self) -> bool {
|
||||||
|
self == &PassReport::default()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// "2 audio deleted, 1 call deleted, 1 skipped, 0 failed" for the log.
|
||||||
|
fn summary(&self) -> String {
|
||||||
|
let calls = self.calls_deleted.len();
|
||||||
|
format!(
|
||||||
|
"{} audio deleted, {calls} {} deleted, {} skipped, {} failed",
|
||||||
|
self.audio_deleted.len(),
|
||||||
|
if calls == 1 { "call" } else { "calls" },
|
||||||
|
self.skipped.len(),
|
||||||
|
self.failed.len()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Starts the job: a pass right away and then every [`PASS_INTERVAL`].
|
||||||
|
/// Call once the startup recovery, the cache clears and the windows are
|
||||||
|
/// done. One thread runs every pass, so two never run at once.
|
||||||
|
pub fn spawn(app: AppHandle) {
|
||||||
|
let started = thread::Builder::new()
|
||||||
|
.name("retention".into())
|
||||||
|
.spawn(move || {
|
||||||
|
let mut last_pass = None;
|
||||||
|
loop {
|
||||||
|
let now = SystemTime::now();
|
||||||
|
if pass_due(last_pass, now) {
|
||||||
|
last_pass = Some(now);
|
||||||
|
// A panic must not end the job: the next pass retries.
|
||||||
|
if panic::catch_unwind(AssertUnwindSafe(|| run(&app))).is_err() {
|
||||||
|
log::error!("retention: the pass panicked");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
thread::sleep(WAKE_INTERVAL);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if let Err(e) = started {
|
||||||
|
log::error!("retention: could not start the job: {e}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// One pass, and tells the UI what changed: `call-updated` per call (the
|
||||||
|
/// list and an open detail refresh; the player shows the retention note),
|
||||||
|
/// and the prompt, if an undecided call it waits for was deleted.
|
||||||
|
fn run(app: &AppHandle) {
|
||||||
|
let report = run_pass(&app.state::<AppState>(), Local::now());
|
||||||
|
for id in report.changed() {
|
||||||
|
processing::emit_updated(app, id);
|
||||||
|
}
|
||||||
|
if !report.calls_deleted.is_empty() {
|
||||||
|
commands::prompt_queue_changed(app);
|
||||||
|
}
|
||||||
|
if !report.is_empty() {
|
||||||
|
log::info!("retention: {}", report.summary());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Runs one pass over all calls as of `now`, with the retention period
|
||||||
|
/// currently set.
|
||||||
|
pub fn run_pass(state: &AppState, now: DateTime<Local>) -> PassReport {
|
||||||
|
let mut report = PassReport::default();
|
||||||
|
let calls = match state.store.list_calls() {
|
||||||
|
Ok(calls) => calls,
|
||||||
|
Err(e) => {
|
||||||
|
log::error!("retention: could not list the calls: {e}");
|
||||||
|
return report;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
for action in plan_retention(&calls, now, state.settings().retention_days) {
|
||||||
|
match action {
|
||||||
|
RetentionAction::DeleteAudio(id) => match state.delete_audio(id) {
|
||||||
|
Ok(()) => report.audio_deleted.push(id),
|
||||||
|
Err(AudioDeleteError::Busy(why)) => {
|
||||||
|
log::info!("retention: call {id}: audio kept for now: {why}");
|
||||||
|
report.skipped.push(id);
|
||||||
|
}
|
||||||
|
Err(AudioDeleteError::Failed(e)) => {
|
||||||
|
log::error!("retention: call {id}: audio not deleted: {e}");
|
||||||
|
report.failed.push(id);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
// The same path, and refusals, as "Verwerfen".
|
||||||
|
RetentionAction::DeleteCall(id) => match state.discard_call(id) {
|
||||||
|
Ok(_) => report.calls_deleted.push(id),
|
||||||
|
Err(e @ (DiscardError::Busy(_) | DiscardError::CleanupPending)) => {
|
||||||
|
log::info!("retention: call {id}: kept for now: {e}");
|
||||||
|
report.skipped.push(id);
|
||||||
|
}
|
||||||
|
Err(DiscardError::Other(e)) => {
|
||||||
|
log::error!("retention: call {id}: not deleted: {e}");
|
||||||
|
report.failed.push(id);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
report
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Whether the next pass is due at `now`, given when the last one started
|
||||||
|
/// (`None`: none yet). Uses the wall clock, which keeps running in standby;
|
||||||
|
/// a clock that went back makes the pass due.
|
||||||
|
fn pass_due(last: Option<SystemTime>, now: SystemTime) -> bool {
|
||||||
|
let Some(last) = last else {
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
now.duration_since(last)
|
||||||
|
.map_or(true, |elapsed| elapsed >= PASS_INTERVAL)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use std::fs;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
use chrono::Duration as Days;
|
||||||
|
use recorder_core::model::CallStatus;
|
||||||
|
use recorder_core::secrets::MemorySecrets;
|
||||||
|
use recorder_core::store::Store;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
struct Fixture {
|
||||||
|
state: AppState,
|
||||||
|
dir: tempfile::TempDir,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fixture() -> Fixture {
|
||||||
|
let dir = tempfile::tempdir().unwrap();
|
||||||
|
let state = AppState::new(
|
||||||
|
Store::open_in_memory().unwrap(),
|
||||||
|
Box::new(MemorySecrets::default()),
|
||||||
|
dir.path().join("settings.json"),
|
||||||
|
dir.path().join("recordings"),
|
||||||
|
dir.path().join("playback"),
|
||||||
|
);
|
||||||
|
Fixture { state, dir }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A call with both track files that ended `ended_days_ago` and, if
|
||||||
|
/// `transcribed_days_ago` is given, was transcribed then (status
|
||||||
|
/// `Done`); otherwise it awaits the decision.
|
||||||
|
fn call(
|
||||||
|
f: &Fixture,
|
||||||
|
name: &str,
|
||||||
|
now: DateTime<Local>,
|
||||||
|
ended_days_ago: i64,
|
||||||
|
transcribed_days_ago: Option<i64>,
|
||||||
|
) -> CallId {
|
||||||
|
let mic = f.dir.path().join(format!("{name}-mic.wav"));
|
||||||
|
let out = f.dir.path().join(format!("{name}-out.wav"));
|
||||||
|
fs::write(&mic, b"mic").unwrap();
|
||||||
|
fs::write(&out, b"out").unwrap();
|
||||||
|
let store = &f.state.store;
|
||||||
|
let ended = now - Days::days(ended_days_ago);
|
||||||
|
let id = store
|
||||||
|
.create_call("Microsoft Teams", ended - Days::minutes(5), &mic, &out)
|
||||||
|
.unwrap();
|
||||||
|
store.finish_recording(id, ended, false).unwrap();
|
||||||
|
if let Some(days) = transcribed_days_ago {
|
||||||
|
store.set_status(id, CallStatus::Done, None).unwrap();
|
||||||
|
store.mark_transcribed(id, now - Days::days(days)).unwrap();
|
||||||
|
}
|
||||||
|
id
|
||||||
|
}
|
||||||
|
|
||||||
|
fn tracks(f: &Fixture, id: CallId) -> [PathBuf; 2] {
|
||||||
|
let call = f.state.store.get_call(id).unwrap().unwrap();
|
||||||
|
[call.mic_path, call.out_path]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn has_audio(f: &Fixture, id: CallId) -> bool {
|
||||||
|
let call = f.state.store.get_call(id).unwrap().unwrap();
|
||||||
|
!call.audio_deleted && tracks(f, id).iter().all(|p| p.exists())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn sorted(mut ids: Vec<CallId>) -> Vec<CallId> {
|
||||||
|
ids.sort();
|
||||||
|
ids
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn a_pass_does_exactly_what_the_plan_says_and_skips_calls_in_use() {
|
||||||
|
let f = fixture();
|
||||||
|
let now = Local::now();
|
||||||
|
let done_old = call(&f, "done-old", now, 9, Some(8));
|
||||||
|
let done_new = call(&f, "done-new", now, 3, Some(2));
|
||||||
|
let undecided_old = call(&f, "undecided-old", now, 8, None);
|
||||||
|
let undecided_new = call(&f, "undecided-new", now, 6, None);
|
||||||
|
let failed_old = call(&f, "failed-old", now, 30, None);
|
||||||
|
f.state
|
||||||
|
.store
|
||||||
|
.set_status(failed_old, CallStatus::Failed, Some("Fehler"))
|
||||||
|
.unwrap();
|
||||||
|
let uploading = call(&f, "uploading", now, 9, Some(8));
|
||||||
|
let upload = f.state.claim_upload(uploading).unwrap();
|
||||||
|
// Its DeepScript jobs are still on record: not deleted yet.
|
||||||
|
let cleanup = call(&f, "cleanup", now, 8, None);
|
||||||
|
f.state
|
||||||
|
.store
|
||||||
|
.record_remote_job(cleanup, "mic", "job-1")
|
||||||
|
.unwrap();
|
||||||
|
let undecided_old_tracks = tracks(&f, undecided_old);
|
||||||
|
|
||||||
|
let report = run_pass(&f.state, now);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
PassReport {
|
||||||
|
audio_deleted: sorted(report.audio_deleted),
|
||||||
|
calls_deleted: report.calls_deleted,
|
||||||
|
skipped: sorted(report.skipped),
|
||||||
|
failed: report.failed,
|
||||||
|
},
|
||||||
|
PassReport {
|
||||||
|
audio_deleted: vec![done_old],
|
||||||
|
calls_deleted: vec![undecided_old],
|
||||||
|
skipped: sorted(vec![uploading, cleanup]),
|
||||||
|
failed: vec![],
|
||||||
|
}
|
||||||
|
);
|
||||||
|
assert!(!has_audio(&f, done_old));
|
||||||
|
assert!(f.state.store.get_call(done_old).unwrap().is_some());
|
||||||
|
assert_eq!(f.state.store.get_call(undecided_old).unwrap(), None);
|
||||||
|
assert!(undecided_old_tracks.iter().all(|p| !p.exists()));
|
||||||
|
for kept in [done_new, undecided_new, failed_old, uploading, cleanup] {
|
||||||
|
assert!(has_audio(&f, kept), "call {kept} keeps its audio");
|
||||||
|
}
|
||||||
|
assert_eq!(f.state.store.remote_jobs(cleanup).unwrap().len(), 1);
|
||||||
|
|
||||||
|
// The next pass tries the skipped call again; nothing is done twice.
|
||||||
|
drop(upload);
|
||||||
|
let report = run_pass(&f.state, now);
|
||||||
|
assert_eq!(report.audio_deleted, vec![uploading]);
|
||||||
|
assert_eq!(report.calls_deleted, Vec::<CallId>::new());
|
||||||
|
assert_eq!(report.skipped, vec![cleanup]);
|
||||||
|
assert!(!has_audio(&f, uploading));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn a_call_that_fails_does_not_stop_the_pass() {
|
||||||
|
let f = fixture();
|
||||||
|
let now = Local::now();
|
||||||
|
let broken = call(&f, "broken", now, 9, Some(8));
|
||||||
|
// A directory where the mic track should be: removing it fails.
|
||||||
|
let [mic, _] = tracks(&f, broken);
|
||||||
|
fs::remove_file(&mic).unwrap();
|
||||||
|
fs::create_dir(&mic).unwrap();
|
||||||
|
let fine = call(&f, "fine", now, 9, Some(8));
|
||||||
|
|
||||||
|
let report = run_pass(&f.state, now);
|
||||||
|
assert_eq!(report.failed, vec![broken]);
|
||||||
|
assert_eq!(report.audio_deleted, vec![fine]);
|
||||||
|
assert!(
|
||||||
|
!f.state
|
||||||
|
.store
|
||||||
|
.get_call(broken)
|
||||||
|
.unwrap()
|
||||||
|
.unwrap()
|
||||||
|
.audio_deleted
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn a_changed_retention_period_applies_to_the_next_pass() {
|
||||||
|
let f = fixture();
|
||||||
|
let now = Local::now();
|
||||||
|
let id = call(&f, "a", now, 4, Some(3));
|
||||||
|
assert_eq!(run_pass(&f.state, now), PassReport::default());
|
||||||
|
|
||||||
|
let mut settings = f.state.settings();
|
||||||
|
settings.retention_days = 2;
|
||||||
|
f.state.save_settings(settings).unwrap();
|
||||||
|
assert_eq!(run_pass(&f.state, now).audio_deleted, vec![id]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn the_summary_counts_each_outcome() {
|
||||||
|
let report = PassReport {
|
||||||
|
audio_deleted: vec![1, 2],
|
||||||
|
calls_deleted: vec![3],
|
||||||
|
skipped: vec![4],
|
||||||
|
failed: vec![],
|
||||||
|
};
|
||||||
|
assert_eq!(
|
||||||
|
report.summary(),
|
||||||
|
"2 audio deleted, 1 call deleted, 1 skipped, 0 failed"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const MIN: Duration = Duration::from_secs(60);
|
||||||
|
|
||||||
|
fn t0() -> SystemTime {
|
||||||
|
SystemTime::UNIX_EPOCH + Duration::from_secs(1_800_000_000)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn the_first_pass_is_due_at_once() {
|
||||||
|
assert!(pass_due(None, t0()));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn the_next_pass_is_due_an_hour_later() {
|
||||||
|
assert!(!pass_due(Some(t0()), t0() + 59 * MIN));
|
||||||
|
assert!(pass_due(Some(t0()), t0() + 60 * MIN));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn a_pass_is_due_after_standby_by_the_wall_clock() {
|
||||||
|
assert!(pass_due(Some(t0()), t0() + 48 * 60 * MIN));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn a_pass_is_due_when_the_clock_went_back() {
|
||||||
|
assert!(pass_due(Some(t0()), t0() - 60 * MIN));
|
||||||
|
}
|
||||||
|
}
|
||||||
+262
-9
@@ -40,6 +40,8 @@ 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";
|
||||||
const ALREADY_UPLOADING: &str = "Wird bereits hochgeladen";
|
const ALREADY_UPLOADING: &str = "Wird bereits hochgeladen";
|
||||||
|
const BEING_RECORDED: &str = "Das Gespräch wird gerade aufgenommen";
|
||||||
|
const PLAYING: &str = "Das Gespräch wird gerade abgespielt";
|
||||||
|
|
||||||
/// Why [`AppState::discard_call`] refused.
|
/// Why [`AppState::discard_call`] refused.
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
@@ -47,6 +49,8 @@ pub enum DiscardError {
|
|||||||
/// DeepScript jobs of the call are still on record; their delete has to
|
/// DeepScript jobs of the call are still on record; their delete has to
|
||||||
/// succeed first, or the cascade would forget them.
|
/// succeed first, or the cascade would forget them.
|
||||||
CleanupPending,
|
CleanupPending,
|
||||||
|
/// The call is recorded, processed or uploaded right now.
|
||||||
|
Busy(&'static str),
|
||||||
Other(String),
|
Other(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,6 +60,7 @@ impl std::fmt::Display for DiscardError {
|
|||||||
DiscardError::CleanupPending => f.write_str(
|
DiscardError::CleanupPending => f.write_str(
|
||||||
"Daten bei DeepScript werden noch gelöscht – bitte später erneut versuchen.",
|
"Daten bei DeepScript werden noch gelöscht – bitte später erneut versuchen.",
|
||||||
),
|
),
|
||||||
|
DiscardError::Busy(msg) => f.write_str(msg),
|
||||||
DiscardError::Other(msg) => f.write_str(msg),
|
DiscardError::Other(msg) => f.write_str(msg),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -67,6 +72,14 @@ impl From<String> for DiscardError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Why [`AppState::delete_audio`] did not delete a call's audio.
|
||||||
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
pub enum AudioDeleteError {
|
||||||
|
/// The call is in use; the next retention pass tries again.
|
||||||
|
Busy(&'static str),
|
||||||
|
Failed(String),
|
||||||
|
}
|
||||||
|
|
||||||
/// 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")]
|
||||||
@@ -723,8 +736,8 @@ impl AppState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Removes call `id`'s cached mix whoever holds it: with the call, and
|
/// Removes call `id`'s cached mix whoever holds it: with the call, and
|
||||||
/// once its audio is deleted. The retention job, when it deletes a
|
/// once its audio is deleted ([`AppState::delete_audio`] does the same
|
||||||
/// call's audio, calls this too.
|
/// while it holds the leases).
|
||||||
pub fn remove_playback(&self, id: CallId) {
|
pub fn remove_playback(&self, id: CallId) {
|
||||||
let mut leases = lock(&self.playback);
|
let mut leases = lock(&self.playback);
|
||||||
leases.latest.remove(&id);
|
leases.latest.remove(&id);
|
||||||
@@ -821,18 +834,18 @@ impl AppState {
|
|||||||
.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().into());
|
return Err(DiscardError::Busy(BEING_RECORDED));
|
||||||
}
|
}
|
||||||
// Both held until the call is gone, so neither a pipeline run nor an
|
// Both held until the call is gone, so neither a pipeline run nor an
|
||||||
// upload can claim it meanwhile (same uploading -> store lock order
|
// upload can claim it meanwhile (same uploading -> store lock order
|
||||||
// as `claim_upload`, so this can never deadlock against it).
|
// as `claim_upload`, so this can never deadlock against it).
|
||||||
let processing = lock(&self.processing);
|
let processing = lock(&self.processing);
|
||||||
if processing.contains(&id) {
|
if processing.contains(&id) {
|
||||||
return Err(ALREADY_PROCESSING.to_string().into());
|
return Err(DiscardError::Busy(ALREADY_PROCESSING));
|
||||||
}
|
}
|
||||||
let uploading = lock(&self.uploading);
|
let uploading = lock(&self.uploading);
|
||||||
if uploading.contains(&id) {
|
if uploading.contains(&id) {
|
||||||
return Err(ALREADY_UPLOADING.to_string().into());
|
return Err(DiscardError::Busy(ALREADY_UPLOADING));
|
||||||
}
|
}
|
||||||
let call = self
|
let call = self
|
||||||
.store
|
.store
|
||||||
@@ -840,7 +853,7 @@ 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().into());
|
return Err(DiscardError::Busy(ALREADY_PROCESSING));
|
||||||
}
|
}
|
||||||
let remote_jobs = self.store.remote_jobs(id).map_err(|e| e.to_string())?;
|
let remote_jobs = self.store.remote_jobs(id).map_err(|e| e.to_string())?;
|
||||||
if !remote_jobs.is_empty() {
|
if !remote_jobs.is_empty() {
|
||||||
@@ -880,6 +893,66 @@ impl AppState {
|
|||||||
Ok(self.prompts_waiting())
|
Ok(self.prompts_waiting())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Deletes a call's audio for the retention job: both track files and
|
||||||
|
/// its cached mix, then marks the row (`audio_deleted`). Transcript,
|
||||||
|
/// title, speaker names and merges and the CRM upload stay. Refused
|
||||||
|
/// ([`AudioDeleteError::Busy`]) while the call is recorded, processed
|
||||||
|
/// (or its remote jobs cleaned up), uploaded or a player holds its mix.
|
||||||
|
/// Already missing files are fine; a file that cannot be deleted leaves
|
||||||
|
/// the row unmarked, so the next pass tries again.
|
||||||
|
pub fn delete_audio(&self, id: CallId) -> Result<(), AudioDeleteError> {
|
||||||
|
if lock(&self.current)
|
||||||
|
.as_ref()
|
||||||
|
.is_some_and(|cur| cur.call_id == id)
|
||||||
|
{
|
||||||
|
return Err(AudioDeleteError::Busy(BEING_RECORDED));
|
||||||
|
}
|
||||||
|
// All held until the row is marked, so no run, upload or player can
|
||||||
|
// take the call meanwhile (same processing -> uploading -> playback
|
||||||
|
// -> store lock order as `discard_call` and `prepare_playback`).
|
||||||
|
let processing = lock(&self.processing);
|
||||||
|
if processing.contains(&id) {
|
||||||
|
return Err(AudioDeleteError::Busy(ALREADY_PROCESSING));
|
||||||
|
}
|
||||||
|
let uploading = lock(&self.uploading);
|
||||||
|
if uploading.contains(&id) {
|
||||||
|
return Err(AudioDeleteError::Busy(ALREADY_UPLOADING));
|
||||||
|
}
|
||||||
|
let mut leases = lock(&self.playback);
|
||||||
|
if leases.latest.contains_key(&id) {
|
||||||
|
return Err(AudioDeleteError::Busy(PLAYING));
|
||||||
|
}
|
||||||
|
let call = self.find_call(id).map_err(AudioDeleteError::Failed)?;
|
||||||
|
match call.status {
|
||||||
|
CallStatus::Recording => return Err(AudioDeleteError::Busy(BEING_RECORDED)),
|
||||||
|
CallStatus::Processing => return Err(AudioDeleteError::Busy(ALREADY_PROCESSING)),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
// Every file is tried, so as little audio as possible stays behind.
|
||||||
|
let mut failed = None;
|
||||||
|
for path in [&call.mic_path, &call.out_path] {
|
||||||
|
if path.as_os_str().is_empty() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
match fs::remove_file(path) {
|
||||||
|
Ok(()) => {}
|
||||||
|
Err(e) if e.kind() == ErrorKind::NotFound => {}
|
||||||
|
Err(e) => {
|
||||||
|
log::error!("call {id}: could not delete {}: {e}", path.display());
|
||||||
|
failed = Some(format!("Audiodatei konnte nicht gelöscht werden: {e}"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if let Some(msg) = failed {
|
||||||
|
return Err(AudioDeleteError::Failed(msg));
|
||||||
|
}
|
||||||
|
leases.latest.remove(&id);
|
||||||
|
playback::remove_cached(&self.playback_dir, id);
|
||||||
|
self.store
|
||||||
|
.mark_audio_deleted(id)
|
||||||
|
.map_err(|e| AudioDeleteError::Failed(format!("Löschen nicht vermerkt: {e}")))
|
||||||
|
}
|
||||||
|
|
||||||
/// Reserves `id` for one pipeline run; release it with
|
/// Reserves `id` for one pipeline run; release it with
|
||||||
/// [`AppState::release_processing`] when the run ends. A call that waits
|
/// [`AppState::release_processing`] when the run ends. A call that waits
|
||||||
/// for the decision can be processed; a failed one only on `retry`, or
|
/// for the decision can be processed; a failed one only on `retry`, or
|
||||||
@@ -1212,7 +1285,7 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
f.state.discard_call(id),
|
f.state.discard_call(id),
|
||||||
Err(DiscardError::Other("Wird bereits verarbeitet".to_string()))
|
Err(DiscardError::Busy("Wird bereits verarbeitet"))
|
||||||
);
|
);
|
||||||
f.state.release_processing(id);
|
f.state.release_processing(id);
|
||||||
f.state
|
f.state
|
||||||
@@ -1221,7 +1294,7 @@ mod tests {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
f.state.discard_call(id),
|
f.state.discard_call(id),
|
||||||
Err(DiscardError::Other("Wird bereits verarbeitet".to_string()))
|
Err(DiscardError::Busy("Wird bereits verarbeitet"))
|
||||||
);
|
);
|
||||||
|
|
||||||
assert!(mic.exists());
|
assert!(mic.exists());
|
||||||
@@ -1283,7 +1356,7 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
f.state.discard_call(id),
|
f.state.discard_call(id),
|
||||||
Err(DiscardError::Other("Wird bereits hochgeladen".to_string()))
|
Err(DiscardError::Busy("Wird bereits hochgeladen"))
|
||||||
);
|
);
|
||||||
assert!(mic.exists());
|
assert!(mic.exists());
|
||||||
assert!(out.exists());
|
assert!(out.exists());
|
||||||
@@ -2138,4 +2211,184 @@ mod tests {
|
|||||||
assert!(!f.dir.path().join("playback").exists());
|
assert!(!f.dir.path().join("playback").exists());
|
||||||
f.state.clear_playback_cache(); // nothing left: no error
|
f.state.clear_playback_cache(); // nothing left: no error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A transcribed (`Done`) call with both tracks, a transcript, a title,
|
||||||
|
/// a CRM upload and a cached mix nobody holds.
|
||||||
|
fn transcribed_call_with_audio(f: &Fixture) -> CallId {
|
||||||
|
let id = transcribed_call(f);
|
||||||
|
let store = &f.state.store;
|
||||||
|
store.set_status(id, CallStatus::Done, None).unwrap();
|
||||||
|
store.mark_transcribed(id, at(11, 0, 0)).unwrap();
|
||||||
|
store.set_title(id, Some("Weinbestellung")).unwrap();
|
||||||
|
f.state.mark_uploaded(id, "4711").unwrap();
|
||||||
|
fs::create_dir_all(f.dir.path().join("playback")).unwrap();
|
||||||
|
fs::write(playback_file(f, id), b"mix").unwrap();
|
||||||
|
id
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Asserts that `delete_audio` left call `id` (`mic`, `out`) untouched.
|
||||||
|
fn assert_audio_kept(f: &Fixture, id: CallId) {
|
||||||
|
let call = f.state.store.get_call(id).unwrap().unwrap();
|
||||||
|
assert!(call.mic_path.exists());
|
||||||
|
assert!(call.out_path.exists());
|
||||||
|
assert!(!call.audio_deleted);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn deleting_audio_keeps_the_transcript_title_and_upload() {
|
||||||
|
let f = fixture();
|
||||||
|
let id = transcribed_call_with_audio(&f);
|
||||||
|
let before = f.state.store.get_call(id).unwrap().unwrap();
|
||||||
|
|
||||||
|
assert_eq!(f.state.delete_audio(id), Ok(()));
|
||||||
|
|
||||||
|
let after = f.state.store.get_call(id).unwrap().unwrap();
|
||||||
|
assert!(!before.mic_path.exists());
|
||||||
|
assert!(!before.out_path.exists());
|
||||||
|
assert!(!playback_file(&f, id).exists());
|
||||||
|
assert!(after.audio_deleted);
|
||||||
|
assert_eq!(after.status, CallStatus::Done);
|
||||||
|
assert_eq!(after.title.as_deref(), Some("Weinbestellung"));
|
||||||
|
assert_eq!(after.crm_transcript_id.as_deref(), Some("4711"));
|
||||||
|
assert_eq!(after.transcribed_at, before.transcribed_at);
|
||||||
|
assert_eq!(f.state.store.segments(id).unwrap().len(), 3);
|
||||||
|
let detail = f.state.call_detail(id).unwrap();
|
||||||
|
assert!(detail.audio_deleted);
|
||||||
|
assert!(!detail.audio_available);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn audio_already_missing_is_still_marked_deleted() {
|
||||||
|
let f = fixture();
|
||||||
|
let id = transcribed_call_with_audio(&f);
|
||||||
|
let call = f.state.store.get_call(id).unwrap().unwrap();
|
||||||
|
fs::remove_file(&call.mic_path).unwrap();
|
||||||
|
fs::remove_file(&call.out_path).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(f.state.delete_audio(id), Ok(()));
|
||||||
|
assert!(f.state.store.get_call(id).unwrap().unwrap().audio_deleted);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn audio_is_not_deleted_while_the_call_is_uploaded() {
|
||||||
|
let f = fixture();
|
||||||
|
let id = transcribed_call_with_audio(&f);
|
||||||
|
// Another backend's upload, so this build can upload it again.
|
||||||
|
f.state
|
||||||
|
.store
|
||||||
|
.set_uploaded(id, "1", "https://other.example", at(12, 0, 0))
|
||||||
|
.unwrap();
|
||||||
|
let claim = f.state.claim_upload(id).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
f.state.delete_audio(id),
|
||||||
|
Err(AudioDeleteError::Busy(ALREADY_UPLOADING))
|
||||||
|
);
|
||||||
|
assert_audio_kept(&f, id);
|
||||||
|
drop(claim);
|
||||||
|
assert_eq!(f.state.delete_audio(id), Ok(()));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn audio_is_not_deleted_while_the_call_is_processed() {
|
||||||
|
let f = fixture();
|
||||||
|
let id = transcribed_call_with_audio(&f);
|
||||||
|
// A cleanup of its remote jobs reserves a call of any status.
|
||||||
|
assert!(f.state.reserve_processing(id));
|
||||||
|
assert_eq!(
|
||||||
|
f.state.delete_audio(id),
|
||||||
|
Err(AudioDeleteError::Busy(ALREADY_PROCESSING))
|
||||||
|
);
|
||||||
|
assert_audio_kept(&f, id);
|
||||||
|
f.state.release_processing(id);
|
||||||
|
|
||||||
|
f.state
|
||||||
|
.store
|
||||||
|
.set_status(id, CallStatus::Processing, None)
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
f.state.delete_audio(id),
|
||||||
|
Err(AudioDeleteError::Busy(ALREADY_PROCESSING))
|
||||||
|
);
|
||||||
|
assert_audio_kept(&f, id);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn audio_is_not_deleted_while_the_call_is_recorded() {
|
||||||
|
let f = fixture();
|
||||||
|
let dir = f.dir.path();
|
||||||
|
let (mic, out) = (dir.join("r-mic.wav"), dir.join("r-out.wav"));
|
||||||
|
fs::write(&mic, b"mic").unwrap();
|
||||||
|
fs::write(&out, b"out").unwrap();
|
||||||
|
let id = f
|
||||||
|
.state
|
||||||
|
.store
|
||||||
|
.create_call("Microsoft Teams", at(10, 0, 0), &mic, &out)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
f.state.delete_audio(id),
|
||||||
|
Err(AudioDeleteError::Busy(BEING_RECORDED))
|
||||||
|
);
|
||||||
|
assert_audio_kept(&f, id);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn audio_is_not_deleted_while_a_player_holds_its_mix() {
|
||||||
|
let f = fixture();
|
||||||
|
let id = playable_call(&f, "a");
|
||||||
|
f.state
|
||||||
|
.store
|
||||||
|
.set_status(id, CallStatus::Done, None)
|
||||||
|
.unwrap();
|
||||||
|
let lease = f.state.prepare_playback(id).unwrap().lease;
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
f.state.delete_audio(id),
|
||||||
|
Err(AudioDeleteError::Busy(PLAYING))
|
||||||
|
);
|
||||||
|
assert_audio_kept(&f, id);
|
||||||
|
assert!(playback_file(&f, id).exists());
|
||||||
|
|
||||||
|
f.state.release_playback(id, lease);
|
||||||
|
assert_eq!(f.state.delete_audio(id), Ok(()));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn audio_that_cannot_be_deleted_stays_unmarked() {
|
||||||
|
let f = fixture();
|
||||||
|
let dir = f.dir.path();
|
||||||
|
// A directory where the mic track should be: removing it fails.
|
||||||
|
let mic = dir.join("x-mic.wav");
|
||||||
|
fs::create_dir(&mic).unwrap();
|
||||||
|
let out = dir.join("x-out.wav");
|
||||||
|
fs::write(&out, b"out").unwrap();
|
||||||
|
let store = &f.state.store;
|
||||||
|
let id = store
|
||||||
|
.create_call("Microsoft Teams", at(10, 0, 0), &mic, &out)
|
||||||
|
.unwrap();
|
||||||
|
store.finish_recording(id, at(10, 5, 0), false).unwrap();
|
||||||
|
store.set_status(id, CallStatus::Done, None).unwrap();
|
||||||
|
|
||||||
|
assert!(matches!(
|
||||||
|
f.state.delete_audio(id),
|
||||||
|
Err(AudioDeleteError::Failed(_))
|
||||||
|
));
|
||||||
|
assert!(!f.state.store.get_call(id).unwrap().unwrap().audio_deleted);
|
||||||
|
// What could be deleted is gone.
|
||||||
|
assert!(!out.exists());
|
||||||
|
|
||||||
|
fs::remove_dir(&mic).unwrap();
|
||||||
|
assert_eq!(f.state.delete_audio(id), Ok(()));
|
||||||
|
assert!(f.state.store.get_call(id).unwrap().unwrap().audio_deleted);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn deleting_the_audio_of_an_unknown_call_fails() {
|
||||||
|
let f = fixture();
|
||||||
|
assert!(matches!(
|
||||||
|
f.state.delete_audio(999),
|
||||||
|
Err(AudioDeleteError::Failed(_))
|
||||||
|
));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -68,6 +68,7 @@
|
|||||||
let follow = $state(true)
|
let follow = $state(true)
|
||||||
/** For "… werden N Tage nach der Transkription gelöscht". */
|
/** For "… werden N Tage nach der Transkription gelöscht". */
|
||||||
let retentionDays = $state<number | null>(null)
|
let retentionDays = $state<number | null>(null)
|
||||||
|
let retentionPeriod = $derived(retentionDays === 1 ? '1 Tag' : `${retentionDays} Tage`)
|
||||||
|
|
||||||
let editingTitle = $state(false)
|
let editingTitle = $state(false)
|
||||||
let titleDraft = $state('')
|
let titleDraft = $state('')
|
||||||
@@ -519,7 +520,7 @@
|
|||||||
</div>
|
</div>
|
||||||
{:else if detail.audio_deleted && retentionDays !== null}
|
{:else if detail.audio_deleted && retentionDays !== null}
|
||||||
<p class="muted no-audio">
|
<p class="muted no-audio">
|
||||||
Audio nicht mehr vorhanden – Aufnahmen werden {retentionDays} Tage nach der
|
Audio nicht mehr vorhanden – Aufnahmen werden {retentionPeriod} nach der
|
||||||
Transkription gelöscht (<a href="#/settings">Einstellungen</a>).
|
Transkription gelöscht (<a href="#/settings">Einstellungen</a>).
|
||||||
</p>
|
</p>
|
||||||
{:else if detail.audio_deleted}
|
{:else if detail.audio_deleted}
|
||||||
|
|||||||
@@ -266,6 +266,13 @@
|
|||||||
settings ? delayError(settings.start_after_secs, 1, 30, 'Start nach') : '',
|
settings ? delayError(settings.start_after_secs, 1, 30, 'Start nach') : '',
|
||||||
)
|
)
|
||||||
let endError = $derived(settings ? delayError(settings.end_after_secs, 3, 120, 'Ende nach') : '')
|
let endError = $derived(settings ? delayError(settings.end_after_secs, 3, 120, 'Ende nach') : '')
|
||||||
|
/** The retention period in the hint ("7 Tage", "1 Tag"; "N Tage" while
|
||||||
|
* the field holds no valid value). */
|
||||||
|
let retentionShown = $derived.by(() => {
|
||||||
|
const days = settings?.retention_days
|
||||||
|
if (days === undefined || !Number.isInteger(days) || days < 1) return 'N Tage'
|
||||||
|
return days === 1 ? '1 Tag' : `${days} Tage`
|
||||||
|
})
|
||||||
|
|
||||||
async function persist() {
|
async function persist() {
|
||||||
if (!settings) return
|
if (!settings) return
|
||||||
@@ -479,6 +486,11 @@
|
|||||||
</select>
|
</select>
|
||||||
<label for="retention">Aufbewahrung der Audiodateien</label>
|
<label for="retention">Aufbewahrung der Audiodateien</label>
|
||||||
<span><input id="retention" type="number" min="1" bind:value={settings.retention_days} /> Tage</span>
|
<span><input id="retention" type="number" min="1" bind:value={settings.retention_days} /> Tage</span>
|
||||||
|
<span></span>
|
||||||
|
<span class="muted">
|
||||||
|
Audio wird {retentionShown} nach der Transkription gelöscht, nicht transkribierte
|
||||||
|
Aufnahmen {retentionShown} nach dem Gespräch; das Transkript bleibt.
|
||||||
|
</span>
|
||||||
<label for="autostart">Beim Anmelden starten</label>
|
<label for="autostart">Beim Anmelden starten</label>
|
||||||
<input id="autostart" type="checkbox" bind:checked={settings.autostart} />
|
<input id="autostart" type="checkbox" bind:checked={settings.autostart} />
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user