add optimizations when nothing to do

This commit is contained in:
blek 2023-10-27 00:04:11 +10:00
parent e91e40e0fc
commit 06d295cf49
Signed by: blek
GPG Key ID: 14546221E3595D0C
1 changed files with 43 additions and 25 deletions

View File

@ -57,9 +57,6 @@ pub async fn check_file(file: String, keys: Vec<String>, prefix: String) -> bool
// check that all files in filesystem exist in the database // check that all files in filesystem exist in the database
pub async fn fskeep(state: State) -> Result<(), Box<dyn Error>> { pub async fn fskeep(state: State) -> Result<(), Box<dyn Error>> {
let mut redis = state.redis.clone();
let keys: Vec<String> = redis.keys(format!("{}*", state.env.redis.prefix))?;
let objects = keys.len();
let mut files_s = tokio::fs::read_dir(state.env.usercont_dir).await?; let mut files_s = tokio::fs::read_dir(state.env.usercont_dir).await?;
let mut files: Vec<String> = vec![]; let mut files: Vec<String> = vec![];
@ -71,7 +68,20 @@ pub async fn fskeep(state: State) -> Result<(), Box<dyn Error>> {
} }
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
log::debug!("Got {} objects", objects); log::debug!("Got {} filesystem objects", files.len());
if files.len() == 0 {
#[cfg(debug_assertions)]
log::debug!("Nothing to do, optimizing away the fs cleanup");
return Ok(());
}
let mut redis = state.redis.clone();
let keys: Vec<String> = redis.keys(format!("{}*", state.env.redis.prefix))?;
let objects = keys.len();
#[cfg(debug_assertions)]
log::debug!("Got {} DB objects", files.len());
let mut set: JoinSet<bool> = JoinSet::new(); let mut set: JoinSet<bool> = JoinSet::new();
@ -100,13 +110,17 @@ pub async fn fskeep(state: State) -> Result<(), Box<dyn Error>> {
pub async fn clean(state: State) -> Result<(), Box<dyn Error>> { pub async fn clean(state: State) -> Result<(), Box<dyn Error>> {
#[cfg(debug_assertions)]
log::debug!("Clean process started");
let mut redis = state.redis.clone(); let mut redis = state.redis.clone();
let keys: Vec<String> = redis.keys(format!("{}*", state.env.redis.prefix))?; let keys: Vec<String> = redis.keys(format!("{}*", state.env.redis.prefix))?;
let objects = keys.len(); let objects = keys.len();
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
log::debug!("Got {} objects", objects); log::debug!("Got {} DB objects", objects);
if objects != 0 {
let mut set: JoinSet<bool> = JoinSet::new(); let mut set: JoinSet<bool> = JoinSet::new();
for key in keys { for key in keys {
set.spawn(check_key(key, redis.clone())); set.spawn(check_key(key, redis.clone()));
@ -131,6 +145,10 @@ pub async fn clean(state: State) -> Result<(), Box<dyn Error>> {
log::debug!("Deleted {} objects", del_count); log::debug!("Deleted {} objects", del_count);
log::debug!("Finished checking the DB, checking the filesystem..."); log::debug!("Finished checking the DB, checking the filesystem...");
} }
} else {
#[cfg(debug_assertions)]
log::debug!("Nothing to do, optimizing away the DB cleanup");
}
fskeep(state).await?; fskeep(state).await?;