Simplified the ctrl-c handler and .env file loading.

This commit is contained in:
DrPeterVanNostrand 2018-12-11 18:58:56 +00:00
parent 3e10c68293
commit fc28738f32
5 changed files with 37 additions and 21 deletions

1
Cargo.lock generated
View File

@ -1031,6 +1031,7 @@ dependencies = [
"failure 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
"jsonrpc-core 8.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lettre 0.9.0 (git+https://github.com/lettre/lettre.git)",
"lettre_email 0.9.0 (git+https://github.com/lettre/lettre.git)",
"native-tls 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -15,6 +15,7 @@ ethereum-types = "0.4.0"
failure = "0.1.2"
hex = "0.3.2"
jsonrpc-core = "8.0.1"
lazy_static = "1.2.0"
# TODO: after `lettre` and `lettre_email` v0.9.x have been published to crates.io, remove these
# GitHub dependencies.
lettre = { git = "https://github.com/lettre/lettre.git" }

View File

@ -10,7 +10,7 @@ pub type Result<T> = ::std::result::Result<T, Error>;
#[derive(Debug)]
pub enum Error {
CtrlcError(ctrlc::Error),
CtrlcSetupError(ctrlc::Error),
EmissionFundsV1ContractDoesNotExist,
FailedToBuildEmail(failure::Error),
FailedToBuildRequest(reqwest::Error),

View File

@ -181,7 +181,7 @@ impl Logger {
self.increment_log_count();
}
pub fn log_ctrlc(&mut self) {
pub fn log_ctrlc_pressed(&mut self) {
warn!(&self.logger, "recieved ctrl-c signal, gracefully shutting down...");
self.increment_log_count();
}

View File

@ -7,6 +7,8 @@ extern crate ethereum_types;
extern crate failure;
extern crate hex;
extern crate jsonrpc_core;
#[macro_use]
extern crate lazy_static;
extern crate lettre;
extern crate lettre_email;
extern crate native_tls;
@ -37,23 +39,41 @@ use error::{Error, Result};
use logger::Logger;
use notify::{Notification, Notifier};
lazy_static! {
// Tracks whether or not the .env file has been loaded.
static ref LOADED_ENV_FILE: AtomicBool = AtomicBool::new(false);
}
/// Attempts to load the .env file one time at the start of the main process or at the start of the
/// tests. Panics if the .env file cannot be found or parsed.
fn load_env_file() {
if let Err(e) = dotenv::dotenv() {
match e {
dotenv::Error::Io(_) => panic!("could not find .env file"),
_ => panic!("coule not parse .env file"),
if !LOADED_ENV_FILE.load(Ordering::Relaxed) {
match dotenv::dotenv() {
Ok(_) => LOADED_ENV_FILE.store(true, Ordering::Relaxed),
Err(dotenv::Error::Io(_)) => panic!("could not find .env file"),
_ => panic!("could not parse .env file"),
};
}
}
/// Sets up ctrl-c to change the value of `poagov_is_running` from `true` to `false`. When
/// `poagov_is_running` changes to `false`, the process of gracefully shutting down begins. The
/// `AtomicBool` returned by this function is used to indicate whether or not the `poagov` binary
/// should continue running.
fn set_ctrlc_handler(logger: Arc<Mutex<Logger>>) -> Result<Arc<AtomicBool>> {
let running = Arc::new(AtomicBool::new(true));
let result = Ok(running.clone());
ctrlc::set_handler(move || {
logger.lock().unwrap().log_ctrlc();
running.store(false, Ordering::SeqCst);
}).map_err(|e| Error::CtrlcError(e))?;
result
let poagov_is_running = Arc::new(AtomicBool::new(true));
let setup_res = {
let poagov_is_running = poagov_is_running.clone();
ctrlc::set_handler(move || {
logger.lock().unwrap().log_ctrlc_pressed();
poagov_is_running.store(false, Ordering::SeqCst);
})
};
if let Err(e) = setup_res {
Err(Error::CtrlcSetupError(e))
} else {
Ok(poagov_is_running)
}
}
fn main() -> Result<()> {
@ -111,14 +131,8 @@ fn main() -> Result<()> {
pub mod tests {
use super::load_env_file;
static mut LOADED_ENV_FILE: bool = false;
/// Loads the .env file once at the start of the tests.
pub fn setup() {
unsafe {
if !LOADED_ENV_FILE {
load_env_file();
LOADED_ENV_FILE = true;
}
}
load_env_file();
}
}