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,36 +110,44 @@ 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);
let mut set: JoinSet<bool> = JoinSet::new(); if objects != 0 {
for key in keys { let mut set: JoinSet<bool> = JoinSet::new();
set.spawn(check_key(key, redis.clone())); for key in keys {
} set.spawn(check_key(key, redis.clone()));
#[cfg(debug_assertions)]
let mut del_count = 0_u32;
while let Some(_deleted) = set.join_next().await {
#[cfg(debug_assertions)] {
if _deleted.is_ok() {
if _deleted.unwrap() {
del_count += 1;
}
}
} }
} #[cfg(debug_assertions)]
let mut del_count = 0_u32;
#[cfg(debug_assertions)] { while let Some(_deleted) = set.join_next().await {
log::debug!("Deleted {} objects", del_count);
log::debug!("Finished checking the DB, checking the filesystem..."); #[cfg(debug_assertions)] {
if _deleted.is_ok() {
if _deleted.unwrap() {
del_count += 1;
}
}
}
}
#[cfg(debug_assertions)] {
log::debug!("Deleted {} objects", del_count);
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?;