//! The runner loop: claim a job, run it, ship its logs and artifacts, and report the result. use std::sync::Arc; use std::sync::atomic::{AtomicBool, Ordering}; use std::time::Duration; use forge_ci::mask::{MaskStream, Masker}; use forge_ci::proto::{FinishRequest, JobAssignment, Outcome}; use tokio::sync::mpsc; use tokio::task::{JoinHandle, JoinSet}; use tokio::time::Instant; use tokio_util::sync::CancellationToken; use crate::artifacts; use crate::cache::CacheStore; use crate::client::MemoryClaim; use crate::client::{ClientError, ForgeClient}; use crate::config::RunnerConfig; use crate::executor::{ExecRequest, ExecResult, Executor}; use crate::limiter::Limiter; use crate::logs::{self, PumpConfig}; use crate::memory::MemoryGate; use crate::oom::OomSink; use crate::pressure::Pressure; use crate::restart::{self, RestartState, Step}; use crate::update::{self, Installed, UpdateState}; use crate::workspace; /// Log lines queued between the executor and the log shipper. const LOG_CHANNEL: usize = 256; /// How many times the runner tries to report a finished job. const FINISH_ATTEMPTS: u32 = 5; /// Largest single artifact the runner will upload. The forge enforces its own total cap. const MAX_ARTIFACT_BYTES: u64 = 512 * 1024 * 1024; /// Timing knobs. Production uses the defaults. Tests shorten them. #[derive(Clone, Copy, Debug)] pub struct RunOptions { pub heartbeat_every: Duration, pub claim_backoff: Duration, pub pump: PumpConfig, /// When set, replaces `timeout_minutes` from the job. pub timeout_override: Option, } impl Default for RunOptions { fn default() -> Self { Self { heartbeat_every: Duration::from_secs(10), claim_backoff: Duration::from_secs(5), pump: PumpConfig::default(), timeout_override: None, } } } /// How [`Runner::run`] ended. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum RunEnd { /// Shutdown was requested. Stopped, /// The binary was replaced. The caller exits with [`update::RESTART_EXIT_CODE`]. Updated, /// An admin asked for a restart and the jobs are done. The caller exits with /// [`update::RESTART_EXIT_CODE`], like for an update. Restart, } /// A runner bound to one forge and one executor. #[derive(Clone)] pub struct Runner { client: ForgeClient, config: Arc, executor: Arc, options: RunOptions, /// The build cache, when it is on. See [`CacheStore`]. cache: Option>, /// The live job limit. Starts at `runner.toml` and follows the forge's override. limiter: Arc, /// The memory budget and the running jobs' reservations, see [`crate::memory`]. memory: Arc, /// A restart an admin asked for, see [`restart`]. restart: Arc, /// The memory box and its controller, when the host allows it, see [`crate::pressure`]. pressure: Option>, } impl Runner { pub fn new( config: RunnerConfig, executor: Arc, options: RunOptions, ) -> Result { let client = ForgeClient::new(&config.url)?; let limiter = Limiter::new(config.concurrency); let memory = MemoryGate::for_host(&config.limits); Ok(Self { client, config: Arc::new(config), executor, options, cache: None, limiter, memory, restart: Arc::new(RestartState::default()), pressure: None, }) } /// Turns on reactive memory control: jobs are paused under pressure, and no job is claimed /// while one is parked. pub fn with_pressure(mut self, pressure: Option>) -> Self { self.pressure = pressure; self } /// Turns on the build cache for every job this runner runs. pub fn with_cache(mut self, cache: CacheStore) -> Self { self.cache = Some(Arc::new(cache)); self } pub fn client(&self) -> &ForgeClient { &self.client } /// Claims and runs jobs, up to `concurrency` at a time, until `shutdown` is cancelled. Jobs /// still running at shutdown are cancelled and reported as cancelled. /// /// When the forge advertises a different binary and `auto_update` is on, no new jobs are /// claimed. Once the running jobs finish, the binary is replaced and the loop returns /// [`RunEnd::Updated`]. pub async fn run(&self, shutdown: CancellationToken) -> RunEnd { let mut jobs: JoinSet<()> = JoinSet::new(); // Hashed once at startup. Without it, or with auto_update off, updates stay disabled. let exe = if self.config.auto_update { match update::locate_self() { Ok(found) => Some(found), Err(e) => { tracing::warn!(error = %e, "auto update disabled, cannot read own binary"); None } } } else { None }; let mut updates = UpdateState::new( self.config.auto_update, exe.as_ref().map(|(_, sha)| sha.clone()), ); tracing::info!( name = %self.config.name, concurrency = self.limiter.current(), memory_budget = %forge_ci::size::format_size(self.memory.budget()), reserve_memory = %forge_ci::size::format_size(self.memory.default_reserve()), auto_update = updates.enabled(), "runner started" ); // Parent of every job's token, so a `now` restart can cancel them all at once. let jobs_cancel = shutdown.child_token(); let end = loop { match restart::decide( self.restart.pending(), jobs.len(), self.restart.jobs_cancelled(), ) { Step::Proceed => {} Step::Restart => { tracing::info!("restart requested by the forge, restarting"); break RunEnd::Restart; } Step::CancelJobs => { tracing::info!("restart now requested, cancelling running jobs"); self.restart.mark_cancelled(); jobs_cancel.cancel(); continue; } Step::WaitForJobs => { tokio::select! { _ = shutdown.cancelled() => break RunEnd::Stopped, Some(joined) = jobs.join_next() => { if let Err(e) = joined { tracing::error!(error = %e, "job task panicked"); } } () = self.restart.changed() => {} } continue; } } if let Some((exe_path, _)) = &exe && let Some(sha) = updates.pending().map(str::to_owned) { if !jobs.is_empty() { // Claiming is paused until the running jobs finish. tokio::select! { _ = shutdown.cancelled() => break RunEnd::Stopped, Some(joined) = jobs.join_next() => { if let Err(e) = joined { tracing::error!(error = %e, "job task panicked"); } } } continue; } match update::install(&self.client, &self.config.runner_secret, exe_path, &sha) .await { Ok(Installed::Replaced) => { tracing::info!(sha = %sha, "binary updated, restarting"); break RunEnd::Updated; } Ok(Installed::Listed) => { tracing::warn!(sha = %sha, "forge binary is on the bad list, not updating"); updates.give_up(&sha); } Err(e) => { tracing::warn!(sha = %sha, error = %e, "update failed, keeping this binary"); updates.give_up(&sha); } } continue; } if let Some(pressure) = &self.pressure { // A parked job or a box under pressure means no new job. The controller wakes // this when that changes. tokio::select! { _ = shutdown.cancelled() => break RunEnd::Stopped, () = self.restart.changed() => continue, () = pressure.wait_open() => {} } } let permit = tokio::select! { _ = shutdown.cancelled() => break RunEnd::Stopped, () = self.restart.changed() => continue, permit = self.limiter.acquire() => match permit { Ok(permit) => permit, Err(_) => break RunEnd::Stopped, }, }; let claimed = tokio::select! { _ = shutdown.cancelled() => { drop(permit); break RunEnd::Stopped; } r = self.client.claim_with( &self.config.runner_secret, &self.config.labels, Some(self.limiter.current()), Some(MemoryClaim { // Real room in the box when memory control is on, else what is promised. free: self .pressure .as_ref() .and_then(|p| p.headroom()) .unwrap_or_else(|| self.memory.free()), budget: self.memory.budget(), reserve: self.memory.default_reserve(), }), ) => r, }; match claimed { Ok(reply) => { // No header means no override: back to the runner.toml value. self.limiter.apply_logged(reply.concurrency); self.memory.apply_logged( reply.memory_budget.as_deref(), reply.reserve_memory.as_deref(), ); if let Some(pressure) = &self.pressure && pressure.budget() != self.memory.budget() { let pressure = pressure.clone(); let budget = self.memory.budget(); let _ = tokio::task::spawn_blocking(move || pressure.resize(budget)).await; } self.restart.observe(reply.restart); if updates.observe(reply.binary_sha256.as_deref()) && let Some(sha) = updates.pending() { tracing::info!(sha = %sha, "forge advertises a new binary, updating when idle"); } match reply.job { Some(job) => { tracing::info!( job_id = %job.job_id, job_name = %job.job_name, repo = %job.repo, "claimed job" ); let runner = self.clone(); let cancel = jobs_cancel.child_token(); // Reserved before the next claim, so that claim sees less free memory. let reservation = self.memory.reserve(job.memory_bytes); jobs.spawn(async move { let _permit = permit; let _reservation = reservation; runner.process(job, cancel).await; }); } None => drop(permit), } } Err(e) => { drop(permit); tracing::warn!(error = %e, "claim failed, backing off"); tokio::select! { _ = shutdown.cancelled() => break RunEnd::Stopped, _ = tokio::time::sleep(self.options.claim_backoff) => {} } } } while let Some(joined) = jobs.try_join_next() { if let Err(e) = joined { tracing::error!(error = %e, "job task panicked"); } } }; while let Some(joined) = jobs.join_next().await { if let Err(e) = joined { tracing::error!(error = %e, "job task panicked"); } } tracing::info!("runner stopped"); end } /// Runs one claimed job end to end and reports its result. Returns the final outcome. pub async fn process(&self, job: JobAssignment, shutdown: CancellationToken) -> Outcome { // Mask the job's secret values and its own token, both raw and base64 (done by Masker). let mut secret_values: Vec = job.secrets.values().cloned().collect(); secret_values.push(job.job_token.clone()); let masker = Masker::new(secret_values); let (tx, rx) = mpsc::channel::>(LOG_CHANNEL); let pump = { let client = self.client.clone(); let job_id = job.job_id.clone(); let token = job.job_token.clone(); let cfg = self.options.pump; tokio::spawn(async move { logs::pump(&client, &job_id, &token, MaskStream::new(masker), rx, cfg).await }) }; send_line( &tx, format!( "forge-runner {}: job {} on runner {}, image {}, commit {}", forge_ci::build::display(), job.job_name, self.config.name, job.image, job.commit ), ) .await; let timeout = self .options .timeout_override .unwrap_or_else(|| Duration::from_secs(60 * u64::from(job.timeout_minutes))); let job_cancel = shutdown.child_token(); let timed_out = Arc::new(AtomicBool::new(false)); let heartbeat = self.spawn_heartbeat(&job, job_cancel.clone()); let registration = self .pressure .as_ref() .map(|p| p.register(&job.job_id, &job.job_name, tx.clone())); let timer = { let cancel = job_cancel.clone(); let flag = timed_out.clone(); let pause = registration.as_ref().map(|r| r.pause_clock()); tokio::spawn(async move { // Time spent frozen does not count, so the deadline moves out by the pauses. let started = Instant::now(); loop { let paused = pause.as_ref().map(|p| p.paused()).unwrap_or_default(); let due = timeout + paused; let elapsed = started.elapsed(); if elapsed >= due { break; } tokio::time::sleep(due - elapsed).await; } flag.store(true, Ordering::SeqCst); cancel.cancel(); }) }; let mut workspace_dir = None; let oom = OomSink::default(); let prepared = workspace::prepare(&self.config.work_dir, &job, &self.config.url).await; let mut result = match prepared { Ok(dir) => { if let Some(cache) = &self.cache { for line in cache.restore(&job, &dir).await { send_line(&tx, line).await; } } let result = self .executor .run(ExecRequest { job: &job, workspace: &dir, logs: tx.clone(), cancel: job_cancel.clone(), oom: oom.clone(), }) .await; workspace_dir = Some(dir); result } Err(e) => ExecResult { outcome: Outcome::Error, exit_code: None, message: Some(format!("workspace: {e}")), }, }; // The memory controller killed this job to run it again later: give it back to the queue // without a result. if registration.as_ref().is_some_and(|r| r.requeued()) { heartbeat.abort(); timer.abort(); drop(registration); drop(tx); let _ = pump.await; let reason = "killed under memory pressure on the runner"; if let Err(e) = self .client .requeue(&job.job_id, &job.job_token, reason) .await { tracing::warn!(job_id = %job.job_id, error = %e, "requeue failed, failing the job"); let failed = ExecResult { outcome: Outcome::Error, exit_code: None, message: Some(format!( "out of memory, and the forge could not requeue it: {e}" )), }; 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; } // Artifacts only come from a successful run. let upload_error = match workspace_dir.as_deref() { Some(dir) if result.outcome == Outcome::Success && !job.artifacts.is_empty() => { self.upload_artifacts(&job, dir).await.err() } _ => None, }; if let Some(message) = upload_error { result = ExecResult { outcome: Outcome::Error, exit_code: result.exit_code, message: Some(message), }; } heartbeat.abort(); timer.abort(); // The log sender it holds must go before the log shipper can finish. drop(registration); if timed_out.load(Ordering::SeqCst) && result.outcome == Outcome::Cancelled { result = ExecResult { outcome: Outcome::Failure, exit_code: None, message: Some(format!("timed out after {} minutes", job.timeout_minutes)), }; } if self.restart.jobs_cancelled() && result.outcome == Outcome::Cancelled { result.message = Some("cancelled because an admin restarted the runner".to_owned()); } // The save decides on the final outcome. It also clears the job's scratch dirs. if let (Some(cache), Some(dir)) = (&self.cache, workspace_dir.as_deref()) { for line in cache.save(&job, result.outcome, dir).await { send_line(&tx, line).await; } } if let Some(message) = &result.message { send_line(&tx, format!("runner: {message}")).await; } let oom_kills = oom.kills(); if let Some(kills) = oom_kills { send_line( &tx, format!( "runner: the out of memory killer killed {kills} process(es) in this job; \ the job's memory is limited to its `memory` plus a little slack, see docs/ci.md" ), ) .await; } drop(tx); match pump.await { Ok(Ok(bytes)) => tracing::debug!(job_id = %job.job_id, bytes, "logs shipped"), Ok(Err(e)) => tracing::warn!(job_id = %job.job_id, error = %e, "log shipping failed"), Err(e) => tracing::error!(job_id = %job.job_id, error = %e, "log task panicked"), } self.finish(&job, &result, oom_kills).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, outcome = ?result.outcome, "job finished"); result.outcome } fn spawn_heartbeat(&self, job: &JobAssignment, cancel: CancellationToken) -> JoinHandle<()> { let client = self.client.clone(); let limiter = self.limiter.clone(); let restart = self.restart.clone(); let job_id = job.job_id.clone(); let token = job.job_token.clone(); let every = self.options.heartbeat_every; tokio::spawn(async move { let mut ticker = tokio::time::interval_at(Instant::now() + every, every); loop { ticker.tick().await; match client .heartbeat_with(&job_id, &token, Some(limiter.current())) .await { Ok(beat) if beat.cancel => { limiter.apply_logged(beat.concurrency); restart.observe(beat.restart); tracing::info!(job_id = %job_id, "forge requested cancel"); cancel.cancel(); return; } // A runner at its limit does not claim, so the heartbeat carries changes too. Ok(beat) => { limiter.apply_logged(beat.concurrency); restart.observe(beat.restart); } Err(e) => tracing::warn!(job_id = %job_id, error = %e, "heartbeat failed"), } } }) } /// Collects the job's artifacts and uploads them. The error is a message for the job record. async fn upload_artifacts( &self, job: &JobAssignment, dir: &std::path::Path, ) -> Result<(), String> { 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))?; } tracing::info!(job_id = %job.job_id, count = found.len(), "artifacts uploaded"); Ok(()) } async fn finish(&self, job: &JobAssignment, result: &ExecResult, oom_kills: Option) { let req = FinishRequest { outcome: result.outcome, exit_code: result.exit_code, message: result.message.clone(), oom_killed: oom_kills.map(|_| true), oom_kills, }; for attempt in 1..=FINISH_ATTEMPTS { 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; } } } tracing::error!( job_id = %job.job_id, "could not report the job result; the forge will reap it after heartbeats stop" ); } } async fn send_line(tx: &mpsc::Sender>, line: String) { let mut bytes = line.into_bytes(); bytes.push(b'\n'); let _ = tx.send(bytes).await; }