schedr_master/
retention.rs1use std::time::Duration;
7
8use jiff::{SignedDuration, Timestamp};
9use schedr_config::config::{Age, RetentionSection};
10use schedr_db::{Database, DbError};
11
12use crate::metrics::MasterMetrics;
13
14#[cfg(test)]
15mod tests;
16
17#[derive(Debug, Clone, Copy, PartialEq, Eq)]
19pub struct RetentionSettings {
20 pub runs: Option<Duration>,
22 pub outputs: Option<Duration>,
24 pub events: Option<Duration>,
26 pub commands: Option<Duration>,
28 pub interval: Duration,
30 pub batch: u32,
32}
33
34impl Default for RetentionSettings {
35 fn default() -> Self {
37 Self {
38 runs: None,
39 outputs: None,
40 events: None,
41 commands: None,
42 interval: Duration::from_hours(1),
43 batch: 100,
44 }
45 }
46}
47
48const fn kept(age: Age) -> Option<Duration> {
49 match age {
50 Age::For(duration) => Some(duration),
51 Age::Forever => None,
52 }
53}
54
55impl RetentionSettings {
56 #[must_use]
58 pub fn from_config(section: &RetentionSection, interval: Duration, batch: usize) -> Self {
59 let runs = kept(section.runs);
60 let outputs = kept(section.outputs()).filter(|o| runs.is_none_or(|r| *o < r));
62 Self {
63 runs,
64 outputs,
65 events: kept(section.events()),
66 commands: kept(section.commands),
67 interval,
68 batch: u32::try_from(batch).unwrap_or(u32::MAX).max(1),
69 }
70 }
71}
72
73#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
75pub struct Pass {
76 pub runs: u64,
78 pub outputs: u64,
80 pub events: u64,
82 pub commands: u64,
84 pub dags: u64,
86 pub versions: u64,
88}
89
90impl Pass {
91 #[must_use]
93 pub const fn any(&self) -> bool {
94 self.runs + self.outputs + self.events + self.commands + self.dags + self.versions > 0
95 }
96}
97
98fn cut(now: Timestamp, age: Duration) -> Timestamp {
100 SignedDuration::try_from(age)
101 .ok()
102 .and_then(|age| now.checked_sub(age).ok())
103 .unwrap_or(Timestamp::MIN)
104}
105
106async fn batches<F, Fut>(leading: &impl Fn() -> bool, mut delete: F) -> Result<u64, DbError>
109where
110 F: FnMut() -> Fut,
111 Fut: std::future::Future<Output = Result<u64, DbError>>,
112{
113 let mut total = 0;
114 while leading() {
115 let deleted = delete().await?;
116 total += deleted;
117 if deleted == 0 {
118 break;
119 }
120 }
121 Ok(total)
122}
123
124pub async fn pass(
131 db: &Database,
132 settings: &RetentionSettings,
133 now: Timestamp,
134 leading: impl Fn() -> bool,
135 metrics: &MasterMetrics,
136) -> Result<Pass, DbError> {
137 let batch = settings.batch;
138 let mut pass = Pass::default();
139 if let Some(age) = settings.outputs {
140 let before = cut(now, age);
141 pass.outputs = batches(&leading, || db.delete_old_outputs(before, batch, now)).await?;
142 metrics.retention_deleted("outputs", pass.outputs);
143 }
144 if let Some(age) = settings.runs {
145 let before = cut(now, age);
146 pass.runs = batches(&leading, || db.delete_old_runs(before, batch)).await?;
147 metrics.retention_deleted("runs", pass.runs);
148 if leading() {
149 pass.dags = db.delete_removed_dags(before).await?;
150 metrics.retention_deleted("dags", pass.dags);
151 }
152 pass.versions = batches(&leading, || db.delete_unused_versions()).await?;
153 metrics.retention_deleted("versions", pass.versions);
154 }
155 if let Some(age) = settings.events {
156 let before = cut(now, age);
157 pass.events = batches(&leading, || db.delete_old_events(before, batch)).await?;
158 metrics.retention_deleted("events", pass.events);
159 }
160 if let Some(age) = settings.commands {
161 let before = cut(now, age);
162 pass.commands = batches(&leading, || db.delete_old_commands(before, batch)).await?;
163 metrics.retention_deleted("commands", pass.commands);
164 }
165 Ok(pass)
166}