Log error that causes `flush_db` to fail

next
Sayan Nandan 3 years ago
parent 3407f1e971
commit 39ab1e7683

@ -46,6 +46,16 @@ use std::sync::Arc;
use tokio; use tokio;
use tokio::sync::Notify; use tokio::sync::Notify;
#[macro_export]
macro_rules! flush_db {
($db:expr) => {
crate::coredb::CoreDB::flush_db(&$db, None)
};
($db:expr, $file:expr) => {
crate::coredb::CoreDB::flush_db(&$db, Some($file))
};
}
/// This is a thread-safe database handle, which on cloning simply /// This is a thread-safe database handle, which on cloning simply
/// gives another atomic reference to the `shared` which is a `Shared` object /// gives another atomic reference to the `shared` which is a `Shared` object
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -356,12 +366,16 @@ impl CoreDB {
self.shared.table.read() self.shared.table.read()
} }
/// Flush the contents of the in-memory table onto disk /// Flush the contents of the in-memory table onto disk
pub fn flush_db(&self) -> TResult<()> { pub fn flush_db(&self, file: Option<flock::FileLock>) -> TResult<()> {
let data = match self.acquire_write() { let data = match self.acquire_write() {
Some(wlock) => wlock, Some(wlock) => wlock,
None => return Err("Can no longer flush data; coretable is poisoned".into()), None => return Err("Can no longer flush data; coretable is poisoned".into()),
}; };
if let Some(mut file) = file {
diskstore::flush_data(&mut file, &data.coremap)?;
} else {
diskstore::write_to_disk(&PERSIST_FILE, &data.coremap)?; diskstore::write_to_disk(&PERSIST_FILE, &data.coremap)?;
}
Ok(()) Ok(())
} }

@ -62,6 +62,7 @@ use tokio::sync::Semaphore;
use tokio::sync::{broadcast, mpsc}; use tokio::sync::{broadcast, mpsc};
pub mod connection; pub mod connection;
mod tls; mod tls;
use crate::flush_db;
/// Responsible for gracefully shutting down the server instead of dying randomly /// Responsible for gracefully shutting down the server instead of dying randomly
// Sounds very sci-fi ;) // Sounds very sci-fi ;)
@ -390,29 +391,28 @@ pub async fn run(
} }
} }
server.finish_with_termsig().await; server.finish_with_termsig().await;
if let Ok(_) = db.flush_db() { if let Some(mut lock) = lock {
log::info!("Successfully saved data to disk"); if let Err(e) = lock.unlock() {
() log::error!("Failed to release lock on data file with '{}'", e);
} else { process::exit(0x100);
log::error!("Failed to flush data to disk"); }
}
if let Err(e) = flush_db!(db) {
log::error!("Failed to flush data to disk with '{}'", e);
loop { loop {
// Keep looping until we successfully write the in-memory table to disk // Keep looping until we successfully write the in-memory table to disk
log::warn!("Press enter to try again..."); log::warn!("Press enter to try again...");
io::stdout().flush().unwrap(); io::stdout().flush().unwrap();
io::stdin().read(&mut [0]).unwrap(); io::stdin().read(&mut [0]).unwrap();
if let Ok(_) = db.flush_db() { if let Ok(_) = flush_db!(db) {
log::info!("Successfully saved data to disk"); log::info!("Successfully saved data to disk");
break; break;
} else { } else {
continue; continue;
} }
} }
} } else {
if let Some(mut lock) = lock { log::info!("Successfully saved data to disk");
if let Err(e) = lock.unlock() {
log::error!("Failed to release lock on data file with '{}'", e);
process::exit(0x100);
}
} }
terminal::write_info("Goodbye :)\n").unwrap(); terminal::write_info("Goodbye :)\n").unwrap();
} }

Loading…
Cancel
Save