| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
use crate::db; |
| 7 |
use crate::repos::RepoName; |
| 8 |
use crate::Shared; |
| 9 |
|
| 10 |
|
| 11 |
#[derive(Clone, Debug)] |
| 12 |
pub struct Done { |
| 13 |
pub repo: String, |
| 14 |
pub failed: bool, |
| 15 |
pub summary: String, |
| 16 |
} |
| 17 |
|
| 18 |
impl std::fmt::Display for Done { |
| 19 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 20 |
write!(f, "{}: {}", self.repo, self.summary) |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
pub async fn gc_all(app: &Shared) -> Vec<Done> { |
| 27 |
|
| 28 |
match crate::reports::expire(&app.db).await { |
| 29 |
Ok(0) => {} |
| 30 |
Ok(n) => tracing::info!("gc: {n} reports' files expired under retention"), |
| 31 |
Err(e) => tracing::warn!("gc: expiring report files: {e}"), |
| 32 |
} |
| 33 |
|
| 34 |
let rows = match db::repos::list_with_drops(&app.db).await { |
| 35 |
Ok(rows) => rows, |
| 36 |
Err(e) => { |
| 37 |
tracing::warn!("gc: listing repositories: {e}"); |
| 38 |
return Vec::new(); |
| 39 |
} |
| 40 |
}; |
| 41 |
let mut done = Vec::with_capacity(rows.len()); |
| 42 |
for row in rows { |
| 43 |
let slug = format!("{}/{}", row.owner, row.name); |
| 44 |
let Some(name) = RepoName::new(&row.owner, &row.name) else { continue }; |
| 45 |
if !app.repos.exists(&name) { |
| 46 |
continue; |
| 47 |
} |
| 48 |
let label = slug.clone(); |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
let listed = match db::flow::artifact_blobs(&app.db, row.id).await { |
| 54 |
Ok(mut blobs) => crate::reports::file_blobs(&app.db, row.id).await.map(|files| { |
| 55 |
blobs.extend(files); |
| 56 |
blobs |
| 57 |
}), |
| 58 |
Err(e) => Err(e), |
| 59 |
}; |
| 60 |
let keep: Vec<oxy_core::hash::ObjectRef> = match listed { |
| 61 |
Ok(blobs) => blobs.iter().filter_map(|b| oxy_core::hash::ObjectRef::from_hex(b)).collect(), |
| 62 |
Err(e) => { |
| 63 |
tracing::warn!("gc: {slug}: listing its artifacts: {e}; skipped"); |
| 64 |
continue; |
| 65 |
} |
| 66 |
}; |
| 67 |
let started = std::time::Instant::now(); |
| 68 |
let result = app |
| 69 |
.repos |
| 70 |
.write(&name, move |repo, _| Ok(repo.gc_keeping(false, &keep, &mut |phase| tracing::debug!("gc {label}: {phase}"))?)) |
| 71 |
.await; |
| 72 |
let d = match result { |
| 73 |
Ok(r) => Done { |
| 74 |
summary: format!( |
| 75 |
"{} reachable; pruned {} ({} bytes){}; packed {} into one pack in {:.1} s", |
| 76 |
r.reachable, |
| 77 |
r.pruned.objects, |
| 78 |
r.pruned.bytes, |
| 79 |
if r.pruned.deferred > 0 { format!(", {} deferred by the grace period", r.pruned.deferred) } else { String::new() }, |
| 80 |
r.packed.objects, |
| 81 |
started.elapsed().as_secs_f64() |
| 82 |
), |
| 83 |
repo: slug, |
| 84 |
failed: false, |
| 85 |
}, |
| 86 |
Err(e) => Done { summary: format!("failed: {e}"), repo: slug, failed: true }, |
| 87 |
}; |
| 88 |
if d.failed { |
| 89 |
tracing::warn!("gc {}", d); |
| 90 |
} else { |
| 91 |
tracing::info!("gc {}", d); |
| 92 |
} |
| 93 |
done.push(d); |
| 94 |
} |
| 95 |
done |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
fn last_run_path(app: &Shared) -> std::path::PathBuf { |
| 102 |
app.config.data.join("gc-last-day") |
| 103 |
} |
| 104 |
|
| 105 |
fn last_run(app: &Shared) -> Option<String> { |
| 106 |
Some(std::fs::read_to_string(last_run_path(app)).ok()?.trim().to_string()) |
| 107 |
} |
| 108 |
|
| 109 |
fn record_run(app: &Shared, day: &str) { |
| 110 |
if let Err(e) = std::fs::write(last_run_path(app), day) { |
| 111 |
tracing::warn!("gc: recording the nightly run: {e}"); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
fn day_text(day: time::Date) -> String { |
| 117 |
format!("{:04}-{:02}-{:02}", day.year(), u8::from(day.month()), day.day()) |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
pub async fn nightly(app: Shared) { |
| 122 |
let hour = app.config.retention.gc_hour; |
| 123 |
if hour >= 24 { |
| 124 |
tracing::info!("gc: the nightly run is off (retention.gc_hour = {hour})"); |
| 125 |
return; |
| 126 |
} |
| 127 |
let mut last_day = last_run(&app); |
| 128 |
loop { |
| 129 |
tokio::time::sleep(std::time::Duration::from_secs(60)).await; |
| 130 |
let now = time::OffsetDateTime::now_utc(); |
| 131 |
if u32::from(now.hour()) != hour { |
| 132 |
continue; |
| 133 |
} |
| 134 |
let day = day_text(now.date()); |
| 135 |
if last_day.as_deref() == Some(day.as_str()) { |
| 136 |
continue; |
| 137 |
} |
| 138 |
record_run(&app, &day); |
| 139 |
last_day = Some(day); |
| 140 |
match crate::db::sessions::sweep(&app.db).await { |
| 141 |
Ok(n) if n > 0 => tracing::info!("sessions: forgot {n} that had ended"), |
| 142 |
Ok(_) => {} |
| 143 |
Err(e) => tracing::warn!("sessions: sweep failed: {e}"), |
| 144 |
} |
| 145 |
let done = gc_all(&app).await; |
| 146 |
tracing::info!("gc: nightly run over {} repositories, {} failed", done.len(), done.iter().filter(|d| d.failed).count()); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
#[cfg(test)] |
| 151 |
mod tests { |
| 152 |
#[test] |
| 153 |
fn a_day_is_written_as_the_record_keeps_it() { |
| 154 |
let day = time::Date::from_calendar_date(2026, time::Month::October, 7).unwrap(); |
| 155 |
assert_eq!(super::day_text(day), "2026-10-07"); |
| 156 |
} |
| 157 |
} |