2023-10-12 13:11:09 +02:00
|
|
|
use state::State;
|
|
|
|
|
2023-10-11 16:06:07 +02:00
|
|
|
|
|
|
|
mod clean;
|
|
|
|
mod state;
|
|
|
|
mod env;
|
2023-10-12 12:39:28 +02:00
|
|
|
mod file;
|
2023-10-11 16:06:07 +02:00
|
|
|
|
|
|
|
pub fn redis_conn(env: env::Env) -> Result<redis::Client, redis::RedisError> {
|
|
|
|
log::info!("Connecting to redis DB on {}", env.redis.host);
|
|
|
|
redis::Client::open(format!("redis://:{}@{}:{}/", env.redis.pass, env.redis.host, env.redis.port))
|
|
|
|
}
|
|
|
|
|
2023-10-12 13:11:09 +02:00
|
|
|
#[derive(Debug, Clone)]
|
2023-10-12 14:12:40 +02:00
|
|
|
#[allow(dead_code)]
|
2023-10-12 13:11:09 +02:00
|
|
|
enum CleanResult {
|
|
|
|
Skip,
|
|
|
|
Break,
|
2023-10-12 14:12:40 +02:00
|
|
|
ScheduleNext(chrono::DateTime<chrono::Local>)
|
2023-10-12 13:11:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn clean(env: env::Env, state: State) -> CleanResult {
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
log::debug!("Initiating clean process");
|
|
|
|
|
|
|
|
let res = clean::clean(state.clone()).await;
|
|
|
|
if res.is_err() {
|
|
|
|
log::error!("Error while cleaning: {}", res.unwrap_err());
|
2023-10-12 14:12:40 +02:00
|
|
|
return CleanResult::ScheduleNext(chrono::Local::now() + env.clean_errdel);
|
2023-10-12 13:11:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(debug_assertions)] {
|
|
|
|
log::debug!("Cleaned successfully");
|
|
|
|
}
|
2023-10-22 08:42:26 +02:00
|
|
|
|
|
|
|
CleanResult::ScheduleNext(chrono::Local::now() + env.clean_del)
|
2023-10-12 13:11:09 +02:00
|
|
|
}
|
|
|
|
|
2023-10-11 16:06:07 +02:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
2023-10-12 12:39:28 +02:00
|
|
|
#[cfg(debug_assertions)] {
|
|
|
|
femme::with_level(log::LevelFilter::Debug);
|
|
|
|
}
|
|
|
|
#[cfg(not(debug_assertions))] {
|
|
|
|
femme::with_level(log::LevelFilter::Info);
|
|
|
|
}
|
|
|
|
|
2023-10-11 16:06:07 +02:00
|
|
|
dotenvy::dotenv().unwrap();
|
|
|
|
|
|
|
|
let env = crate::env::Env::load().unwrap();
|
2023-10-12 13:11:09 +02:00
|
|
|
let state = crate::state::State {
|
2023-10-12 12:39:28 +02:00
|
|
|
redis: redis_conn(env.clone()).unwrap(),
|
|
|
|
env: env.clone()
|
2023-10-11 16:06:07 +02:00
|
|
|
};
|
|
|
|
|
2023-10-12 13:11:09 +02:00
|
|
|
log::info!("Initalizing initial clean");
|
|
|
|
let cl = clean(env.clone(), state.clone()).await;
|
|
|
|
log::info!("Initial clean exited with status {:?}", cl);
|
2023-10-12 12:39:28 +02:00
|
|
|
|
2023-10-12 13:11:09 +02:00
|
|
|
loop {
|
|
|
|
let res = clean(env.clone(), state.clone()).await;
|
|
|
|
|
|
|
|
match res {
|
|
|
|
CleanResult::Break => {
|
|
|
|
break
|
|
|
|
},
|
2023-10-12 14:12:40 +02:00
|
|
|
CleanResult::ScheduleNext(next) => {
|
|
|
|
#[cfg(debug_assertions)] {
|
|
|
|
log::debug!("Next run is scheduled at {} ({})", next.format("%d-%m-%Y %H:%M:%S"), next.timestamp());
|
|
|
|
}
|
|
|
|
tokio::time::sleep((next - chrono::Local::now()).to_std().unwrap()).await;
|
|
|
|
},
|
2023-10-12 13:11:09 +02:00
|
|
|
_ => {}
|
2023-10-11 16:06:07 +02:00
|
|
|
}
|
|
|
|
}
|
2023-10-12 13:11:09 +02:00
|
|
|
|
|
|
|
log::info!("Main loop broke");
|
2023-10-11 16:06:07 +02:00
|
|
|
}
|