use std::collections::HashSet; use std::path::PathBuf; use std::time::Duration; use forge_core::time::stamp_ago; use tokio::time::{Instant, MissedTickBehavior}; use super::{artifact_key, log_path, sync_run_statuses}; use crate::state::AppState; const REAP_EVERY: Duration = Duration::from_secs(30); const LOST_AFTER: Duration = Duration::from_secs(120); const ALERTS_SLOW_EVERY: Duration = Duration::from_secs(5 * 60); const PURGE_EVERY: Duration = Duration::from_secs(24 * 60 * 60); const HEALTH_EVERY: Duration = Duration::from_secs(60); pub fn spawn(state: AppState) { tokio::spawn(run(state)); } async fn run(state: AppState) { let mut tick = tokio::time::interval(REAP_EVERY); tick.set_missed_tick_behavior(MissedTickBehavior::Delay); let mut next_purge = Instant::now(); let mut next_slow_alerts = Instant::now() + ALERTS_SLOW_EVERY; let mut next_health = Instant::now() + HEALTH_EVERY; let booted = Instant::now(); loop { tick.tick().await; if booted.elapsed() >= LOST_AFTER { reap_before(&state, &stamp_ago(LOST_AFTER)).await; } crate::alerts::check_runners(&state, crate::views::now_unix()).await; if Instant::now() >= next_health { crate::live::admin().bump(&crate::live::Topic::AdminHealth); next_health = Instant::now() + HEALTH_EVERY; } if Instant::now() >= next_slow_alerts { crate::alerts::check_slow(&state).await; next_slow_alerts = Instant::now() + ALERTS_SLOW_EVERY; } if Instant::now() >= next_purge { purge_once(&state).await; next_purge = Instant::now() + PURGE_EVERY; } } } pub async fn reap_before(state: &AppState, older_than: &str) -> usize { let jobs = match state.store.reap_lost_jobs(older_than).await { Ok(jobs) => jobs, Err(e) => { tracing::error!(error = %e, "ci: reaping lost jobs failed"); return 0; } }; let mut seen = HashSet::new(); let mut wake = false; for job in &jobs { state.ci_live.close_job(&job.id); state.ci_live.bump_run(&job.run_id); if !seen.insert(job.run_id.clone()) { continue; } match state.store.run_by_id(&job.run_id).await { Ok(Some(run)) => { state.ci_live.bump_repo(&run.repo_id); match sync_run_statuses(state, &run).await { Ok(queued) => wake |= queued, Err(e) => { tracing::error!(error = %e, run_id = %run.id, "ci: syncing statuses failed") } } } Ok(None) => {} Err(e) => tracing::error!(error = %e, run_id = %job.run_id, "ci: loading run failed"), } } if wake { state.ci_waker.notify_waiters(); } if !jobs.is_empty() { state.ci_live.bump_runners(); tracing::warn!(count = jobs.len(), "ci: reaped lost jobs"); } jobs.len() } pub async fn purge_once(state: &AppState) -> (usize, usize) { let live = state.live(); let ci = &live.ci; let day = |days: u64| Duration::from_secs(days.saturating_mul(24 * 60 * 60)); let logs_before = stamp_ago(day(ci.log_retention_days)); let artifacts_before = stamp_ago(day(ci.artifact_retention_days)); let max_total_bytes = ci.artifact_max_total_mb.saturating_mul(1024 * 1024); let (job_ids, artifact_ids, run_ids) = match state .store .purge_old( &logs_before, &artifacts_before, ci.artifact_keep_runs, max_total_bytes, ) .await { Ok(ids) => ids, Err(e) => { tracing::error!(error = %e, "ci: retention purge failed"); return (0, 0); } }; let logs: Vec = job_ids .iter() .map(|id| log_path(&state.config.data_dir, id)) .collect(); let log_count = logs.len(); let removed = tokio::task::spawn_blocking(move || { for path in &logs { if let Err(e) = std::fs::remove_file(path) && e.kind() != std::io::ErrorKind::NotFound { tracing::error!(error = %e, path = %path.display(), "ci: deleting a log failed"); } } }) .await; if let Err(e) = removed { tracing::error!(error = %e, "ci: log deletion task failed"); } let artifact_count = artifact_ids.len(); for id in &artifact_ids { if let Err(e) = state.blobs.delete(&artifact_key(id)).await { tracing::error!(error = %e, artifact_id = %id, "ci: deleting an artifact failed"); } } for id in &job_ids { state.ci_live.bump_job(id); } for id in &run_ids { state.ci_live.bump_run(id); if let Ok(Some(run)) = state.store.run_by_id(id).await { state.ci_live.bump_repo(&run.repo_id); } } (log_count, artifact_count) }