Move server-specific code into main.rs

This commit is contained in:
Roman Zeyde 2018-06-05 13:40:36 +03:00
parent f6e74a225a
commit 4a8b8fe9a1
No known key found for this signature in database
GPG Key ID: 87CAE5FA46917CBB
4 changed files with 112 additions and 101 deletions

View File

@ -1,14 +1,9 @@
use chan;
use chan_signal;
use error_chain::ChainedError;
use std::sync::Arc;
use std::thread;
use std::time::Duration;
use config::Config;
use {daemon, index, query, rpc, store};
use errors::*;
use {daemon, index, store};
pub struct App {
store: store::DBStore,
@ -17,6 +12,14 @@ pub struct App {
}
impl App {
pub fn new(store: store::DBStore, index: index::Index, daemon: daemon::Daemon) -> Arc<App> {
Arc::new(App {
store,
index,
daemon,
})
}
pub fn write_store(&self) -> &store::WriteStore {
&self.store
}
@ -31,17 +34,17 @@ impl App {
}
}
struct Waiter {
pub struct Waiter {
signal: chan::Receiver<chan_signal::Signal>,
duration: Duration,
}
impl Waiter {
fn new(duration: Duration) -> Waiter {
pub fn new(duration: Duration) -> Waiter {
let signal = chan_signal::notify(&[chan_signal::Signal::INT]);
Waiter { signal, duration }
}
fn wait(&self) -> Option<chan_signal::Signal> {
pub fn wait(&self) -> Option<chan_signal::Signal> {
let signal = &self.signal;
let timeout = chan::after(self.duration);
let result;
@ -52,81 +55,3 @@ impl Waiter {
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()?);
let store = store::DBStore::open(
config.db_path,
store::StoreOptions {
// compact manually after the first run has finished successfully
auto_compact: false,
},
);
let index = index::Index::load(&store);
let mut tip = index.update(&store, &daemon)?;
store.compact_if_needed();
drop(store); // to be re-opened soon
let store = store::DBStore::open(config.db_path, store::StoreOptions { auto_compact: true });
let app = Arc::new(App {
store,
index,
daemon,
});
let query = Arc::new(query::Query::new(app.clone()));
rpc::start(&config.rpc_addr, query.clone());
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(())
}
struct Repeat {
do_restart: bool,
iter_count: usize,
}
impl Repeat {
fn new(config: &Config) -> Repeat {
Repeat {
do_restart: config.restart,
iter_count: 0,
}
}
}
impl Iterator for Repeat {
type Item = ();
fn next(&mut self) -> Option<()> {
self.iter_count += 1;
if self.iter_count == 1 {
return Some(()); // don't sleep before 1st iteration
}
thread::sleep(Duration::from_secs(1));
if self.do_restart {
Some(())
} else {
None
}
}
}
pub fn main() {
let config = Config::from_args();
for _ in Repeat::new(&config) {
match run_server(&config) {
Ok(_) => break,
Err(e) => error!("{}", e.display_chain()),
}
}
}

View File

@ -1,7 +1,93 @@
extern crate electrs;
use electrs::app;
#[macro_use]
extern crate log;
#[macro_use]
extern crate error_chain;
use error_chain::ChainedError;
use std::thread;
use std::time::Duration;
use electrs::{app::{App, Waiter},
config::Config,
daemon::Daemon,
errors::*,
index::Index,
query::Query,
rpc,
store::{DBStore, StoreOptions}};
fn run_server(config: &Config) -> Result<()> {
let daemon = Daemon::new(config.network_type)?;
debug!("{:?}", daemon.getblockchaininfo()?);
let signal = Waiter::new(Duration::from_secs(5));
let store = DBStore::open(
config.db_path,
StoreOptions {
// compact manually after the first run has finished successfully
auto_compact: false,
},
);
let index = Index::load(&store);
let mut tip = index.update(&store, &daemon)?;
store.compact_if_needed();
drop(store); // to be re-opened soon
let store = DBStore::open(config.db_path, StoreOptions { auto_compact: true });
let app = App::new(store, index, daemon);
let query = Query::new(app.clone());
rpc::start(&config.rpc_addr, query.clone());
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(())
}
struct Repeat {
do_restart: bool,
iter_count: usize,
}
impl Repeat {
fn new(config: &Config) -> Repeat {
Repeat {
do_restart: config.restart,
iter_count: 0,
}
}
}
impl Iterator for Repeat {
type Item = ();
fn next(&mut self) -> Option<()> {
self.iter_count += 1;
if self.iter_count == 1 {
return Some(()); // don't sleep before 1st iteration
}
thread::sleep(Duration::from_secs(1));
if self.do_restart {
Some(())
} else {
None
}
}
}
fn main() {
app::main()
let config = Config::from_args();
for _ in Repeat::new(&config) {
match run_server(&config) {
Ok(_) => break,
Err(e) => error!("{}", e.display_chain()),
}
}
}

View File

@ -27,12 +27,12 @@ extern crate serde_derive;
extern crate serde_json;
pub mod app;
mod config;
mod daemon;
mod errors;
mod index;
mod mempool;
mod query;
mod rpc;
mod store;
mod util;
pub mod config;
pub mod daemon;
pub mod errors;
pub mod index;
pub mod mempool;
pub mod query;
pub mod rpc;
pub mod store;
pub mod util;

View File

@ -138,11 +138,11 @@ pub struct Query {
}
impl Query {
pub fn new(app: Arc<App>) -> Query {
Query {
pub fn new(app: Arc<App>) -> Arc<Query> {
Arc::new(Query {
app,
tracker: RwLock::new(Tracker::new()),
}
})
}
fn load_txns_by_prefix(