add os error handling
This commit is contained in:
parent
06d295cf49
commit
9b1e78a21c
|
@ -42,17 +42,17 @@ async fn check_key(key: String, mut client: redis::Client) -> bool {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn check_file(file: String, keys: Vec<String>, prefix: String) -> bool {
|
pub async fn check_file(file: String, keys: Vec<String>, prefix: String) -> Result<bool, String> {
|
||||||
if ! keys.iter().find(|x| x.chars().skip(prefix.len() + 4 + 2).collect::<String>() == file).is_none() {
|
if ! keys.iter().find(|x| x.chars().skip(prefix.len() + 4 + 2).collect::<String>() == file).is_none() {
|
||||||
#[cfg(debug_assertions)] {
|
#[cfg(debug_assertions)] {
|
||||||
log::debug!("File {file} is marked for deletion because it exists in the filesystem, but is not in the database");
|
log::debug!("File {file} is marked for deletion because it exists in the filesystem, but is not in the database");
|
||||||
}
|
}
|
||||||
let _ = tokio::fs::remove_file(file).await;
|
let _ = tokio::fs::remove_file(file).await.map_err(|err| err.to_string())?;
|
||||||
return true
|
return Ok(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
false
|
Ok(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
// check that all files in filesystem exist in the database
|
// check that all files in filesystem exist in the database
|
||||||
|
@ -78,12 +78,11 @@ pub async fn fskeep(state: State) -> Result<(), Box<dyn Error>> {
|
||||||
|
|
||||||
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();
|
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
log::debug!("Got {} DB objects", files.len());
|
log::debug!("Got {} DB objects", files.len());
|
||||||
|
|
||||||
let mut set: JoinSet<bool> = JoinSet::new();
|
let mut set: JoinSet<Result<bool, String>> = JoinSet::new();
|
||||||
|
|
||||||
for file in files {
|
for file in files {
|
||||||
set.spawn(check_file(file, keys.clone(), state.env.redis.prefix.clone()));
|
set.spawn(check_file(file, keys.clone(), state.env.redis.prefix.clone()));
|
||||||
|
@ -92,13 +91,26 @@ pub async fn fskeep(state: State) -> Result<(), Box<dyn Error>> {
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
let mut del_count = 0_u32;
|
let mut del_count = 0_u32;
|
||||||
|
|
||||||
while let Some(_deleted) = set.join_next().await {
|
let mut errors: Vec<String> = vec![];
|
||||||
#[cfg(debug_assertions)] {
|
|
||||||
if _deleted.is_ok() {
|
while let Some(deleted) = set.join_next().await {
|
||||||
if _deleted.unwrap() {
|
if let Ok(deleted) = deleted {
|
||||||
del_count += 1;
|
if let Ok(deleted) = deleted {
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
if deleted {
|
||||||
|
del_count += 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if let Err(err) = deleted {
|
||||||
|
errors.push(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if errors.len() != 0 {
|
||||||
|
log::error!("Got the following errors while deleting files:");
|
||||||
|
for error in errors.iter() {
|
||||||
|
log::error!("\t {}", error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue