diff --git a/crates/forge-runner/src/runner.rs b/crates/forge-runner/src/runner.rs index b6494d0..fb55c49 100644 --- a/crates/forge-runner/src/runner.rs +++ b/crates/forge-runner/src/runner.rs @@ -31,7 +31,8 @@ use crate::workspace; /// Log lines queued between the executor and the log shipper. const LOG_CHANNEL: usize = 64; /// How many times the runner tries to report a finished job. -const FINISH_ATTEMPTS: u32 = 5; +/// How long a result is retried while the forge cannot be reached (a restart or a self-update). +const FINISH_PATIENCE: Duration = Duration::from_secs(10 * 60); /// Largest single artifact the runner will upload. The forge enforces its own total cap. const MAX_ARTIFACT_BYTES: u64 = 512 * 1024 * 1024; /// How long a stopping runner waits for its last log lines to reach the forge. @@ -479,6 +480,13 @@ impl Runner { drop(registration); drop(tx); let _ = pump.await; + // The workspace is named after the job, so it goes before the job is handed back: a + // free slot here may claim it again at once and build in the same folder. + if let Some(dir) = workspace_dir.take() + && let Err(e) = workspace::cleanup(&dir).await + { + tracing::warn!(job_id = %job.job_id, error = %e, "workspace cleanup failed"); + } let reason = "killed under memory pressure on the runner"; if let Err(e) = self .client @@ -495,11 +503,6 @@ impl Runner { }; self.finish(&job, &failed, None).await; } - if let Some(dir) = workspace_dir - && let Err(e) = workspace::cleanup(&dir).await - { - tracing::warn!(job_id = %job.job_id, error = %e, "workspace cleanup failed"); - } tracing::info!(job_id = %job.job_id, "job requeued after memory pressure"); return Outcome::Cancelled; } @@ -632,12 +635,26 @@ impl Runner { oom_killed: oom_kills.map(|_| true), oom_kills, }; - for attempt in 1..=FINISH_ATTEMPTS { + // A forge that is down or restarting is waited for; a forge that refuses the result + // (the job was reaped, cancelled or its token ended) is not asked again. + 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"); - tokio::time::sleep(Duration::from_secs(2 * u64::from(attempt))).await; + let away = match &e { + ClientError::Http(_) => true, + ClientError::Status { status, .. } => status.is_server_error(), + _ => false, + }; + if (!away && attempt >= 3) || started.elapsed() >= FINISH_PATIENCE { + break; + } + let wait = Duration::from_secs((2 * u64::from(attempt)).min(15)); + tokio::time::sleep(wait).await; } } } diff --git a/crates/forge-web/src/ci/reaper.rs b/crates/forge-web/src/ci/reaper.rs index a60bb2f..7c88bec 100644 --- a/crates/forge-web/src/ci/reaper.rs +++ b/crates/forge-web/src/ci/reaper.rs @@ -37,9 +37,15 @@ async fn run(state: AppState) { let mut next_purge = Instant::now(); let mut next_slow_alerts = Instant::now() + ALERTS_SLOW_EVERY; let mut next_health = Instant::now() + HEALTH_EVERY; + // After a restart or a self-update, every running job's last heartbeat is as old as the + // downtime. Its runner kept working and beats again within seconds, so nothing is reaped + // until a full LOST_AFTER has passed since boot. + let booted = Instant::now(); loop { tick.tick().await; - reap_once(&state).await; + if booted.elapsed() >= LOST_AFTER { + reap_once(&state).await; + } crate::alerts::check_runners(&state).await; if Instant::now() >= next_health { crate::live::admin().bump(&crate::live::Topic::AdminHealth); diff --git a/docs/ci.md b/docs/ci.md index de5b16c..73c51f5 100644 --- a/docs/ci.md +++ b/docs/ci.md @@ -622,6 +622,11 @@ jobs are running, the runner stops claiming, replaces its binary, and exits with restarts it. It is skipped when the forge's binary is for a different OS or architecture. Set `auto_update = false` to pin the binary by hand. +A forge restart or self-update does not stop running jobs: they keep running on their runners, which +keep their logs and retry heartbeats until the forge answers again. A job that finishes while the +forge is away keeps offering its result for up to 10 minutes. After it boots, the forge waits two +minutes before it marks any job lost, so runners have time to report in. + `concurrency` is the number of jobs run in parallel. The default is 1. An admin can change it per runner on the runners page (`/admin/runners`) with no restart: type a number from 1 to 64 and save. The runner picks it up on its next claim or job heartbeat, grows or shrinks its job slots, and logs