Also catch SIGQUIT and SIGTERM and clean up properly

This commit is contained in:
Nadja Reitzenstein 2021-11-26 03:42:30 +01:00
parent 200179f621
commit 9fcb7664aa
3 changed files with 13 additions and 7 deletions

View File

@ -1,11 +1,13 @@
use slog::{Drain, Logger};
use slog_async;
use slog_async::AsyncGuard;
use slog_term::{TermDecorator, FullFormat};
pub fn init() -> Logger {
pub fn init() -> (Logger, AsyncGuard) {
let decorator = TermDecorator::new().build();
let drain = FullFormat::new(decorator).build().fuse();
let drain = slog_async::Async::new(drain).build().fuse();
let (drain, guard) = slog_async::Async::new(drain).build_with_guard();
let drain = drain.fuse();
return slog::Logger::root(drain, o!());
return (slog::Logger::root(drain, o!()), guard);
}

View File

@ -115,7 +115,8 @@ fn main() {
// on.
// TODO: Now would be a really good time to close stdin/out and move logging to syslog
// Log is in an Arc so we can do very cheap clones in closures.
let log = Arc::new(log::init());
let (log, guard) = log::init();
let log = Arc::new(log);
info!(log, "Starting");
match maybe(matches, log.clone()) {
@ -125,9 +126,8 @@ fn main() {
retval = -1;
}
}
drop(guard);
}
std::process::exit(retval);
}
// Returning a `Result` from `main` allows us to use the `?` shorthand.

View File

@ -27,11 +27,15 @@ pub fn serve_api_connections(log: Arc<Logger>, config: Config, db: Databases, nw
-> Result<(), Error>
{
let signal = Box::pin(async {
use signal_hook::consts::signal::*;
let (tx, mut rx) = UnixStream::pair()?;
// Initialize signal handler.
// We currently only care about Ctrl-C so SIGINT it is.
// TODO: Make this do SIGHUP and a few others too. (By cloning the tx end of the pipe)
sigpipe::register(signal_hook::consts::SIGINT, tx.as_raw_fd())?;
let fd = tx.as_raw_fd();
sigpipe::register(SIGINT, fd)?;
sigpipe::register(SIGQUIT, fd)?;
sigpipe::register(SIGTERM, fd)?;
// When a signal is received this future can complete and read a byte from the underlying
// socket — the actual data is discarded but the act of being able to receive data tells us
// that we received a SIGINT.