-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
pub(crate) type Signal = &'static str; | ||
|
||
use tokio::task::JoinHandle; | ||
use tokio::{ | ||
select, | ||
sync::{ | ||
oneshot, | ||
oneshot::{Receiver, Sender}, | ||
}, | ||
}; | ||
|
||
#[cfg(target_family = "unix")] | ||
use tokio::signal::unix; | ||
#[cfg(target_family = "windows")] | ||
use tokio::signal::windows; | ||
|
||
pub struct SignalMonitor { | ||
stop_tx: Sender<Signal>, | ||
stop_rx: Receiver<Signal>, | ||
} | ||
|
||
impl Default for SignalMonitor { | ||
fn default() -> Self { | ||
let (stop_tx, stop_rx) = oneshot::channel(); | ||
Self { stop_tx, stop_rx } | ||
} | ||
} | ||
|
||
impl SignalMonitor { | ||
pub async fn recv(self) -> anyhow::Result<Signal> { | ||
Self::start(self.stop_tx)?; | ||
Ok(self.stop_rx.await?) | ||
} | ||
|
||
#[cfg(target_family = "unix")] | ||
fn start(stop_tx: Sender<Signal>) -> anyhow::Result<JoinHandle<()>> { | ||
let mut sigterm = unix::signal(unix::SignalKind::terminate())?; | ||
let mut sigint = unix::signal(unix::SignalKind::interrupt())?; | ||
let mut sigquit = unix::signal(unix::SignalKind::quit())?; | ||
Ok(tokio::spawn(async move { | ||
select! { | ||
_ = sigterm.recv() => stop_tx.send("SIGTERM").expect("Failed to handle SIGTERM event"), | ||
_ = sigint.recv() => stop_tx.send("SIGINT").expect("Failed to handle SIGINT event"), | ||
_ = sigquit.recv() => stop_tx.send("SIGQUIT").expect("Failed to handle SIGQUIT event"), | ||
}; | ||
})) | ||
} | ||
|
||
#[cfg(target_family = "windows")] | ||
fn start(stop_tx: Sender<Signal>) -> anyhow::Result<JoinHandle<()>> { | ||
let mut ctrl_c = windows::ctrl_c()?; | ||
let mut ctrl_close = windows::ctrl_close()?; | ||
let mut ctrl_logoff = windows::ctrl_logoff()?; | ||
let mut ctrl_shutdown = windows::ctrl_shutdown()?; | ||
let mut ctrl_break = windows::ctrl_break()?; | ||
Ok(tokio::spawn(async move { | ||
select! { | ||
_ = ctrl_c.recv() => stop_tx.send("CTRL-C").expect("Failed to handle CTRL-C event"), | ||
_ = ctrl_close.recv() => stop_tx.send("CTRL-CLOSE").expect("Failed to handle CTRL-CLOSE event"), | ||
_ = ctrl_logoff.recv() => stop_tx.send("CTRL-LOGOFF").expect("Failed to handle CTRL-LOGOFF event"), | ||
_ = ctrl_shutdown.recv() => stop_tx.send("CTRL-SHUTDOWN").expect("Failed to handle CTRL-HUTDOWN event"), | ||
_ = ctrl_break.recv() => stop_tx.send("CTRL-BREAK").expect("Failed to handle CTRL-BREAK event") | ||
}; | ||
})) | ||
} | ||
} |