diff options
| author | Fuwn <[email protected]> | 2021-07-31 16:15:30 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2021-07-31 16:15:30 +0000 |
| commit | 7c1575e2d5255824b59ab604c1e8ab6537e37cff (patch) | |
| tree | b5f55f22cba4d89e0aeb667ea9b570b12571d528 | |
| parent | Merge pull request #65 from Whirlsplash/renovate/serde_derive-1.x (diff) | |
| download | whirl-7c1575e2d5255824b59ab604c1e8ab6537e37cff.tar.xz whirl-7c1575e2d5255824b59ab604c1e8ab6537e37cff.zip | |
feat(whirl): platform specific signal handling
I hadn't realised until now; the `signal-hook` crate doesn't
function properly on Windows, for the time being; the functionality of
this crate has been disabled on Windows.
This change also adapts the signal handler to use the Tokio-specific
`signal-hook` crate.
| -rw-r--r-- | crates/whirl/Cargo.toml | 5 | ||||
| -rw-r--r-- | crates/whirl/src/lib.rs | 21 |
2 files changed, 17 insertions, 9 deletions
diff --git a/crates/whirl/Cargo.toml b/crates/whirl/Cargo.toml index bcf6855..ac24853 100644 --- a/crates/whirl/Cargo.toml +++ b/crates/whirl/Cargo.toml @@ -37,7 +37,6 @@ serde_derive = "1.0.127" # CLI structopt = "0.3.22" -signal-hook = "0.3.9" # Config whirl_config = { path = "../whirl_config" } @@ -64,3 +63,7 @@ mimalloc = { version = "0.1.26", default-features = false } [target.'cfg(unix)'.dependencies] jemallocator = "0.3.2" + +# Signal +signal-hook = "0.3.9" +signal-hook-tokio = "0.3.0" diff --git a/crates/whirl/src/lib.rs b/crates/whirl/src/lib.rs index 1e3931c..6a0774b 100644 --- a/crates/whirl/src/lib.rs +++ b/crates/whirl/src/lib.rs @@ -38,7 +38,10 @@ static GLOBAL: jemallocator::Jemalloc = jemallocator::Jemalloc; pub mod cli; -use signal_hook::consts::{SIGINT, SIGTERM}; +#[cfg(unix)] +use signal_hook::consts::signal::{SIGINT, SIGTERM}; +#[cfg(unix)] +use tokio_stream::StreamExt; use whirl_config::Config; pub struct Whirl; @@ -80,13 +83,15 @@ impl Whirl { } // Ctrl+C handling - tokio::spawn(async move { - for signal in signal_hook::iterator::Signals::new(&[SIGTERM, SIGINT]) - .unwrap() - .forever() - { - info!("signal received: {:?}, killing whirl", signal); - std::process::exit(0); + #[cfg(unix)] + tokio::spawn({ + while let Some(signal) = signal_hook_tokio::Signals::new(&[SIGTERM, SIGINT]).fuse() { + match signal { + _ => { + info!("signal received: {:?}, killing whirl", signal); + std::process::exit(0); + } + } } }); |