Stop indexing after SIGINT

This commit is contained in:
Roman Zeyde 2018-06-01 19:08:22 +03:00
parent 48ec215050
commit 98d1dabfff
No known key found for this signature in database
GPG Key ID: 87CAE5FA46917CBB
4 changed files with 33 additions and 5 deletions

View File

@ -9,6 +9,8 @@ arrayref = "0.3"
base64 = "0.9"
bincode = "1.0"
bitcoin = "0.13"
chan = "0.1"
chan-signal = "0.3"
error-chain = "0.11"
hex = "0.3"
log = "0.4"

View File

@ -17,6 +17,5 @@ Flush only on the last write.
= Rust
Use Bytes instead of Vec[u8] when possible
Handle SIGINT gracefully (https://www.reddit.com/r/rust/comments/6swidb/how_to_properly_catch_sigint_in_a_threaded_program/)
Return errors instead of panics
Use generators instead of vectors

View File

@ -1,4 +1,6 @@
use argparse::{ArgumentParser, Store, StoreTrue};
use chan;
use chan_signal;
use error_chain::ChainedError;
use simplelog::LevelFilter;
use std::fs::OpenOptions;
@ -100,7 +102,30 @@ impl App {
}
}
struct Waiter {
signal: chan::Receiver<chan_signal::Signal>,
duration: Duration,
}
impl Waiter {
fn new(duration: Duration) -> Waiter {
let signal = chan_signal::notify(&[chan_signal::Signal::INT]);
Waiter { signal, duration }
}
fn wait(&self) -> Option<chan_signal::Signal> {
let signal = &self.signal;
let timeout = chan::after(self.duration);
let result;
chan_select! {
signal.recv() -> sig => { result = sig; },
timeout.recv() => { result = None; },
}
result
}
}
fn run_server(config: &Config) -> Result<()> {
let signal = Waiter::new(Duration::from_secs(5));
let daemon = daemon::Daemon::new(config.network_type)?;
debug!("{:?}", daemon.getblockchaininfo()?);
@ -124,16 +149,16 @@ fn run_server(config: &Config) -> Result<()> {
});
let query = Arc::new(query::Query::new(app.clone()));
let poll_delay = Duration::from_secs(5);
rpc::start(&config.rpc_addr, query.clone());
loop {
thread::sleep(poll_delay);
while let None = signal.wait() {
query.update_mempool()?;
if tip == app.daemon().getbestblockhash()? {
continue;
}
tip = app.index().update(app.write_store(), app.daemon())?;
}
info!("closing server");
Ok(())
}
fn setup_logging(config: &Config) {

View File

@ -4,6 +4,7 @@ extern crate argparse;
extern crate base64;
extern crate bincode;
extern crate bitcoin;
extern crate chan_signal;
extern crate crypto;
extern crate hex;
extern crate pbr;
@ -11,7 +12,8 @@ extern crate rocksdb;
extern crate serde;
extern crate simplelog;
extern crate time;
#[macro_use]
extern crate chan;
#[macro_use]
extern crate arrayref;
#[macro_use]