graceful api shutdown (#3677)

This commit is contained in:
deevope
2022-01-07 14:24:54 +01:00
committed by GitHub
parent 2237f42144
commit c92d2e9fba
8 changed files with 134 additions and 97 deletions
+19 -8
View File
@@ -25,21 +25,27 @@ use crate::config::GlobalConfig;
use crate::p2p::Seeding;
use crate::servers;
use crate::tui::ui;
use futures::channel::oneshot;
use grin_p2p::msg::PeerAddrs;
use grin_p2p::PeerAddr;
use grin_util::logger::LogEntry;
use std::sync::mpsc;
/// wrap below to allow UI to clean up on stop
pub fn start_server(config: servers::ServerConfig, logs_rx: Option<mpsc::Receiver<LogEntry>>) {
start_server_tui(config, logs_rx);
// Just kill process for now, otherwise the process
// hangs around until sigint because the API server
// currently has no shutdown facility
pub fn start_server(
config: servers::ServerConfig,
logs_rx: Option<mpsc::Receiver<LogEntry>>,
api_chan: &'static mut (oneshot::Sender<()>, oneshot::Receiver<()>),
) {
start_server_tui(config, logs_rx, api_chan);
exit(0);
}
fn start_server_tui(config: servers::ServerConfig, logs_rx: Option<mpsc::Receiver<LogEntry>>) {
fn start_server_tui(
config: servers::ServerConfig,
logs_rx: Option<mpsc::Receiver<LogEntry>>,
api_chan: &'static mut (oneshot::Sender<()>, oneshot::Receiver<()>),
) {
// Run the UI controller.. here for now for simplicity to access
// everything it might need
if config.run_tui.unwrap_or(false) {
@@ -53,6 +59,8 @@ fn start_server_tui(config: servers::ServerConfig, logs_rx: Option<mpsc::Receive
});
controller.run(serv);
},
None,
api_chan,
)
.unwrap();
} else {
@@ -73,6 +81,8 @@ fn start_server_tui(config: servers::ServerConfig, logs_rx: Option<mpsc::Receive
warn!("Received SIGINT (Ctrl+C) or SIGTERM (kill).");
serv.stop();
},
None,
api_chan,
)
.unwrap();
}
@@ -86,6 +96,7 @@ pub fn server_command(
server_args: Option<&ArgMatches<'_>>,
global_config: GlobalConfig,
logs_rx: Option<mpsc::Receiver<LogEntry>>,
api_chan: &'static mut (oneshot::Sender<()>, oneshot::Receiver<()>),
) -> i32 {
// just get defaults from the global config
let mut server_config = global_config.members.as_ref().unwrap().server.clone();
@@ -118,7 +129,7 @@ pub fn server_command(
if let Some(a) = server_args {
match a.subcommand() {
("run", _) => {
start_server(server_config, logs_rx);
start_server(server_config, logs_rx, api_chan);
}
("", _) => {
println!("Subcommand required, use 'grin help server' for details");
@@ -132,7 +143,7 @@ pub fn server_command(
}
}
} else {
start_server(server_config, logs_rx);
start_server(server_config, logs_rx, api_chan);
}
0
}
+6 -2
View File
@@ -23,6 +23,7 @@ use crate::config::config::SERVER_CONFIG_FILE_NAME;
use crate::core::global;
use crate::util::init_logger;
use clap::App;
use futures::channel::oneshot;
use grin_api as api;
use grin_chain as chain;
use grin_config as config;
@@ -127,6 +128,9 @@ fn real_main() -> i32 {
let mut logging_config = config.members.as_ref().unwrap().logging.clone().unwrap();
logging_config.tui_running = config.members.as_ref().unwrap().server.run_tui;
let api_chan: &'static mut (oneshot::Sender<()>, oneshot::Receiver<()>) =
Box::leak(Box::new(oneshot::channel::<()>()));
let (logs_tx, logs_rx) = if logging_config.tui_running.unwrap() {
let (logs_tx, logs_rx) = mpsc::sync_channel::<LogEntry>(200);
(Some(logs_tx), Some(logs_rx))
@@ -177,7 +181,7 @@ fn real_main() -> i32 {
match args.subcommand() {
// server commands and options
("server", Some(server_args)) => {
cmd::server_command(Some(server_args), node_config.unwrap(), logs_rx)
cmd::server_command(Some(server_args), node_config.unwrap(), logs_rx, api_chan)
}
// client commands and options
@@ -196,6 +200,6 @@ fn real_main() -> i32 {
// If nothing is specified, try to just use the config file instead
// this could possibly become the way to configure most things
// with most command line options being phased out
_ => cmd::server_command(None, node_config.unwrap(), logs_rx),
_ => cmd::server_command(None, node_config.unwrap(), logs_rx, api_chan),
}
}