Run until signal

This commit is contained in:
Nadja Reitzenstein 2022-03-11 22:43:50 +01:00
parent 4489f710d8
commit c54b44e720
2 changed files with 19 additions and 4 deletions

View File

@ -43,6 +43,7 @@ use std::path::Path;
use std::sync::Arc; use std::sync::Arc;
use anyhow::Context; use anyhow::Context;
use futures_rustls::TlsAcceptor; use futures_rustls::TlsAcceptor;
use futures_util::StreamExt;
use rustls::{Certificate, KeyLogFile, PrivateKey, ServerConfig}; use rustls::{Certificate, KeyLogFile, PrivateKey, ServerConfig};
use rustls::server::NoClientAuth; use rustls::server::NoClientAuth;
use signal_hook::consts::signal::*; use signal_hook::consts::signal::*;
@ -75,18 +76,32 @@ impl Diflouroborane {
self.log_version_number(); self.log_version_number();
let signals = signal_hook_async_std::Signals::new(&[ let mut signals = signal_hook_async_std::Signals::new(&[
SIGINT, SIGINT,
SIGQUIT, SIGQUIT,
SIGTERM, SIGTERM,
]).context("Failed to construct signal handler")?; ]).context("Failed to construct signal handler")?;
tracing::debug!("Set up signal handler");
let tlsconfig = TlsConfig::new(config.tlskeylog.as_ref(), !config.is_quiet())?; let tlsconfig = TlsConfig::new(config.tlskeylog.as_ref(), !config.is_quiet())?;
let acceptor = tlsconfig.make_tls_acceptor(&config.tlsconfig)?; let acceptor = tlsconfig.make_tls_acceptor(&config.tlsconfig)?;
APIServer::bind(self.executor.clone(), &config.listens, acceptor); let mut apiserver = self.executor.run(APIServer::bind(self.executor.clone(), &config.listens, acceptor))?;
let (mut tx, rx) = async_oneshot::oneshot();
self.executor.spawn(apiserver.handle_until(rx));
let f = async {
let mut sig = None;
while {
sig = signals.next().await;
sig.is_none()
} {}
tracing::info!(signal = %sig.unwrap(), "Received signal");
tx.send(());
};
self.executor.run(f);
Ok(()) Ok(())
} }
} }

View File

@ -131,7 +131,7 @@ fn main() -> anyhow::Result<()> {
config.log_format = matches.value_of("log format").unwrap_or("Full").to_string(); config.log_format = matches.value_of("log format").unwrap_or("Full").to_string();
let mut bffh = Diflouroborane::new(); let mut bffh = Diflouroborane::new();
bffh.setup(&config); bffh.setup(&config)?;
} }
Ok(()) Ok(())