fix the deletion of the data.ms in case of errors

This commit is contained in:
Tamo 2022-10-16 03:04:17 +02:00 committed by Clément Renault
parent e9295c03ce
commit 554600dfd8
No known key found for this signature in database
GPG Key ID: 92ADA4E935E71FA4
2 changed files with 17 additions and 13 deletions

View File

@ -66,7 +66,6 @@ pub fn setup_meilisearch(opt: &Opt) -> anyhow::Result<(IndexScheduler, AuthContr
// we don't want to create anything in the data.ms yet, thus we // we don't want to create anything in the data.ms yet, thus we
// wrap our two builders in a closure that'll be executed later. // wrap our two builders in a closure that'll be executed later.
let auth_controller_builder = || AuthController::new(&opt.db_path, &opt.master_key); let auth_controller_builder = || AuthController::new(&opt.db_path, &opt.master_key);
let index_scheduler_builder = || { let index_scheduler_builder = || {
IndexScheduler::new( IndexScheduler::new(
opt.db_path.join("tasks"), opt.db_path.join("tasks"),
@ -80,6 +79,19 @@ pub fn setup_meilisearch(opt: &Opt) -> anyhow::Result<(IndexScheduler, AuthContr
todo!("We'll see later"), todo!("We'll see later"),
) )
}; };
let meilisearch = || -> anyhow::Result<_> {
// if anything wrong happens we delete the `data.ms` entirely.
match (
index_scheduler_builder().map_err(anyhow::Error::from),
auth_controller_builder().map_err(anyhow::Error::from),
) {
(Ok(i), Ok(a)) => Ok((i, a)),
(Err(e), _) | (_, Err(e)) => {
std::fs::remove_dir_all(&opt.db_path)?;
Err(e)
}
}
};
let (index_scheduler, auth_controller) = if let Some(ref _path) = opt.import_snapshot { let (index_scheduler, auth_controller) = if let Some(ref _path) = opt.import_snapshot {
// handle the snapshot with something akin to the dumps // handle the snapshot with something akin to the dumps
@ -90,8 +102,7 @@ pub fn setup_meilisearch(opt: &Opt) -> anyhow::Result<(IndexScheduler, AuthContr
let src_path_exists = path.exists(); let src_path_exists = path.exists();
if empty_db && src_path_exists { if empty_db && src_path_exists {
let mut index_scheduler = index_scheduler_builder()?; let (mut index_scheduler, mut auth_controller) = meilisearch()?;
let mut auth_controller = auth_controller_builder()?;
import_dump( import_dump(
&opt.db_path, &opt.db_path,
path, path,
@ -109,8 +120,7 @@ pub fn setup_meilisearch(opt: &Opt) -> anyhow::Result<(IndexScheduler, AuthContr
} else if !src_path_exists && !opt.ignore_missing_dump { } else if !src_path_exists && !opt.ignore_missing_dump {
bail!("dump doesn't exist at {:?}", path) bail!("dump doesn't exist at {:?}", path)
} else { } else {
let mut index_scheduler = index_scheduler_builder()?; let (mut index_scheduler, mut auth_controller) = meilisearch()?;
let mut auth_controller = auth_controller_builder()?;
import_dump( import_dump(
&opt.db_path, &opt.db_path,
path, path,
@ -120,7 +130,7 @@ pub fn setup_meilisearch(opt: &Opt) -> anyhow::Result<(IndexScheduler, AuthContr
(index_scheduler, auth_controller) (index_scheduler, auth_controller)
} }
} else { } else {
(index_scheduler_builder()?, auth_controller_builder()?) meilisearch()?
}; };
/* /*

View File

@ -48,13 +48,7 @@ async fn main() -> anyhow::Result<()> {
_ => unreachable!(), _ => unreachable!(),
} }
let (index_scheduler, auth_controller) = match setup_meilisearch(&opt) { let (index_scheduler, auth_controller) = setup_meilisearch(&opt)?;
Ok(ret) => ret,
Err(e) => {
std::fs::remove_dir_all(opt.db_path)?;
return Err(e);
}
};
#[cfg(all(not(debug_assertions), feature = "analytics"))] #[cfg(all(not(debug_assertions), feature = "analytics"))]
let analytics = if !opt.no_analytics { let analytics = if !opt.no_analytics {