1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-26 15:29:21 +00:00
comunicapiv3/src/cleanup_thread.rs

68 lines
1.8 KiB
Rust
Raw Normal View History

2021-02-13 15:36:39 +00:00
//! # Cleanup thread
//!
//! This module manage a thread that automatically delete old data
//!
//! @author Pierre Hubert
2021-02-14 17:54:39 +00:00
use crate::constants::{CLEAN_UP_INTERVAL, INITIAL_REFRESH_LOAD_INTERVAL};
2021-02-13 15:36:39 +00:00
use crate::data::error::Res;
use crate::helpers::{account_helper, comments_helper, conversations_helper, likes_helper, notifications_helper, posts_helper, user_helper};
2021-02-13 15:36:39 +00:00
/// Start the maintenance thread
pub fn start() -> Res {
std::thread::spawn(clean_up_thread_handler);
Ok(())
}
/// Clean up thread handler
fn clean_up_thread_handler() {
2021-02-14 17:54:39 +00:00
// Let the server start before doing cleanup
std::thread::sleep(INITIAL_REFRESH_LOAD_INTERVAL);
2021-02-13 15:36:39 +00:00
loop {
println!("Start clean up operation");
match do_clean() {
Ok(_) => {
println!("Clean thread executed successfully!");
}
Err(e) => {
eprintln!("Failed to clean data! {:#?}", e);
}
}
std::thread::sleep(CLEAN_UP_INTERVAL);
}
}
/// Do the cleanup
fn do_clean() -> Res {
// Clean old login tokens
account_helper::clean_up_old_access_tokens()?;
2021-02-14 15:45:00 +00:00
// Automatic account cleanup
for user in user_helper::get_all_users()? {
// Clean old likes
likes_helper::clean_old_user_likes(&user)?;
2021-02-14 15:56:48 +00:00
// Clean old notifications
notifications_helper::clean_old_user_notifications(&user)?;
2021-02-14 17:49:47 +00:00
// Clean old comments
comments_helper::clean_old_comments(&user)?;
2021-02-14 18:02:38 +00:00
// Clean old posts
posts_helper::clean_old_posts(&user)?;
// Clean old conversation messages
conversations_helper::clean_old_messages(&user)?;
// Remove the account, if it have been inactive for a long time
account_helper::remove_if_inactive_for_too_long_time(&user)?;
2021-02-14 15:45:00 +00:00
}
2021-02-13 15:36:39 +00:00
Ok(())
}