feat #14740: retention planning and startup recovery
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -26,6 +26,20 @@ pub enum StoreError {
|
||||
|
||||
pub type Result<T> = std::result::Result<T, StoreError>;
|
||||
|
||||
/// Error text stored on calls that were mid-processing when the app last
|
||||
/// shut down (or crashed).
|
||||
const RECOVERY_ERROR: &str = "Verarbeitung unterbrochen – bitte erneut versuchen";
|
||||
|
||||
/// What [`Store::startup_recovery`] did to reconcile call states left over
|
||||
/// from an unclean shutdown.
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub struct RecoveryReport {
|
||||
/// Calls moved from `Recording` to `AwaitingDecision`.
|
||||
pub interrupted: Vec<CallId>,
|
||||
/// Calls moved from `Processing` to `Failed`.
|
||||
pub failed: Vec<CallId>,
|
||||
}
|
||||
|
||||
const SCHEMA_SQL: &str = "
|
||||
CREATE TABLE IF NOT EXISTS calls (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
@@ -340,6 +354,61 @@ impl Store {
|
||||
conn.execute("DELETE FROM remote_jobs WHERE call_id = ?1", params![id])?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Reconciles call states left over from an unclean shutdown (the app
|
||||
/// crashed or was killed mid-recording or mid-processing).
|
||||
///
|
||||
/// - `Recording` calls were still being recorded: they move to
|
||||
/// `AwaitingDecision`, with `interrupted` set and `ended_at` set to now
|
||||
/// if it was not already set (a later pass corrects it from the WAV
|
||||
/// length).
|
||||
/// - `Processing` calls were mid-transcription/summarization: they move
|
||||
/// to `Failed` with a fixed error message.
|
||||
///
|
||||
/// Both updates run in a single transaction.
|
||||
pub fn startup_recovery(&self) -> Result<RecoveryReport> {
|
||||
let now = Local::now();
|
||||
let mut conn = self.conn.lock().unwrap();
|
||||
let tx = conn.transaction()?;
|
||||
|
||||
let interrupted = ids_with_status(&tx, CallStatus::Recording)?;
|
||||
for id in &interrupted {
|
||||
tx.execute(
|
||||
"UPDATE calls SET status = ?1, interrupted = 1, ended_at = COALESCE(ended_at, ?2) \
|
||||
WHERE id = ?3",
|
||||
params![
|
||||
status_to_str(CallStatus::AwaitingDecision),
|
||||
dt_to_str(now),
|
||||
id
|
||||
],
|
||||
)?;
|
||||
}
|
||||
|
||||
let failed = ids_with_status(&tx, CallStatus::Processing)?;
|
||||
for id in &failed {
|
||||
tx.execute(
|
||||
"UPDATE calls SET status = ?1, error = ?2 WHERE id = ?3",
|
||||
params![status_to_str(CallStatus::Failed), RECOVERY_ERROR, id],
|
||||
)?;
|
||||
}
|
||||
|
||||
tx.commit()?;
|
||||
Ok(RecoveryReport {
|
||||
interrupted,
|
||||
failed,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Ids of calls currently in `status`, ordered by id (insertion order).
|
||||
fn ids_with_status(conn: &rusqlite::Connection, status: CallStatus) -> Result<Vec<CallId>> {
|
||||
let mut stmt = conn.prepare("SELECT id FROM calls WHERE status = ?1 ORDER BY id ASC")?;
|
||||
let mut rows = stmt.query(params![status_to_str(status)])?;
|
||||
let mut ids = Vec::new();
|
||||
while let Some(row) = rows.next()? {
|
||||
ids.push(row.get(0)?);
|
||||
}
|
||||
Ok(ids)
|
||||
}
|
||||
|
||||
const SELECT_ALL_CALLS_SQL: &str = "SELECT id, app, started_at, ended_at, status, interrupted, error, mic_path, out_path, transcribed_at, audio_deleted, account_id FROM calls";
|
||||
@@ -605,6 +674,76 @@ mod tests {
|
||||
assert_eq!(ids, vec![id3, id2, id1]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn startup_recovery_marks_states() {
|
||||
let store = Store::open_in_memory().unwrap();
|
||||
let started = Local::now();
|
||||
|
||||
let recording_id = store
|
||||
.create_call(
|
||||
"teams.exe",
|
||||
started,
|
||||
Path::new("/tmp/mic1.wav"),
|
||||
Path::new("/tmp/out1.wav"),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let processing_id = store
|
||||
.create_call(
|
||||
"chrome.exe",
|
||||
started,
|
||||
Path::new("/tmp/mic2.wav"),
|
||||
Path::new("/tmp/out2.wav"),
|
||||
)
|
||||
.unwrap();
|
||||
store
|
||||
.set_status(processing_id, CallStatus::Processing, None)
|
||||
.unwrap();
|
||||
|
||||
let before = Local::now();
|
||||
let report = store.startup_recovery().unwrap();
|
||||
let after = Local::now();
|
||||
|
||||
assert_eq!(report.interrupted, vec![recording_id]);
|
||||
assert_eq!(report.failed, vec![processing_id]);
|
||||
|
||||
let recovered = store.get_call(recording_id).unwrap().unwrap();
|
||||
assert_eq!(recovered.status, CallStatus::AwaitingDecision);
|
||||
assert!(recovered.interrupted);
|
||||
let ended_at = recovered.ended_at.expect("ended_at set by recovery");
|
||||
assert!(ended_at >= before && ended_at <= after);
|
||||
|
||||
let failed_call = store.get_call(processing_id).unwrap().unwrap();
|
||||
assert_eq!(failed_call.status, CallStatus::Failed);
|
||||
assert_eq!(
|
||||
failed_call.error.as_deref(),
|
||||
Some("Verarbeitung unterbrochen – bitte erneut versuchen")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn startup_recovery_keeps_existing_ended_at() {
|
||||
let store = Store::open_in_memory().unwrap();
|
||||
let started = Local::now();
|
||||
let id = store
|
||||
.create_call(
|
||||
"teams.exe",
|
||||
started,
|
||||
Path::new("/tmp/mic.wav"),
|
||||
Path::new("/tmp/out.wav"),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let original_ended_at = started + chrono::Duration::minutes(5);
|
||||
store.set_ended_at(id, original_ended_at).unwrap();
|
||||
|
||||
let report = store.startup_recovery().unwrap();
|
||||
assert_eq!(report.interrupted, vec![id]);
|
||||
|
||||
let recovered = store.get_call(id).unwrap().unwrap();
|
||||
assert_eq!(recovered.ended_at, Some(original_ended_at));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delete_call_cascades() {
|
||||
let store = Store::open_in_memory().unwrap();
|
||||
|
||||
Reference in New Issue
Block a user