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:
@@ -20,6 +20,9 @@ use super::{TranscribeError, TranscribeOptions, Transcriber};
|
||||
const DEFAULT_BASE_URL: &str = "https://api.deepscript.com/v1";
|
||||
const DEFAULT_POLL_INTERVAL: Duration = Duration::from_secs(5);
|
||||
const DEFAULT_MAX_WAIT: Duration = Duration::from_secs(60 * 60);
|
||||
/// How many status polls in a row may fail transiently (network, timeout,
|
||||
/// HTTP 429/5xx) before `wait` gives up.
|
||||
const MAX_POLL_RETRIES: u32 = 3;
|
||||
|
||||
/// DeepScript client. Build with [`DeepScript::new`]; the `with_*` builders
|
||||
/// exist so tests can shrink `poll_interval`/`max_wait` and point
|
||||
@@ -73,12 +76,37 @@ impl DeepScript {
|
||||
/// A non-2xx response is *not* an error here; callers inspect
|
||||
/// `resp.status` themselves.
|
||||
async fn send(&self, req: HttpRequest) -> Result<HttpResponse, TranscribeError> {
|
||||
self.transport.send(req).await.map_err(|e| match e {
|
||||
TransportError::Timeout => TranscribeError::Timeout,
|
||||
TransportError::Io(msg) | TransportError::Network(msg) => {
|
||||
TranscribeError::Transport(msg)
|
||||
self.transport.send(req).await.map_err(transport_error)
|
||||
}
|
||||
|
||||
/// Asks for the status of `remote_id` once.
|
||||
async fn poll_status(&self, remote_id: &str) -> Result<String, PollError> {
|
||||
let req = HttpRequest {
|
||||
method: Method::Get,
|
||||
url: format!("{}/transcriptions/{remote_id}/status", self.base_url),
|
||||
headers: vec![self.auth_header()],
|
||||
body: HttpBody::Empty,
|
||||
};
|
||||
let resp = match self.transport.send(req).await {
|
||||
Ok(resp) => resp,
|
||||
Err(e @ (TransportError::Network(_) | TransportError::Timeout)) => {
|
||||
return Err(PollError::Transient(transport_error(e)))
|
||||
}
|
||||
})
|
||||
Err(e) => return Err(PollError::Fatal(transport_error(e))),
|
||||
};
|
||||
match resp.status {
|
||||
200 => resp
|
||||
.json::<DataEnvelope<StatusData>>()
|
||||
.map(|envelope| envelope.data.status)
|
||||
.map_err(|e| {
|
||||
PollError::Fatal(TranscribeError::Failed(format!("ungültige Antwort: {e}")))
|
||||
}),
|
||||
429 | 500..=599 => Err(PollError::Transient(error_for_status(
|
||||
resp.status,
|
||||
&resp.body,
|
||||
))),
|
||||
other => Err(PollError::Fatal(error_for_status(other, &resp.body))),
|
||||
}
|
||||
}
|
||||
|
||||
async fn fetch_detail(&self, remote_id: &str) -> Result<DetailData, TranscribeError> {
|
||||
@@ -191,26 +219,20 @@ impl Transcriber for DeepScript {
|
||||
}
|
||||
}
|
||||
|
||||
/// Polls until the job is done. A poll that fails transiently is retried
|
||||
/// at the normal interval, up to [`MAX_POLL_RETRIES`] times in a row.
|
||||
async fn wait(&self, remote_id: &str) -> Result<Vec<Word>, TranscribeError> {
|
||||
let deadline = Instant::now() + self.max_wait;
|
||||
let mut failed_polls = 0;
|
||||
loop {
|
||||
if Instant::now() >= deadline {
|
||||
return Err(TranscribeError::Timeout);
|
||||
}
|
||||
|
||||
let req = HttpRequest {
|
||||
method: Method::Get,
|
||||
url: format!("{}/transcriptions/{remote_id}/status", self.base_url),
|
||||
headers: vec![self.auth_header()],
|
||||
body: HttpBody::Empty,
|
||||
};
|
||||
let resp = self.send(req).await?;
|
||||
match resp.status {
|
||||
200 => {
|
||||
let envelope: DataEnvelope<StatusData> = resp
|
||||
.json()
|
||||
.map_err(|e| TranscribeError::Failed(format!("ungültige Antwort: {e}")))?;
|
||||
match envelope.data.status.as_str() {
|
||||
match self.poll_status(remote_id).await {
|
||||
Ok(status) => {
|
||||
failed_polls = 0;
|
||||
match status.as_str() {
|
||||
"completed" => return self.collect_words(remote_id).await,
|
||||
"failed" => return self.collect_failure(remote_id).await,
|
||||
"queued" | "processing" => {}
|
||||
@@ -223,7 +245,14 @@ impl Transcriber for DeepScript {
|
||||
}
|
||||
}
|
||||
}
|
||||
other => return Err(error_for_status(other, &resp.body)),
|
||||
Err(PollError::Transient(e)) if failed_polls < MAX_POLL_RETRIES => {
|
||||
failed_polls += 1;
|
||||
log::warn!(
|
||||
"DeepScript: Statusabfrage für {remote_id} fehlgeschlagen ({}), Versuch {failed_polls} von {MAX_POLL_RETRIES}",
|
||||
e.user_message()
|
||||
);
|
||||
}
|
||||
Err(PollError::Transient(e) | PollError::Fatal(e)) => return Err(e),
|
||||
}
|
||||
|
||||
tokio::time::sleep(self.poll_interval).await;
|
||||
@@ -245,6 +274,20 @@ impl Transcriber for DeepScript {
|
||||
}
|
||||
}
|
||||
|
||||
/// A failed status poll: `Transient` ones (network, timeout, HTTP 429/5xx)
|
||||
/// are worth retrying.
|
||||
enum PollError {
|
||||
Transient(TranscribeError),
|
||||
Fatal(TranscribeError),
|
||||
}
|
||||
|
||||
fn transport_error(e: TransportError) -> TranscribeError {
|
||||
match e {
|
||||
TransportError::Timeout => TranscribeError::Timeout,
|
||||
TransportError::Io(msg) | TransportError::Network(msg) => TranscribeError::Transport(msg),
|
||||
}
|
||||
}
|
||||
|
||||
/// Maps a non-2xx status that isn't handled specially by the caller (401,
|
||||
/// 429, and a generic fallback carrying the problem body's `detail`/`title`
|
||||
/// — the set shared by every DeepScript endpoint).
|
||||
@@ -600,6 +643,72 @@ mod tests {
|
||||
transport.assert_done();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn wait_retries_a_transient_status_error() {
|
||||
let transport = Arc::new(FakeTransport::new());
|
||||
let status_url = format!("{BASE}/transcriptions/job-8/status");
|
||||
transport.expect(Method::Get, &status_url, 503, "");
|
||||
transport.expect(
|
||||
Method::Get,
|
||||
&status_url,
|
||||
200,
|
||||
r#"{"data":{"id":"job-8","status":"completed"}}"#,
|
||||
);
|
||||
transport.expect(
|
||||
Method::Get,
|
||||
&format!("{BASE}/transcriptions/job-8"),
|
||||
200,
|
||||
r#"{"data":{"id":"job-8","status":"completed","resultJson":{"words":[]}}}"#,
|
||||
);
|
||||
let ds = deep_script(&transport);
|
||||
|
||||
let words = ds.wait("job-8").await.unwrap();
|
||||
|
||||
assert_eq!(words, Vec::<Word>::new());
|
||||
transport.assert_done();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn wait_gives_up_after_four_transient_errors_in_a_row() {
|
||||
let transport = Arc::new(FakeTransport::new());
|
||||
let status_url = format!("{BASE}/transcriptions/job-9/status");
|
||||
// A successful poll in between resets the count.
|
||||
for status in [503, 429, 200, 502, 503, 500, 503] {
|
||||
let body = if status == 200 {
|
||||
r#"{"data":{"id":"job-9","status":"processing"}}"#
|
||||
} else {
|
||||
r#"{"title":"Service Unavailable"}"#
|
||||
};
|
||||
transport.expect(Method::Get, &status_url, status, body);
|
||||
}
|
||||
let ds = deep_script(&transport);
|
||||
|
||||
let err = ds.wait("job-9").await.unwrap_err();
|
||||
|
||||
assert_eq!(
|
||||
err,
|
||||
TranscribeError::Failed("HTTP 503: Service Unavailable".to_string())
|
||||
);
|
||||
transport.assert_done();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn wait_does_not_retry_a_client_error() {
|
||||
let transport = Arc::new(FakeTransport::new());
|
||||
transport.expect(
|
||||
Method::Get,
|
||||
&format!("{BASE}/transcriptions/job-10/status"),
|
||||
401,
|
||||
"",
|
||||
);
|
||||
let ds = deep_script(&transport);
|
||||
|
||||
let err = ds.wait("job-10").await.unwrap_err();
|
||||
|
||||
assert_eq!(err, TranscribeError::Unauthorized);
|
||||
transport.assert_done();
|
||||
}
|
||||
|
||||
#[tokio::test(start_paused = true)]
|
||||
async fn wait_times_out_after_max_wait() {
|
||||
let transport = Arc::new(FakeTransport::new());
|
||||
|
||||
Reference in New Issue
Block a user