//! The sweeper (architecture section 8): once at boot and then hourly, it removes what has expired. //! //! - browser sessions past their expiry, with their back-channel logout jobs queued first; //! - authorization codes that expired more than a day ago (a day is kept so that a reused code is still //! recognised as reuse, which revokes the tokens it issued); //! - refresh tokens past their expiry; //! - revoked access tokens past their expiry (the revocation has done its work by then); //! - directory change rows older than 30 days (a cursor older than the oldest row gets `resync`); //! - account links and invitations past their expiry; //! - sent mail older than 30 days (its body was blanked when it was sent); //! - failed and abandoned mail older than 30 days; //! - back-channel deliveries delivered or given up more than 7 days ago; //! - audit entries older than a year (`AUDIT_KEEP`). //! //! Each step runs on its own: one failing step is logged and does not stop the others. use std::time::Duration; use im_core::store::Store; use im_core::time::{now_stamp, stamp_before, stamp_minus}; use tokio::sync::watch; /// The time between two sweeps. const EVERY: Duration = Duration::from_secs(60 * 60); /// How long a directory change row is kept (contracts 3.2 and architecture section 3). const DIRECTORY_KEEP: Duration = Duration::from_secs(30 * 24 * 60 * 60); /// How long an expired authorization code is kept, so a reuse is still seen as reuse. const CODE_KEEP: Duration = Duration::from_secs(24 * 60 * 60); /// How long a sent mail is kept in the outbox before it is deleted (architecture section 8). Failed and /// abandoned mail are kept as long. const MAIL_KEEP: Duration = Duration::from_secs(30 * 24 * 60 * 60); /// How long a finished back-channel delivery (sent or given up) is kept. A week is long enough to inspect /// a failure on the admin pages. const OUTBOX_KEEP: Duration = Duration::from_secs(7 * 24 * 60 * 60); /// How long an audit entry is kept. A fixed year, not a setting: `docs/architecture.md` section 3 lists it with /// the other retention rules. Entries older than this are removed. const AUDIT_KEEP: Duration = Duration::from_secs(365 * 24 * 60 * 60); /// Starts the sweeper. It runs until `stop` is set. pub fn spawn(store: Store, mut stop: watch::Receiver) -> tokio::task::JoinHandle<()> { tokio::spawn(async move { loop { sweep_once(&store).await; tokio::select! { () = tokio::time::sleep(EVERY) => {} _ = stop.wait_for(|stopped| *stopped) => return, } } }) } /// One pass over every table. Failures are logged per step. pub async fn sweep_once(store: &Store) { let now = now_stamp(); match store.sweep_expired_sessions().await { Ok(0) => {} Ok(count) => tracing::info!(count, "sweeper: expired sessions removed"), Err(error) => tracing::error!(error = %error, "sweeper: sessions"), } let codes_before = stamp_minus(&now, CODE_KEEP).unwrap_or_else(|| now.clone()); match store.sweep_auth_codes(&codes_before).await { Ok(0) => {} Ok(count) => tracing::info!(count, "sweeper: expired authorization codes removed"), Err(error) => tracing::error!(error = %error, "sweeper: authorization codes"), } match store.sweep_refresh_tokens(&now).await { Ok(0) => {} Ok(count) => tracing::info!(count, "sweeper: expired refresh tokens removed"), Err(error) => tracing::error!(error = %error, "sweeper: refresh tokens"), } match store.sweep_revoked_access_tokens(&now).await { Ok(0) => {} Ok(count) => tracing::info!(count, "sweeper: expired access token revocations removed"), Err(error) => tracing::error!(error = %error, "sweeper: access token revocations"), } match store .prune_directory_changes(&stamp_before(DIRECTORY_KEEP)) .await { Ok(0) => {} Ok(count) => tracing::info!(count, "sweeper: old directory changes removed"), Err(error) => tracing::error!(error = %error, "sweeper: directory changes"), } match store.sweep_links(&now).await { Ok(0) => {} Ok(count) => tracing::info!(count, "sweeper: expired account links removed"), Err(error) => tracing::error!(error = %error, "sweeper: account links"), } match store.sweep_invites(&now).await { Ok(0) => {} Ok(count) => tracing::info!(count, "sweeper: expired invitations removed"), Err(error) => tracing::error!(error = %error, "sweeper: invitations"), } match store.prune_sent_mail(&stamp_before(MAIL_KEEP)).await { Ok(0) => {} Ok(count) => tracing::info!(count, "sweeper: sent mail removed"), Err(error) => tracing::error!(error = %error, "sweeper: sent mail"), } match store.prune_failed_mail(&stamp_before(MAIL_KEEP)).await { Ok(0) => {} Ok(count) => tracing::info!(count, "sweeper: failed mail removed"), Err(error) => tracing::error!(error = %error, "sweeper: failed mail"), } match store .prune_backchannel_outbox(&stamp_before(OUTBOX_KEEP)) .await { Ok(0) => {} Ok(count) => tracing::info!(count, "sweeper: finished back-channel deliveries removed"), Err(error) => tracing::error!(error = %error, "sweeper: back-channel deliveries"), } match store.prune_audit(&stamp_before(AUDIT_KEEP)).await { Ok(0) => {} Ok(count) => tracing::info!(count, "sweeper: old audit entries removed"), Err(error) => tracing::error!(error = %error, "sweeper: audit entries"), } }