OXYGENHUB
# d2263e299da7 oxygenhub / crates / hub / src / maintenance.rs
6.2 KiB · Rust · last changed Board cards can be archived, by hand or by age (refs #293) 11 days 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
    // Report files past retention first (#127), so this run prunes them.
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
    // Drops are repositories to keep tidy too (#279).
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
        // Flow's artifacts and reports' files are named only in the
50
        // database: kept as roots, or gc would prune them after the grace
51
        // period (#126, #127). A listing that fails skips this repository
52
        // rather than risk them.
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
/// The day the nightly run last ran, kept in the data directory: a restart
99
/// within `gc_hour` (a deploy) does not run it again. On 2026-10-17 one did,
100
/// and metalgear2's requests waited 74 s behind its repack in GCS.
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
/// A day as the record keeps it: `2026-10-17`.
116
fn day_text(day: time::Date) -> String {
117
    format!("{:04}-{:02}-{:02}", day.year(), u8::from(day.month()), day.day())
118
}
119
120
/// Run [`gc_all`] once a day at `retention.gc_hour` UTC.
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
/// Each hour, archive the cards of columns that archive after some days
151
/// once they have been there longer (#293).
152
pub async fn archiver(app: Shared) {
153
    loop {
154
        match crate::db::projects::archive_due(&app.db, crate::db::now_ms()).await {
155
            Ok(n) if n > 0 => tracing::info!("projects: archived {n} card(s) their columns keep no longer"),
156
            Ok(_) => {}
157
            Err(e) => tracing::warn!("projects: archiving failed: {e}"),
158
        }
159
        tokio::time::sleep(std::time::Duration::from_secs(3600)).await;
160
    }
161
}
162
163
#[cfg(test)]
164
mod tests {
165
    #[test]
166
    fn a_day_is_written_as_the_record_keeps_it() {
167
        let day = time::Date::from_calendar_date(2026, time::Month::October, 7).unwrap();
168
        assert_eq!(super::day_text(day), "2026-10-07");
169
    }
170
}