Skip to main content

cloudillo_auth/
cleanup.rs

1//! Periodic cleanup task for expired auth data (API keys, verification codes)
2
3use async_trait::async_trait;
4use serde::{Deserialize, Serialize};
5use std::sync::Arc;
6
7use cloudillo_core::scheduler::{Task, TaskId};
8
9use crate::prelude::*;
10
11/// Cleanup task for expired authentication data
12///
13/// Removes expired API keys and verification codes.
14/// Scheduled to run daily at 3 AM via cron.
15#[derive(Clone, Debug, Serialize, Deserialize)]
16pub struct AuthCleanupTask;
17
18#[async_trait]
19impl Task<App> for AuthCleanupTask {
20	fn kind() -> &'static str {
21		"auth.cleanup"
22	}
23
24	fn build(_id: TaskId, _context: &str) -> ClResult<Arc<dyn Task<App>>> {
25		Ok(Arc::new(AuthCleanupTask))
26	}
27
28	fn serialize(&self) -> String {
29		String::new()
30	}
31
32	fn kind_of(&self) -> &'static str {
33		"auth.cleanup"
34	}
35
36	async fn run(&self, app: &App) -> ClResult<()> {
37		info!("Running auth cleanup task");
38
39		// Cleanup expired API keys
40		match app.auth_adapter.cleanup_expired_api_keys().await {
41			Ok(count) => {
42				if count > 0 {
43					info!("Cleaned up {} expired API keys", count);
44				}
45			}
46			Err(e) => {
47				warn!("Failed to cleanup expired API keys: {}", e);
48			}
49		}
50
51		// Cleanup expired verification codes
52		match app.auth_adapter.cleanup_expired_verification_codes().await {
53			Ok(count) => {
54				if count > 0 {
55					info!("Cleaned up {} expired verification codes", count);
56				}
57			}
58			Err(e) => {
59				warn!("Failed to cleanup expired verification codes: {}", e);
60			}
61		}
62
63		Ok(())
64	}
65}
66
67// vim: ts=4