From e3f41f2165a73e82a0c0e2bab352c999932b3e44 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Mon, 8 Apr 2019 14:00:23 +0200 Subject: [PATCH] src/tools/daemon.rs: use new ServerState handler --- src/bin/proxmox-backup-api.rs | 12 +++++++- src/bin/proxmox-backup-proxy.rs | 15 ++++++++-- src/tools/daemon.rs | 49 ++++++++++----------------------- 3 files changed, 38 insertions(+), 38 deletions(-) diff --git a/src/bin/proxmox-backup-api.rs b/src/bin/proxmox-backup-api.rs index ec942d5e..37d950ae 100644 --- a/src/bin/proxmox-backup-api.rs +++ b/src/bin/proxmox-backup-api.rs @@ -12,6 +12,7 @@ use proxmox_backup::config; use failure::*; use lazy_static::lazy_static; +use futures::*; use futures::future::Future; use hyper; @@ -66,7 +67,16 @@ fn run() -> Result<(), Error> { }, )?; - hyper::rt::run(server); + tokio::run(lazy(|| { + + if let Err(err) = server::server_state_init() { + eprintln!("unable to start daemon - {}", err); + } else { + tokio::spawn(server); + } + + Ok(()) + })); Ok(()) } diff --git a/src/bin/proxmox-backup-proxy.rs b/src/bin/proxmox-backup-proxy.rs index eded263f..556b2ac3 100644 --- a/src/bin/proxmox-backup-proxy.rs +++ b/src/bin/proxmox-backup-proxy.rs @@ -1,5 +1,6 @@ use proxmox_backup::configdir; use proxmox_backup::tools; +use proxmox_backup::server; use proxmox_backup::tools::daemon; use proxmox_backup::api_schema::router::*; use proxmox_backup::api_schema::config::*; @@ -9,8 +10,8 @@ use proxmox_backup::auth_helpers::*; use failure::*; use lazy_static::lazy_static; +use futures::*; use futures::stream::Stream; -use tokio::prelude::*; use hyper; @@ -96,6 +97,16 @@ fn run() -> Result<(), Error> { }, )?; - hyper::rt::run(server); + tokio::run(lazy(|| { + + if let Err(err) = server::server_state_init() { + eprintln!("unable to start daemon - {}", err); + } else { + tokio::spawn(server); + } + + Ok(()) + })); + Ok(()) } diff --git a/src/tools/daemon.rs b/src/tools/daemon.rs index eb1ead05..e3becbd5 100644 --- a/src/tools/daemon.rs +++ b/src/tools/daemon.rs @@ -6,12 +6,10 @@ use std::os::unix::ffi::OsStrExt; use std::panic::UnwindSafe; use failure::*; -use futures::future::poll_fn; -use futures::try_ready; use tokio::prelude::*; +use crate::server; use crate::tools::fd_change_cloexec; -use crate::tools::signalfd::{SigSet, SignalFd}; // Unfortunately FnBox is nightly-only and Box is unusable, so just use Box... pub type BoxedStoreFunc = Box Result + UnwindSafe + Send>; @@ -169,40 +167,21 @@ where let service = create_service(listener)?; - // Block SIGHUP for *all* threads and use it for a signalfd handler: - use nix::sys::signal; - let mut sigs = SigSet::empty(); - sigs.add(signal::Signal::SIGHUP); - signal::sigprocmask(signal::SigmaskHow::SIG_BLOCK, Some(&sigs), None)?; - - let mut sigfdstream = SignalFd::new(&sigs)? - .map_err(|e| log::error!("error in signal handler: {}", e)); - let mut reloader = Some(reloader); - // Use a Future instead of a Stream for ease-of-use: Poll until we receive a SIGHUP. - let signal_handler = poll_fn(move || { - match try_ready!(sigfdstream.poll()) { - Some(si) => { - log::info!("received signal {}", si.ssi_signo); - if si.ssi_signo == signal::Signal::SIGHUP as u32 { - if let Err(e) = reloader.take().unwrap().fork_restart() { - log::error!("error during reload: {}", e); - } - Ok(Async::Ready(())) - } else { - Ok(Async::NotReady) - } - } - // or the stream ended (which it can't, really) - None => Ok(Async::Ready(())) - } - }); - - Ok(service.select(signal_handler) - .map(|_| { - log::info!("daemon shutting down..."); - crate::tools::request_shutdown(); + let abort_future = server::shutdown_future().map_err(|_| {}); + Ok(service + .select(abort_future) + .map(move |_| { + crate::tools::request_shutdown(); // make sure we are in shutdown mode + if server::is_reload_request() { + log::info!("daemon reload..."); + if let Err(e) = reloader.take().unwrap().fork_restart() { + log::error!("error during reload: {}", e); + } + } else { + log::info!("daemon shutting down..."); + } }) .map_err(|_| ()) )