Skip to main content

wowlab_sentinel/cron/
retention.rs

1use async_trait::async_trait;
2
3use super::CronJob;
4use crate::state::ServerState;
5
6#[derive(Debug)]
7pub(super) struct RetentionCleanupJob {
8    pub schedule: String,
9    pub snapshots_retention_days: f64,
10    pub rolling_retention_days: f64,
11}
12
13#[async_trait]
14impl CronJob for RetentionCleanupJob {
15    fn name(&self) -> &'static str {
16        "retention_cleanup"
17    }
18
19    fn schedule(&self) -> &str {
20        &self.schedule
21    }
22
23    async fn run(&self, state: &ServerState) {
24        if let Err(e) = run_retention_cleanup(
25            state,
26            self.snapshots_retention_days,
27            self.rolling_retention_days,
28        )
29        .await
30        {
31            tracing::error!(error = %e, "Retention cleanup failed");
32        }
33    }
34}
35
36async fn run_retention_cleanup(
37    state: &ServerState,
38    snapshots_days: f64,
39    rolling_days: f64,
40) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
41    let snap_result =
42        sqlx::query_file!("queries/retention_delete_old_snapshots.sql", snapshots_days)
43            .execute(state.dbs.get::<crate::cron::CronDb>())
44            .await?;
45
46    if snap_result.rows_affected() > 0 {
47        tracing::info!(
48            count = snap_result.rows_affected(),
49            retention_days = snapshots_days,
50            "Cleaned old ranking snapshots"
51        );
52    }
53
54    let roll_result = sqlx::query_file!("queries/retention_delete_old_rolling.sql", rolling_days)
55        .execute(state.dbs.get::<crate::cron::CronDb>())
56        .await?;
57
58    if roll_result.rows_affected() > 0 {
59        tracing::info!(
60            count = roll_result.rows_affected(),
61            retention_days = rolling_days,
62            "Cleaned old rolling rollups"
63        );
64    }
65
66    Ok(())
67}