Handle SIGINT and SIGTERM (#1180)

* Handle SIGINT and SIGTERM
This commit is contained in:
Quentin Le Sceller
2018-06-20 23:15:30 -04:00
committed by GitHub
parent 70ba1c838c
commit 9f8c7cf1b4
4 changed files with 92 additions and 40 deletions
+20 -2
View File
@@ -17,6 +17,7 @@
extern crate blake2_rfc as blake2;
#[macro_use]
extern crate clap;
extern crate ctrlc;
extern crate cursive;
extern crate daemonize;
extern crate serde;
@@ -39,6 +40,7 @@ pub mod tui;
use std::env::current_dir;
use std::process::exit;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::thread;
use std::time::Duration;
@@ -103,17 +105,33 @@ fn start_server_tui(config: servers::ServerConfig) {
if config.run_tui.is_some() && config.run_tui.unwrap() {
println!("Starting GRIN in UI mode...");
servers::Server::start(config, |serv: Arc<servers::Server>| {
let running = Arc::new(AtomicBool::new(true));
let r = running.clone();
let _ = thread::Builder::new()
.name("ui".to_string())
.spawn(move || {
let mut controller = ui::Controller::new().unwrap_or_else(|e| {
panic!("Error loading UI controller: {}", e);
});
controller.run(serv.clone());
controller.run(serv.clone(), r);
});
ctrlc::set_handler(move || {
running.store(false, Ordering::SeqCst);
}).expect("Error setting Ctrl-C handler");
}).unwrap();
} else {
servers::Server::start(config, |_| {}).unwrap();
servers::Server::start(config, |serv: Arc<servers::Server>| {
let running = Arc::new(AtomicBool::new(true));
let r = running.clone();
ctrlc::set_handler(move || {
r.store(false, Ordering::SeqCst);
}).expect("Error setting Ctrl-C handler");
while running.load(Ordering::SeqCst) {
thread::sleep(Duration::from_secs(1));
}
warn!(LOGGER, "Received SIGINT (Ctrl+C).");
serv.stop();
}).unwrap();
}
}
+10 -1
View File
@@ -15,6 +15,7 @@
//! Basic TUI to better output the overall system status and status
//! of various subsystems
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{mpsc, Arc};
use time;
@@ -33,6 +34,7 @@ use servers::Server;
use tui::constants::ROOT_STACK;
use tui::types::{TUIStatusListener, UIMessage};
use tui::{menu, mining, peers, status, version};
use util::LOGGER;
use built_info;
@@ -161,15 +163,21 @@ impl Controller {
})
}
/// Run the controller
pub fn run(&mut self, server: Arc<Server>) {
pub fn run(&mut self, server: Arc<Server>, running: Arc<AtomicBool>) {
let stat_update_interval = 1;
let mut next_stat_update = time::get_time().sec + stat_update_interval;
while self.ui.step() {
if !running.load(Ordering::SeqCst) {
warn!(LOGGER, "Received SIGINT (Ctrl+C).");
server.stop();
self.ui.stop();
}
while let Some(message) = self.rx.try_iter().next() {
match message {
ControllerMessage::Shutdown => {
server.stop();
self.ui.stop();
running.store(false, Ordering::SeqCst)
/*self.ui
.ui_tx
.send(UIMessage::UpdateOutput("update".to_string()))
@@ -177,6 +185,7 @@ impl Controller {
}
}
}
if time::get_time().sec > next_stat_update {
let stats = server.get_server_stats().unwrap();
self.ui.ui_tx.send(UIMessage::UpdateStatus(stats)).unwrap();