use tracing::{error, info};
pub async fn close_postgres(db: sea_orm::DatabaseConnection, name: &str) {
match db.close().await {
Ok(_) => info!("PostgreSQL connection '{}' closed successfully", name),
Err(e) => error!("Error closing PostgreSQL connection '{}': {}", name, e),
}
}
pub async fn close_redis(redis: redis::aio::ConnectionManager, name: &str) {
drop(redis);
info!("Redis connection '{}' closed successfully", name);
}
pub struct CleanupCoordinator {
tasks: Vec<(&'static str, tokio::task::JoinHandle<()>)>,
}
impl CleanupCoordinator {
pub fn new() -> Self {
Self { tasks: Vec::new() }
}
pub fn add_task<F>(&mut self, name: &'static str, task: F)
where
F: std::future::Future<Output = ()> + Send + 'static,
{
let handle = tokio::spawn(task);
self.tasks.push((name, handle));
}
pub async fn run(self) {
info!("Running {} cleanup tasks", self.tasks.len());
for (name, handle) in self.tasks {
match handle.await {
Ok(_) => {
info!("Cleanup task '{}' completed successfully", name);
}
Err(e) => {
error!("Cleanup task '{}' failed: {}", name, e);
}
}
}
info!("All cleanup tasks completed");
}
}
impl Default for CleanupCoordinator {
fn default() -> Self {
Self::new()
}
}