1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-06-26 06:57:43 +02:00

add support for unix signals

This commit is contained in:
Nikolay Kim
2017-12-28 11:36:20 -08:00
parent 02b37570f4
commit d80a0c9f94
5 changed files with 109 additions and 5 deletions

View File

@ -33,6 +33,9 @@ use openssl::pkcs12::ParsedPkcs12;
#[cfg(feature="alpn")]
use tokio_openssl::{SslStream, SslAcceptorExt};
#[cfg(feature="signal")]
use actix::actors::signal;
use helpers;
use channel::{HttpChannel, HttpHandler, IntoHttpHandler};
@ -108,7 +111,7 @@ pub struct HttpServer<T, A, H, U>
workers: Vec<SyncAddress<Worker<H>>>,
sockets: HashMap<net::SocketAddr, net::TcpListener>,
accept: Vec<(mio::SetReadiness, sync_mpsc::Sender<Command>)>,
spawned: bool,
exit: bool,
}
unsafe impl<T, A, H, U> Sync for HttpServer<T, A, H, U> where H: 'static {}
@ -152,7 +155,7 @@ impl<T, A, H, U, V> HttpServer<T, A, H, U>
workers: Vec::new(),
sockets: HashMap::new(),
accept: Vec::new(),
spawned: false,
exit: false,
}
}
@ -202,6 +205,16 @@ impl<T, A, H, U, V> HttpServer<T, A, H, U>
self
}
#[cfg(feature="signal")]
/// Send `SystemExit` message to actix system
///
/// `SystemExit` message stops currently running system arbiter and all
/// nested arbiters.
pub fn system_exit(mut self) -> Self {
self.exit = true;
self
}
/// Get addresses of bound sockets.
pub fn addrs(&self) -> Vec<net::SocketAddr> {
self.sockets.keys().cloned().collect()
@ -341,7 +354,7 @@ impl<H: HttpHandler, U, V> HttpServer<TcpStream, net::SocketAddr, H, U>
/// }
/// ```
pub fn spawn(mut self) -> SyncAddress<Self> {
self.spawned = true;
self.exit = true;
let (tx, rx) = sync_mpsc::channel();
thread::spawn(move || {
@ -475,6 +488,41 @@ impl<T, A, H, U, V> HttpServer<T, A, H, U>
}
}
#[cfg(feature="signal")]
/// Unix Signals support
/// Handle `SIGINT`, `SIGTERM`, `SIGQUIT` signals and send `SystemExit(0)`
/// message to `System` actor.
impl<T, A, H, U> Handler<signal::Signal> for HttpServer<T, A, H, U>
where T: AsyncRead + AsyncWrite + 'static,
H: HttpHandler + 'static,
U: 'static,
A: 'static,
{
fn handle(&mut self, msg: signal::Signal, ctx: &mut Context<Self>)
-> Response<Self, signal::Signal>
{
match msg.0 {
signal::SignalType::Int => {
info!("SIGINT received, exiting");
self.exit = true;
Handler::<StopServer>::handle(self, StopServer{graceful: false}, ctx);
}
signal::SignalType::Term => {
info!("SIGTERM received, stopping");
self.exit = true;
Handler::<StopServer>::handle(self, StopServer{graceful: true}, ctx);
}
signal::SignalType::Quit => {
info!("SIGQUIT received, exiting");
self.exit = true;
Handler::<StopServer>::handle(self, StopServer{graceful: false}, ctx);
}
_ => (),
};
Self::empty()
}
}
#[derive(Message)]
struct IoStream<T> {
io: T,
@ -522,7 +570,9 @@ pub struct ResumeServer;
///
/// If server starts with `spawn()` method, then spawned thread get terminated.
#[derive(Message)]
pub struct StopServer;
pub struct StopServer {
pub graceful: bool
}
impl<T, A, H, U> Handler<PauseServer> for HttpServer<T, A, H, U>
where T: AsyncRead + AsyncWrite + 'static,
@ -571,7 +621,7 @@ impl<T, A, H, U> Handler<StopServer> for HttpServer<T, A, H, U>
ctx.stop();
// we need to stop system if server was spawned
if self.spawned {
if self.exit {
Arbiter::system().send(msgs::SystemExit(0))
}
Self::empty()