2025-03-24 16:47:42 +01:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
use std::time::Duration;
|
|
|
|
|
use tokio::time;
|
|
|
|
|
use tracing::{debug, error, info, instrument};
|
|
|
|
|
|
|
|
|
|
use crate::common::errors::Result;
|
|
|
|
|
use crate::domain::repositories::trash_repository::TrashRepository;
|
2026-03-03 15:36:42 +00:00
|
|
|
use crate::infrastructure::repositories::pg::trash_db_repository::TrashDbRepository;
|
2026-06-11 13:19:29 +00:00
|
|
|
use crate::infrastructure::services::dedup_service::DedupService;
|
2025-03-24 16:47:42 +01:00
|
|
|
|
2026-02-26 00:42:41 +01:00
|
|
|
/// Service for automatic cleanup of expired items in the trash.
|
|
|
|
|
///
|
|
|
|
|
/// Uses `TrashRepository::delete_expired_bulk` to purge all expired items
|
|
|
|
|
/// in **2 SQL statements inside a single transaction**, instead of the
|
|
|
|
|
/// previous N+1 pattern that issued 3 queries per expired item.
|
2026-06-11 13:19:29 +00:00
|
|
|
///
|
|
|
|
|
/// Each sweep ends with a dedup `garbage_collect()` pass: it reclaims the
|
|
|
|
|
/// blobs the expiry just dereferenced AND any other zero-reference rows —
|
|
|
|
|
/// notably chunks left behind by aborted streaming uploads, whose rollback
|
|
|
|
|
/// registers them at ref_count 0 precisely so this sweep can find them.
|
|
|
|
|
/// Without it, orphans would only be collected when a user happens to
|
|
|
|
|
/// empty their trash by hand.
|
2025-03-24 16:47:42 +01:00
|
|
|
pub struct TrashCleanupService {
|
2026-03-03 15:36:42 +00:00
|
|
|
trash_repository: Arc<TrashDbRepository>,
|
2026-06-11 13:19:29 +00:00
|
|
|
dedup_service: Arc<DedupService>,
|
2025-03-24 16:47:42 +01:00
|
|
|
cleanup_interval_hours: u64,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TrashCleanupService {
|
2026-06-11 13:19:29 +00:00
|
|
|
pub fn new(
|
|
|
|
|
trash_repository: Arc<TrashDbRepository>,
|
|
|
|
|
dedup_service: Arc<DedupService>,
|
|
|
|
|
cleanup_interval_hours: u64,
|
|
|
|
|
) -> Self {
|
2025-03-24 16:47:42 +01:00
|
|
|
Self {
|
|
|
|
|
trash_repository,
|
2026-06-11 13:19:29 +00:00
|
|
|
dedup_service,
|
2026-02-12 09:41:25 +01:00
|
|
|
cleanup_interval_hours: cleanup_interval_hours.max(1), // Minimum 1 hour
|
2025-03-24 16:47:42 +01:00
|
|
|
}
|
|
|
|
|
}
|
2026-02-14 01:29:34 +01:00
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Starts the periodic cleanup job
|
2025-03-24 16:47:42 +01:00
|
|
|
#[instrument(skip(self))]
|
|
|
|
|
pub async fn start_cleanup_job(&self) {
|
|
|
|
|
let trash_repository = self.trash_repository.clone();
|
2026-06-11 13:19:29 +00:00
|
|
|
let dedup_service = self.dedup_service.clone();
|
2025-03-24 16:47:42 +01:00
|
|
|
let interval_hours = self.cleanup_interval_hours;
|
2026-02-14 01:29:34 +01:00
|
|
|
|
|
|
|
|
info!(
|
|
|
|
|
"Starting trash cleanup job with interval of {} hours",
|
|
|
|
|
interval_hours
|
|
|
|
|
);
|
|
|
|
|
|
2025-03-24 16:47:42 +01:00
|
|
|
tokio::spawn(async move {
|
|
|
|
|
let interval_duration = Duration::from_secs(interval_hours * 60 * 60);
|
|
|
|
|
let mut interval = time::interval(interval_duration);
|
2026-02-14 01:29:34 +01:00
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
// First immediate execution
|
2026-06-11 13:19:29 +00:00
|
|
|
Self::cleanup_expired_items(trash_repository.clone(), dedup_service.clone())
|
2026-02-14 01:29:34 +01:00
|
|
|
.await
|
2026-02-12 09:41:25 +01:00
|
|
|
.unwrap_or_else(|e| error!("Error in initial trash cleanup: {:?}", e));
|
2026-02-14 01:29:34 +01:00
|
|
|
|
2025-03-24 16:47:42 +01:00
|
|
|
loop {
|
|
|
|
|
interval.tick().await;
|
2026-02-12 09:41:25 +01:00
|
|
|
debug!("Running scheduled trash cleanup task");
|
2026-02-14 01:29:34 +01:00
|
|
|
|
2026-06-11 13:19:29 +00:00
|
|
|
if let Err(e) =
|
|
|
|
|
Self::cleanup_expired_items(trash_repository.clone(), dedup_service.clone())
|
|
|
|
|
.await
|
|
|
|
|
{
|
2026-02-12 09:41:25 +01:00
|
|
|
error!("Error in scheduled trash cleanup: {:?}", e);
|
2025-03-24 16:47:42 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-02-14 01:29:34 +01:00
|
|
|
|
2026-06-11 13:19:29 +00:00
|
|
|
/// Bulk-delete all expired trash items in a single transaction, then
|
|
|
|
|
/// garbage-collect every zero-reference manifest/blob (expired content
|
|
|
|
|
/// plus aborted-upload orphans).
|
|
|
|
|
#[instrument(skip(trash_repository, dedup_service))]
|
|
|
|
|
async fn cleanup_expired_items(
|
|
|
|
|
trash_repository: Arc<TrashDbRepository>,
|
|
|
|
|
dedup_service: Arc<DedupService>,
|
|
|
|
|
) -> Result<()> {
|
2026-02-26 00:42:41 +01:00
|
|
|
debug!("Starting bulk cleanup of expired trash items");
|
2026-02-14 01:29:34 +01:00
|
|
|
|
2026-02-26 00:42:41 +01:00
|
|
|
let (files, folders) = trash_repository.delete_expired_bulk().await?;
|
2026-02-14 01:29:34 +01:00
|
|
|
|
2026-02-26 00:42:41 +01:00
|
|
|
if files == 0 && folders == 0 {
|
2026-02-12 09:41:25 +01:00
|
|
|
debug!("No expired items to clean up");
|
2026-02-26 00:42:41 +01:00
|
|
|
} else {
|
|
|
|
|
info!(
|
|
|
|
|
"Trash cleanup completed: {} files + {} folders purged",
|
|
|
|
|
files, folders
|
|
|
|
|
);
|
2025-03-24 16:47:42 +01:00
|
|
|
}
|
2026-02-14 01:29:34 +01:00
|
|
|
|
2026-06-11 13:19:29 +00:00
|
|
|
// Runs on the maintenance pool; batched (500 rows/iteration) with
|
|
|
|
|
// yield points, so it never starves request-path queries.
|
|
|
|
|
match dedup_service.garbage_collect().await {
|
|
|
|
|
Ok((0, _)) => debug!("Trash cleanup GC: nothing to collect"),
|
|
|
|
|
Ok((items, bytes)) => {
|
|
|
|
|
info!("Trash cleanup GC: reclaimed {items} orphaned blobs ({bytes} bytes)");
|
|
|
|
|
}
|
|
|
|
|
Err(e) => error!("Trash cleanup GC failed: {:?}", e),
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-24 16:47:42 +01:00
|
|
|
Ok(())
|
|
|
|
|
}
|
2026-02-14 01:29:34 +01:00
|
|
|
}
|