use sqlx::PgPool;
use std::time::Duration;
use tracing::{error, info};
use crate::handlers::token_rotation::send_rotation_reminders;
const REMINDER_INTERVAL: Duration = Duration::from_secs(24 * 60 * 60);
const DEFAULT_THRESHOLD_DAYS: i64 = 90;
pub fn start_token_rotation_reminders_worker(pool: PgPool) {
let threshold_days = std::env::var("TOKEN_ROTATION_THRESHOLD_DAYS")
.ok()
.and_then(|s| s.parse::<i64>().ok())
.unwrap_or(DEFAULT_THRESHOLD_DAYS);
tokio::spawn(async move {
let mut interval = tokio::time::interval(REMINDER_INTERVAL);
interval.tick().await;
loop {
interval.tick().await;
match send_rotation_reminders(&pool, threshold_days).await {
Ok(count) if count > 0 => {
info!(
"Token rotation reminders: sent {} reminder(s) for tokens older than {} days",
count, threshold_days
);
}
Ok(_) => {
tracing::debug!(
"Token rotation reminders: no tokens needed reminding (threshold {} days)",
threshold_days
);
}
Err(e) => {
error!("Token rotation reminders failed: {:?}", e);
}
}
}
});
info!(
"Token rotation reminder worker started (runs every 24h, threshold {} days)",
threshold_days
);
}