feat(sessions): clean expired sessions (exp > 3month)

This commit is contained in:
Edouard Vanbelle
2026-08-09 16:18:07 +02:00
parent d638566d04
commit a34da40ce9
5 changed files with 202 additions and 0 deletions
+37
View File
@@ -1271,6 +1271,36 @@ impl AppServiceFactory {
.await
}
/// Registers the session-cleanup janitor with the scheduler.
///
/// Purges rows in `auth.sessions` whose `expires_at` is older than
/// the janitor's retention window (currently 90 days, hardcoded —
/// see `SessionCleanupService::RETENTION_DAYS`). Runs on the
/// maintenance pool: the sweep is one bulk-DELETE per tick, but
/// keeping session-hygiene off the request pool matches every
/// other janitor in this file and prevents starvation surprises.
///
/// Fills the gap called out in
/// `[[project_session_janitor_missing]]` — `delete_expired_sessions`
/// (and its cutoff-taking sibling) existed on the repo but nothing
/// was scheduling them, so `auth.sessions` bloated forever.
pub async fn create_session_cleanup_service(
&self,
maintenance_pool: &Arc<PgPool>,
core: &CoreServices,
) -> Arc<crate::infrastructure::services::session_cleanup_service::SessionCleanupService> {
let session_repo = Arc::new(
crate::infrastructure::repositories::SessionPgRepository::new(maintenance_pool.clone()),
);
Arc::new(
crate::infrastructure::services::session_cleanup_service::SessionCleanupService::new(
session_repo,
),
)
.register(&core.job_registry)
.await
}
/// Starts the tree-ETag flush job (requires database).
///
/// The statement triggers on `storage.files`/`storage.folders` only
@@ -1709,6 +1739,13 @@ impl AppServiceFactory {
self.start_tree_etag_flush_job(&maintenance_pool);
// Session janitor — bulk-deletes `auth.sessions` rows past
// the retention window (90 days beyond `expires_at`). Runs
// once every 24h on the maintenance pool.
let _ = self
.create_session_cleanup_service(&maintenance_pool, &core)
.await;
self.start_db_pool_monitor(&pool);
self.start_content_index_job(&maintenance_pool, &core, content_index);