src/tools/daemon.rs: use new ServerState handler

This commit is contained in:
Dietmar Maurer 2019-04-08 14:00:23 +02:00
parent 7a630df709
commit e3f41f2165
3 changed files with 38 additions and 38 deletions

View File

@ -12,6 +12,7 @@ use proxmox_backup::config;
use failure::*; use failure::*;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use futures::*;
use futures::future::Future; use futures::future::Future;
use hyper; 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(()) Ok(())
} }

View File

@ -1,5 +1,6 @@
use proxmox_backup::configdir; use proxmox_backup::configdir;
use proxmox_backup::tools; use proxmox_backup::tools;
use proxmox_backup::server;
use proxmox_backup::tools::daemon; use proxmox_backup::tools::daemon;
use proxmox_backup::api_schema::router::*; use proxmox_backup::api_schema::router::*;
use proxmox_backup::api_schema::config::*; use proxmox_backup::api_schema::config::*;
@ -9,8 +10,8 @@ use proxmox_backup::auth_helpers::*;
use failure::*; use failure::*;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use futures::*;
use futures::stream::Stream; use futures::stream::Stream;
use tokio::prelude::*;
use hyper; 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(()) Ok(())
} }

View File

@ -6,12 +6,10 @@ use std::os::unix::ffi::OsStrExt;
use std::panic::UnwindSafe; use std::panic::UnwindSafe;
use failure::*; use failure::*;
use futures::future::poll_fn;
use futures::try_ready;
use tokio::prelude::*; use tokio::prelude::*;
use crate::server;
use crate::tools::fd_change_cloexec; use crate::tools::fd_change_cloexec;
use crate::tools::signalfd::{SigSet, SignalFd};
// Unfortunately FnBox is nightly-only and Box<FnOnce> is unusable, so just use Box<Fn>... // Unfortunately FnBox is nightly-only and Box<FnOnce> is unusable, so just use Box<Fn>...
pub type BoxedStoreFunc = Box<dyn Fn() -> Result<String, Error> + UnwindSafe + Send>; pub type BoxedStoreFunc = Box<dyn Fn() -> Result<String, Error> + UnwindSafe + Send>;
@ -169,40 +167,21 @@ where
let service = create_service(listener)?; 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); let mut reloader = Some(reloader);
// Use a Future instead of a Stream for ease-of-use: Poll until we receive a SIGHUP. let abort_future = server::shutdown_future().map_err(|_| {});
let signal_handler = poll_fn(move || { Ok(service
match try_ready!(sigfdstream.poll()) { .select(abort_future)
Some(si) => { .map(move |_| {
log::info!("received signal {}", si.ssi_signo); crate::tools::request_shutdown(); // make sure we are in shutdown mode
if si.ssi_signo == signal::Signal::SIGHUP as u32 { if server::is_reload_request() {
log::info!("daemon reload...");
if let Err(e) = reloader.take().unwrap().fork_restart() { if let Err(e) = reloader.take().unwrap().fork_restart() {
log::error!("error during reload: {}", e); log::error!("error during reload: {}", e);
} }
Ok(Async::Ready(()))
} else { } 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..."); log::info!("daemon shutting down...");
crate::tools::request_shutdown(); }
}) })
.map_err(|_| ()) .map_err(|_| ())
) )