diff --git a/crates/forge-runner/src/runner.rs b/crates/forge-runner/src/runner.rs index fe172cf..4f9e1ca 100644 --- a/crates/forge-runner/src/runner.rs +++ b/crates/forge-runner/src/runner.rs @@ -24,7 +24,7 @@ use crate::update::{self, Installed, UpdateState}; use crate::workspace; const LOG_CHANNEL: usize = 64; -const FINISH_PATIENCE: Duration = Duration::from_secs(10 * 60); +const FORGE_PATIENCE: Duration = Duration::from_secs(10 * 60); const MAX_ARTIFACT_BYTES: u64 = 512 * 1024 * 1024; const LOG_FLUSH: Duration = Duration::from_secs(5); @@ -457,10 +457,12 @@ impl Runner { let found = artifacts::collect(dir, &job.artifacts, MAX_ARTIFACT_BYTES) .map_err(|e| format!("artifacts: {e}"))?; for artifact in &found { - self.client - .upload_artifact(&job.job_id, &job.job_token, &artifact.name, &artifact.path) - .await - .map_err(|e| format!("artifact {}: {e}", artifact.name))?; + patiently(&job.job_id, "artifact upload", || { + self.client + .upload_artifact(&job.job_id, &job.job_token, &artifact.name, &artifact.path) + }) + .await + .map_err(|e| format!("artifact {}: {e}", artifact.name))?; } tracing::info!(job_id = %job.job_id, count = found.len(), "artifacts uploaded"); Ok(()) @@ -474,26 +476,12 @@ impl Runner { oom_killed: oom_kills.map(|_| true), oom_kills, }; - let started = Instant::now(); - let mut attempt = 0u32; - loop { - attempt += 1; - match self.client.finish(&job.job_id, &job.job_token, &req).await { - Ok(()) => return, - Err(e) => { - tracing::warn!(job_id = %job.job_id, attempt, error = %e, "finish failed"); - let away = match &e { - ClientError::Http(_) => true, - ClientError::Status { status, .. } => status.is_server_error(), - _ => false, - }; - if !away || started.elapsed() >= FINISH_PATIENCE { - break; - } - let wait = Duration::from_secs((2 * u64::from(attempt)).min(15)); - tokio::time::sleep(wait).await; - } - } + let reported = patiently(&job.job_id, "finish", || { + self.client.finish(&job.job_id, &job.job_token, &req) + }) + .await; + if reported.is_ok() { + return; } tracing::error!( job_id = %job.job_id, @@ -502,6 +490,31 @@ impl Runner { } } +async fn patiently(job_id: &str, what: &str, mut op: F) -> Result<(), ClientError> +where + F: FnMut() -> Fut, + Fut: std::future::Future>, +{ + let started = Instant::now(); + let mut attempt = 0u32; + loop { + attempt += 1; + let Err(e) = op().await else { + return Ok(()); + }; + tracing::warn!(job_id, attempt, error = %e, "{what} failed"); + let away = match &e { + ClientError::Http(_) => true, + ClientError::Status { status, .. } => status.is_server_error(), + _ => false, + }; + if !away || started.elapsed() >= FORGE_PATIENCE { + return Err(e); + } + tokio::time::sleep(Duration::from_secs((2 * u64::from(attempt)).min(15))).await; + } +} + fn not_claiming(last: &mut Option, reason: String) { if last.as_deref() != Some(reason.as_str()) { tracing::info!("not claiming: {reason}");