Properly stop api server

This commit is contained in:
Gregor Reitzenstein 2020-12-16 12:27:34 +01:00
parent be73385758
commit 7bdbdac86b
2 changed files with 8 additions and 2 deletions

View File

@ -175,8 +175,11 @@ fn maybe(matches: clap::ArgMatches, log: Arc<Logger>) -> Result<(), Error> {
// TODO: Spawn api connections on their own (non-main) thread, use the main thread to
// handle signals (a cli if stdin is not closed?) and make it stop and clean up all threads
// when bffh should exit
server::serve_api_connections(log.clone(), config, db, network)
// Signal is dropped here, stopping all executor threads as well.
let r = server::serve_api_connections(log.clone(), config, db, network);
signal.try_send(());
std::mem::drop(signal);
return r;
});
return r;

View File

@ -54,6 +54,7 @@ pub fn serve_api_connections(log: Arc<Logger>, config: Settings, db: Databases,
.map(|l| {
let addr = l.address.clone();
let port = l.port.unwrap_or(config::DEFAULT_PORT);
info!(&log, "Binding to {} port {}.", l.address.as_str(), &port);
TcpListener::bind((l.address.as_str(), port))
// If the bind errors, include the address so we can log it
// Since this closure is lazy we need to have a cloned addr
@ -124,6 +125,8 @@ pub fn serve_api_connections(log: Arc<Logger>, config: Settings, db: Databases,
return LoopResult::Continue;
});
info!(&log, "Started");
// Check each signal as it arrives
let handle_signals = signal.map(|r| { r.unwrap() }).into_stream();