re-enable synchronous mode more robustly through a drop guard

feat/vaults
Tomáš Mládek 2021-12-05 23:31:19 +01:00
parent b49315e22b
commit 7adaecdafc
1 changed files with 24 additions and 2 deletions

View File

@ -78,6 +78,26 @@ pub async fn rescan_vault(
.unwrap();
}
}
struct PragmaSynchronousGuard<'a>(&'a DbPool);
impl Drop for PragmaSynchronousGuard<'_> {
fn drop(&mut self) {
debug!("Re-enabling synchronous mode.");
let connection = self.0.get();
let res: Result<_, String> = match connection {
Ok(connection) => connection
.execute("PRAGMA synchronous = NORMAL;")
.map_err(|err| format!("{}", err)),
Err(err) => Err(format!("{}", err)),
};
if let Err(err) = res {
error!(
"Error setting synchronous mode back to NORMAL! Data loss possible! {}",
err
);
}
}
}
type UpdatePathResult = Result<UpdatePathOutcome>;
@ -105,6 +125,7 @@ fn _rescan_vault<T: AsRef<Path>>(
// Disable syncing in SQLite for the duration of the import
debug!("Disabling SQLite synchronous mode");
pool.get()?.execute("PRAGMA synchronous = OFF;")?;
let _guard = PragmaSynchronousGuard(&pool);
// Walk through the vault, find all paths
debug!("Traversing vault directory");
@ -153,6 +174,8 @@ fn _rescan_vault<T: AsRef<Path>>(
})
.collect();
debug!("Processing done, cleaning up...");
let existing_files = existing_files.read().unwrap();
let connection = pool.get()?;
@ -199,8 +222,7 @@ fn _rescan_vault<T: AsRef<Path>>(
}
// Re-enable SQLite syncing
debug!("Re-enabling synchronous mode.");
pool.get()?.execute("PRAGMA synchronous = NORMAL;")?;
drop(_guard);
info!(
"Finished updating {} ({} created, {} deleted, {} left unchanged). Took {}s.",