fix signal handling

This commit is contained in:
Rob Ede 2021-11-03 15:05:51 +00:00
parent c33fb4ef9a
commit 512de2163f
3 changed files with 25 additions and 12 deletions

View File

@ -8,7 +8,7 @@ use std::{
use actix_rt::{time::sleep, System};
use futures_core::future::BoxFuture;
use log::{error, info, trace};
use log::{error, info};
use tokio::sync::{
mpsc::{UnboundedReceiver, UnboundedSender},
oneshot,
@ -65,8 +65,6 @@ impl Server {
}
pub(crate) fn new(mut builder: ServerBuilder) -> Self {
trace!("start running server");
let sockets = mem::take(&mut builder.sockets)
.into_iter()
.map(|t| (t.0, t.2))
@ -93,12 +91,10 @@ impl Server {
);
}
trace!("run server");
match Accept::start(sockets, &builder) {
Ok((waker_queue, worker_handles)) => {
// construct OS signals listener future
let signals = (!builder.listen_os_signals).then(Signals::new);
let signals = (builder.listen_os_signals).then(Signals::new);
Self::Server(ServerInner {
cmd_tx: builder.cmd_tx.clone(),

View File

@ -1,6 +1,11 @@
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::{
fmt,
future::Future,
pin::Pin,
task::{Context, Poll},
};
use log::trace;
/// Types of process signals.
// #[allow(dead_code)]
@ -16,6 +21,16 @@ pub(crate) enum Signal {
Quit,
}
impl fmt::Display for Signal {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Signal::Int => "SIGINT",
Signal::Term => "SIGTERM",
Signal::Quit => "SIGQUIT",
})
}
}
/// Process signal listener.
pub(crate) struct Signals {
#[cfg(not(unix))]
@ -28,6 +43,8 @@ pub(crate) struct Signals {
impl Signals {
/// Constructs an OS signal listening future.
pub(crate) fn new() -> Self {
trace!("setting up OS signal listener");
#[cfg(not(unix))]
{
Signals {
@ -80,6 +97,7 @@ impl Future for Signals {
for (sig, fut) in self.signals.iter_mut() {
// TODO: match on if let Some ?
if Pin::new(fut).poll_recv(cx).is_ready() {
trace!("{} received", sig);
return Poll::Ready(*sig);
}
}

View File

@ -460,7 +460,7 @@ struct Shutdown {
/// Start time of shutdown.
start_from: Instant,
/// Notify of the shutdown outcome (force/grace) to stop caller.
/// Notify caller of the shutdown outcome (graceful/force).
tx: oneshot::Sender<bool>,
}
@ -472,8 +472,7 @@ impl Default for WorkerState {
impl Drop for ServerWorker {
fn drop(&mut self) {
trace!("dropping ServerWorker");
// Stop the Arbiter ServerWorker runs on on drop.
trace!("stopping ServerWorker Arbiter");
Arbiter::try_current().as_ref().map(ArbiterHandle::stop);
}
}