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