| 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 |
pub async fn nightly(app: Shared) { |
| 100 |
let hour = app.config.retention.gc_hour; |
| 101 |
if hour >= 24 { |
| 102 |
tracing::info!("gc: the nightly run is off (retention.gc_hour = {hour})"); |
| 103 |
return; |
| 104 |
} |
| 105 |
let mut last_day = None; |
| 106 |
loop { |
| 107 |
tokio::time::sleep(std::time::Duration::from_secs(60)).await; |
| 108 |
let now = time::OffsetDateTime::now_utc(); |
| 109 |
if u32::from(now.hour()) != hour { |
| 110 |
continue; |
| 111 |
} |
| 112 |
let day = now.date(); |
| 113 |
if last_day == Some(day) { |
| 114 |
continue; |
| 115 |
} |
| 116 |
last_day = Some(day); |
| 117 |
match crate::db::sessions::sweep(&app.db).await { |
| 118 |
Ok(n) if n > 0 => tracing::info!("sessions: forgot {n} that had ended"), |
| 119 |
Ok(_) => {} |
| 120 |
Err(e) => tracing::warn!("sessions: sweep failed: {e}"), |
| 121 |
} |
| 122 |
let done = gc_all(&app).await; |
| 123 |
tracing::info!("gc: nightly run over {} repositories, {} failed", done.len(), done.iter().filter(|d| d.failed).count()); |
| 124 |
} |
| 125 |
} |