use std::time::Duration;
use snodra_messaging::Config;
use snodra_messaging::store::StoreError;
use crate::db::{Db, Dialect, Stmt};
pub(crate) const BATCH: u64 = 1000;
pub(crate) async fn batched(
db: &Db,
table: &'static str,
key: &'static str,
condition: &'static str,
before: i64,
) -> Result<u64, StoreError> {
let sql = match db.dialect() {
#[cfg(feature = "postgres")]
Dialect::Postgres => format!(
"DELETE FROM {table} WHERE {key} IN (SELECT {key} FROM {table} WHERE {condition} LIMIT {BATCH})"
),
#[cfg(feature = "mysql")]
Dialect::MySql => {
let _ = key;
format!("DELETE FROM {table} WHERE {condition} LIMIT {BATCH}")
}
};
let mut total = 0;
loop {
let mut t = db.own().await?;
let result = t
.conn()
.execute(Stmt::built(sql.clone()).time("before", before))
.await;
let n = t.finish(result).await?;
total += n;
if n < BATCH {
return Ok(total);
}
}
}
fn ms(d: Duration) -> i64 {
i64::try_from(d.as_millis()).unwrap_or(i64::MAX)
}
pub(crate) async fn run(db: &Db, config: &Config) -> Result<u64, StoreError> {
let now = db.now();
let before = |retention: Duration| now.saturating_sub(ms(retention));
let failed = before(config.sagas_failed_retention);
let mut total = 0;
for (table, key, condition, bound) in [
(
"snodra_outbox",
"id",
"sent_at < ?",
before(config.outbox_retention),
),
(
"snodra_processed_events",
"id_hash",
"processed_at < ?",
before(config.idempotency_retention),
),
(
"snodra_external_calls",
"id_hash",
"updated_at < ?",
before(config.external_calls_retention),
),
(
"snodra_sagas",
"id",
"ended = TRUE AND ended_at < ?",
before(config.sagas_ended_retention),
),
(
"snodra_saga_dispatch",
"id",
"dispatched_at < ?",
before(config.sagas_dispatch_retention),
),
("snodra_saga_dispatch", "id", "failed_at < ?", failed),
(
"snodra_saga_dispatch_leases",
"saga_id",
"lease_until < ?",
now,
),
(
"snodra_deadlines",
"id",
"finished_at < ?",
before(config.deadlines_retention),
),
(
"snodra_saga_failures",
"id_hash",
"last_failed_at < ?",
failed,
),
("snodra_dead_letters", "id", "expire_at <= ?", now),
] {
total += batched(db, table, key, condition, bound).await?;
}
Ok(total)
}