2021-02-13 15:36:39 +00:00
|
|
|
//! # Cleanup thread
|
|
|
|
//!
|
|
|
|
//! This module manage a thread that automatically delete old data
|
|
|
|
//!
|
|
|
|
//! @author Pierre Hubert
|
|
|
|
|
|
|
|
use crate::constants::CLEAN_UP_INTERVAL;
|
|
|
|
use crate::data::error::Res;
|
2021-02-14 15:56:48 +00:00
|
|
|
use crate::helpers::{account_helper, likes_helper, notifications_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() {
|
|
|
|
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 15:45:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-13 15:36:39 +00:00
|
|
|
Ok(())
|
|
|
|
}
|