forge
Explore
Log in

Don't you dare go Hollow

forge

Clone
★ 0

0 watching0 forksRustAGPL-3.07.1 MiB490f099Tahinli7 hours ago322 commits

prune.rs95 lines, 3.0 KiBHistoryRawDownload
1//! Removes overwritten and deleted history. Pages stop serving such a hash at once; this deletes
2//! the objects themselves, one repo at a time, once the grace period has passed.
3
4use std::path::Path;
5use std::time::{Duration, SystemTime};
6
7use forge_git::browse::reachable_commits;
8use forge_git::maintenance::{PRUNE_AFTER_GRACE, prune_due, prune_unreachable};
9use tokio::time::MissedTickBehavior;
10
11use crate::state::AppState;
12
13/// How often repos are checked for a due prune.
14const SWEEP_EVERY: Duration = Duration::from_secs(5 * 60);
15
16/// Starts the prune task. It runs until the process exits.
17pub fn spawn(state: AppState) {
18 tokio::spawn(run(state));
19}
20
21async fn run(state: AppState) {
22 let mut tick = tokio::time::interval(SWEEP_EVERY);
23 tick.set_missed_tick_behavior(MissedTickBehavior::Delay);
24 loop {
25 tick.tick().await;
26 sweep(&state).await;
27 }
28}
29
30async fn sweep(state: &AppState) {
31 let ids = match state.store.all_repo_ids().await {
32 Ok(ids) => ids,
33 Err(e) => {
34 tracing::error!(error = %e, "prune: listing repos failed");
35 return;
36 }
37 };
38 for id in ids {
39 let Ok(dir) = forge_git::repo::repo_path(&state.git.data_dir, &id) else {
40 continue;
41 };
42 if !dir.is_dir() || !prune_due(&dir, SystemTime::now()) {
43 continue;
44 }
45 match prune_unreachable(&state.git, &dir, PRUNE_AFTER_GRACE).await {
46 Ok(()) => {
47 // Prunes the standby soon. Does nothing when replication is off.
48 crate::replication::enqueue_repo(state, &id);
49 blank_unreachable_messages(state, &id, &dir).await;
50 }
51 Err(e) => tracing::error!(error = %e, repo_id = %id, "prune failed"),
52 }
53 }
54}
55
56/// Blanks the stored message of each co-authored commit the prune made unreachable. The rows stay,
57/// so contribution counts do not change. Errors are logged; the prune itself already succeeded.
58async fn blank_unreachable_messages(state: &AppState, repo_id: &str, dir: &Path) {
59 if let Err(e) = blank_unreachable_inner(state, repo_id, dir).await {
60 tracing::error!(error = %e, repo_id = %repo_id, "prune: blanking commit messages failed");
61 }
62}
63
64async fn blank_unreachable_inner(
65 state: &AppState,
66 repo_id: &str,
67 dir: &Path,
68) -> Result<(), String> {
69 let oids = state
70 .store
71 .coauthored_oids(repo_id)
72 .await
73 .map_err(|e| e.to_string())?;
74 if oids.is_empty() {
75 return Ok(());
76 }
77 let repo = dir.to_path_buf();
78 let check = oids.clone();
79 let reachable = tokio::task::spawn_blocking(move || reachable_commits(&repo, &check))
80 .await
81 .map_err(|e| e.to_string())?
82 .map_err(|e| e.to_string())?;
83 let gone: Vec<String> = oids
84 .into_iter()
85 .filter(|oid| !reachable.contains(oid))
86 .collect();
87 if gone.is_empty() {
88 return Ok(());
89 }
90 state
91 .store
92 .blank_coauthored(repo_id, &gone)
93 .await
94 .map_err(|e| e.to_string())
95}