OXYGENHUB
# 0650d94ede82 oxygenhub / crates / hub / src / maintenance.rs
3.1 KiB · Rust · last changed The hub garbage-collects every repository nightly 1 month ago source blame history raw
1
//! Housekeeping that runs on its own (hub spec §17.2): the nightly
2
//! garbage collection of every repository, one at a time under its
3
//! write lock, so a push never races a repack. `oxygenhub gc` runs the
4
//! same pass by hand.
5
6
use crate::db;
7
use crate::repos::RepoName;
8
use crate::Shared;
9
10
/// What one repository's gc did, for the log and `oxygenhub gc`.
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
/// Garbage-collect every repository, in turn. A repository that fails is
25
/// reported and the rest still run.
26
pub async fn gc_all(app: &Shared) -> Vec<Done> {
27
    let rows = match db::repos::list(&app.db).await {
28
        Ok(rows) => rows,
29
        Err(e) => {
30
            tracing::warn!("gc: listing repositories: {e}");
31
            return Vec::new();
32
        }
33
    };
34
    let mut done = Vec::with_capacity(rows.len());
35
    for row in rows {
36
        let slug = format!("{}/{}", row.owner, row.name);
37
        let Some(name) = RepoName::new(&row.owner, &row.name) else { continue };
38
        if !app.repos.exists(&name) {
39
            continue;
40
        }
41
        let label = slug.clone();
42
        let started = std::time::Instant::now();
43
        let result =
44
            app.repos.write(&name, move |repo, _| Ok(repo.gc_with(false, &mut |phase| tracing::debug!("gc {label}: {phase}"))?)).await;
45
        let d = match result {
46
            Ok(r) => Done {
47
                summary: format!(
48
                    "{} reachable; pruned {} ({} bytes){}; packed {} into one pack in {:.1} s",
49
                    r.reachable,
50
                    r.pruned.objects,
51
                    r.pruned.bytes,
52
                    if r.pruned.deferred > 0 { format!(", {} deferred by the grace period", r.pruned.deferred) } else { String::new() },
53
                    r.packed.objects,
54
                    started.elapsed().as_secs_f64()
55
                ),
56
                repo: slug,
57
                failed: false,
58
            },
59
            Err(e) => Done { summary: format!("failed: {e}"), repo: slug, failed: true },
60
        };
61
        if d.failed {
62
            tracing::warn!("gc {}", d);
63
        } else {
64
            tracing::info!("gc {}", d);
65
        }
66
        done.push(d);
67
    }
68
    done
69
}
70
71
/// Run [`gc_all`] once a day at `retention.gc_hour` UTC.
72
pub async fn nightly(app: Shared) {
73
    let hour = app.config.retention.gc_hour;
74
    if hour >= 24 {
75
        tracing::info!("gc: the nightly run is off (retention.gc_hour = {hour})");
76
        return;
77
    }
78
    let mut last_day = None;
79
    loop {
80
        tokio::time::sleep(std::time::Duration::from_secs(60)).await;
81
        let now = time::OffsetDateTime::now_utc();
82
        if u32::from(now.hour()) != hour {
83
            continue;
84
        }
85
        let day = now.date();
86
        if last_day == Some(day) {
87
            continue;
88
        }
89
        last_day = Some(day);
90
        let done = gc_all(&app).await;
91
        tracing::info!("gc: nightly run over {} repositories, {} failed", done.len(), done.iter().filter(|d| d.failed).count());
92
    }
93
}