Files
sorsfonalak/services/worker/src/cleanup.rs
T
fecoandClaude Opus 5.5 02e4e225f0
CI / test (push) Successful in 1m8s
CI / build (push) Successful in 5m13s
CI / deploy (push) Skipped
Error log, retry notice, a waiting counter that keeps counting
- generation_errors: every failed attempt (model, attempt, outcome, duration); admin
  Hibanapló page and GET /v1/admin/errors; kept 90 days
- The waiting page counts from when the text was asked for (requested_at), not from
  the page load, so a reload no longer restarts it
- It says when an attempt failed and is retried, and when a failed text's unit was
  given back; admins see the error

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
2026-11-18 21:47:37 +00:00

57 lines
2.1 KiB
Rust

//! Hourly removal of data that is no longer needed.
use std::time::Duration;
use sqlx::PgPool;
use tokio::sync::watch;
const INTERVAL: Duration = Duration::from_secs(3600);
/// (what, statement) pairs; each returns the number of deleted rows.
const STATEMENTS: [(&str, &str); 7] = [
("sessions", "DELETE FROM sessions WHERE expires_at < now()"),
("auth_tokens", "DELETE FROM auth_tokens WHERE expires_at < now()"),
// Sent e-mails contain links with tokens; keep them a week for support.
(
"emails",
"DELETE FROM email_outbox WHERE sent_at < now() - interval '7 days' \
OR failed_at < now() - interval '30 days'",
),
("invites", "DELETE FROM synastry_invites WHERE accepted_at IS NULL AND expires_at < now() - interval '30 days'"),
// Unused invitations 30 days after they closed; accepted ones once the account is deleted.
(
"invitations",
"DELETE FROM invitations WHERE (accepted_at IS NULL \
AND least(coalesce(revoked_at, expires_at), expires_at) < now() - interval '30 days') \
OR (accepted_at IS NOT NULL AND accepted_by IS NULL)",
),
("prompt_previews", "DELETE FROM prompt_previews WHERE created_at < now() - interval '30 days'"),
("generation_errors", "DELETE FROM generation_errors WHERE created_at < now() - interval '90 days'"),
];
pub async fn run_once(db: &PgPool) -> Result<Vec<(&'static str, u64)>, sqlx::Error> {
let mut deleted = Vec::new();
for (what, sql) in STATEMENTS {
deleted.push((what, sqlx::query(sql).execute(db).await?.rows_affected()));
}
Ok(deleted)
}
pub async fn run(db: PgPool, stop: watch::Receiver<bool>) {
loop {
match run_once(&db).await {
Ok(deleted) => {
let total: u64 = deleted.iter().map(|(_, n)| n).sum();
if total > 0 {
tracing::info!(?deleted, "cleanup");
}
}
Err(e) => tracing::error!(error = %e, "cleanup"),
}
tokio::select! {
() = tokio::time::sleep(INTERVAL) => {}
() = crate::stopped(stop.clone()) => break,
}
}
}