Files
sorsfonalak/services/worker/src/cleanup.rs
T
fecoandClaude Opus 5.5 c90232f40c Gift packages: buy a main package for someone, with a printable gift card
- /ajandek: the package, whom it is for and from whom, the recipient's
  address typed twice (or "I hand it over"), a message; paid on Stripe
  like any package, the invoice is the buyer's
- On payment the gift gets its own code (not a promo code: it cannot be
  withdrawn, and it adds the whole package); redeemed on /bevaltas
- The recipient's e-mail carries the message and the code, with the gift
  card attached as a PDF; the buyer's confirmation says it was a gift
- /ajandekaim: codes, state, the card to download, sending it by e-mail
  (again, or to another address) up to three times
- crates/gift-card draws the A4 card (example chart, questions avatar,
  QR code) as SVG and makes a PDF with krilla, fonts compiled in; the
  backend draws downloads on a blocking thread, the worker the e-mails
- Telegram notices say "(ajándék)"; unpaid gifts go after a week

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
2026-11-23 20:28:11 +00:00

60 lines
2.3 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); 8] = [
("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'"),
// Gifts never paid (Stripe's payment page is long closed by then), with
// the recipient's address in them.
("gifts", "DELETE FROM gifts WHERE paid_at IS NULL AND created_at < now() - interval '7 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,
}
}
}