2020-04-17 12:11:25 +00:00
|
|
|
use anyhow::{Error};
|
2019-04-08 10:21:29 +00:00
|
|
|
use lazy_static::lazy_static;
|
|
|
|
use std::sync::Mutex;
|
|
|
|
|
|
|
|
use futures::*;
|
|
|
|
|
2019-12-12 14:27:07 +00:00
|
|
|
use tokio::signal::unix::{signal, SignalKind};
|
2019-04-08 10:21:29 +00:00
|
|
|
|
2021-09-14 06:35:43 +00:00
|
|
|
use pbs_tools::broadcast_future::BroadcastData;
|
2019-04-08 10:21:29 +00:00
|
|
|
|
|
|
|
#[derive(PartialEq, Copy, Clone, Debug)]
|
|
|
|
pub enum ServerMode {
|
|
|
|
Normal,
|
|
|
|
Shutdown,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ServerState {
|
|
|
|
pub mode: ServerMode,
|
2019-04-30 08:21:48 +00:00
|
|
|
pub shutdown_listeners: BroadcastData<()>,
|
|
|
|
pub last_worker_listeners: BroadcastData<()>,
|
2019-04-08 10:21:29 +00:00
|
|
|
pub worker_count: usize,
|
2020-07-24 10:11:33 +00:00
|
|
|
pub internal_task_count: usize,
|
2019-04-08 10:21:29 +00:00
|
|
|
pub reload_request: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
static ref SERVER_STATE: Mutex<ServerState> = Mutex::new(ServerState {
|
|
|
|
mode: ServerMode::Normal,
|
2019-04-30 08:21:48 +00:00
|
|
|
shutdown_listeners: BroadcastData::new(),
|
|
|
|
last_worker_listeners: BroadcastData::new(),
|
2019-04-08 10:21:29 +00:00
|
|
|
worker_count: 0,
|
2020-07-24 10:11:33 +00:00
|
|
|
internal_task_count: 0,
|
2019-04-08 10:21:29 +00:00
|
|
|
reload_request: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-29 12:34:25 +00:00
|
|
|
/// Listen to ``SIGINT`` and ``SIGHUP`` signals
|
|
|
|
///
|
|
|
|
/// * ``SIGINT``: start server shutdown
|
|
|
|
///
|
|
|
|
/// * ``SIGHUP``: start server reload
|
|
|
|
pub fn catch_shutdown_and_reload_signals() -> Result<(), Error> {
|
2019-04-08 10:21:29 +00:00
|
|
|
|
2019-12-12 14:27:07 +00:00
|
|
|
let mut stream = signal(SignalKind::interrupt())?;
|
2019-04-08 10:21:29 +00:00
|
|
|
|
2019-12-12 14:27:07 +00:00
|
|
|
let future = async move {
|
|
|
|
while stream.recv().await.is_some() {
|
2021-09-29 12:34:25 +00:00
|
|
|
log::info!("got shutdown request (SIGINT)");
|
2019-12-12 14:27:07 +00:00
|
|
|
SERVER_STATE.lock().unwrap().reload_request = false;
|
2021-09-21 05:58:40 +00:00
|
|
|
crate::request_shutdown();
|
2019-12-12 14:27:07 +00:00
|
|
|
}
|
|
|
|
}.boxed();
|
2019-04-08 10:21:29 +00:00
|
|
|
|
|
|
|
let abort_future = last_worker_future().map_err(|_| {});
|
2019-08-23 11:41:38 +00:00
|
|
|
let task = futures::future::select(future, abort_future);
|
2019-04-08 10:21:29 +00:00
|
|
|
|
2019-08-23 11:41:38 +00:00
|
|
|
tokio::spawn(task.map(|_| ()));
|
2019-04-08 10:21:29 +00:00
|
|
|
|
2019-12-12 14:27:07 +00:00
|
|
|
let mut stream = signal(SignalKind::hangup())?;
|
2019-04-08 10:21:29 +00:00
|
|
|
|
2019-12-12 14:27:07 +00:00
|
|
|
let future = async move {
|
|
|
|
while stream.recv().await.is_some() {
|
2021-09-29 12:34:25 +00:00
|
|
|
log::info!("got reload request (SIGHUP)");
|
2019-12-12 14:27:07 +00:00
|
|
|
SERVER_STATE.lock().unwrap().reload_request = true;
|
2021-09-21 05:58:40 +00:00
|
|
|
crate::request_shutdown();
|
2019-12-12 14:27:07 +00:00
|
|
|
}
|
|
|
|
}.boxed();
|
2019-04-08 10:21:29 +00:00
|
|
|
|
|
|
|
let abort_future = last_worker_future().map_err(|_| {});
|
2019-08-23 11:41:38 +00:00
|
|
|
let task = futures::future::select(future, abort_future);
|
2019-04-08 10:21:29 +00:00
|
|
|
|
2019-08-23 11:41:38 +00:00
|
|
|
tokio::spawn(task.map(|_| ()));
|
2019-04-08 10:21:29 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_reload_request() -> bool {
|
|
|
|
let data = SERVER_STATE.lock().unwrap();
|
|
|
|
|
2019-10-26 09:36:01 +00:00
|
|
|
data.mode == ServerMode::Shutdown && data.reload_request
|
2019-04-08 10:21:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn server_shutdown() {
|
|
|
|
let mut data = SERVER_STATE.lock().unwrap();
|
|
|
|
|
2021-09-29 12:34:25 +00:00
|
|
|
log::info!("request_shutdown");
|
2019-04-08 10:21:29 +00:00
|
|
|
|
|
|
|
data.mode = ServerMode::Shutdown;
|
|
|
|
|
2019-04-30 08:21:48 +00:00
|
|
|
data.shutdown_listeners.notify_listeners(Ok(()));
|
2019-04-08 10:21:29 +00:00
|
|
|
|
|
|
|
drop(data); // unlock
|
|
|
|
|
|
|
|
check_last_worker();
|
|
|
|
}
|
|
|
|
|
2019-08-23 11:41:38 +00:00
|
|
|
pub fn shutdown_future() -> impl Future<Output = ()> {
|
2019-04-08 10:21:29 +00:00
|
|
|
let mut data = SERVER_STATE.lock().unwrap();
|
2019-08-23 11:41:38 +00:00
|
|
|
data
|
|
|
|
.shutdown_listeners
|
|
|
|
.listen()
|
|
|
|
.map(|_| ())
|
2019-04-08 10:21:29 +00:00
|
|
|
}
|
|
|
|
|
2019-08-23 11:41:38 +00:00
|
|
|
pub fn last_worker_future() -> impl Future<Output = Result<(), Error>> {
|
2019-04-08 10:21:29 +00:00
|
|
|
let mut data = SERVER_STATE.lock().unwrap();
|
2019-04-30 08:21:48 +00:00
|
|
|
data.last_worker_listeners.listen()
|
2019-04-08 10:21:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_worker_count(count: usize) {
|
2020-07-23 13:20:13 +00:00
|
|
|
SERVER_STATE.lock().unwrap().worker_count = count;
|
|
|
|
|
|
|
|
check_last_worker();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn check_last_worker() {
|
2019-04-08 10:21:29 +00:00
|
|
|
let mut data = SERVER_STATE.lock().unwrap();
|
|
|
|
|
2020-07-24 10:11:33 +00:00
|
|
|
if !(data.mode == ServerMode::Shutdown && data.worker_count == 0 && data.internal_task_count == 0) { return; }
|
2019-04-08 10:21:29 +00:00
|
|
|
|
2019-04-30 08:21:48 +00:00
|
|
|
data.last_worker_listeners.notify_listeners(Ok(()));
|
2019-04-08 10:21:29 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 13:20:13 +00:00
|
|
|
/// Spawns a tokio task that will be tracked for reload
|
|
|
|
/// and if it is finished, notify the last_worker_listener if we
|
|
|
|
/// are in shutdown mode
|
|
|
|
pub fn spawn_internal_task<T>(task: T)
|
|
|
|
where
|
|
|
|
T: Future + Send + 'static,
|
|
|
|
T::Output: Send + 'static,
|
|
|
|
{
|
2019-04-08 10:21:29 +00:00
|
|
|
let mut data = SERVER_STATE.lock().unwrap();
|
2020-07-24 10:11:33 +00:00
|
|
|
data.internal_task_count += 1;
|
2019-04-08 10:21:29 +00:00
|
|
|
|
2020-07-23 13:20:13 +00:00
|
|
|
tokio::spawn(async move {
|
|
|
|
let _ = tokio::spawn(task).await; // ignore errors
|
2019-04-08 10:21:29 +00:00
|
|
|
|
2020-07-23 13:20:13 +00:00
|
|
|
{ // drop mutex
|
|
|
|
let mut data = SERVER_STATE.lock().unwrap();
|
2020-07-24 10:11:33 +00:00
|
|
|
if data.internal_task_count > 0 {
|
|
|
|
data.internal_task_count -= 1;
|
2020-07-23 13:20:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
check_last_worker();
|
|
|
|
});
|
2019-04-08 10:21:29 +00:00
|
|
|
}
|