From be1676252c41f8188465741edcbcb381d2c12080 Mon Sep 17 00:00:00 2001 From: Tarrence van As Date: Fri, 10 Nov 2023 10:16:26 -0500 Subject: [PATCH] Use cross platform interrupt handler (#1178) --- Cargo.lock | 23 +++++++++++++++++++++++ crates/torii/server/Cargo.toml | 2 ++ crates/torii/server/src/cli.rs | 21 +++++---------------- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8de278bb93..c7d80ecd4a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1934,6 +1934,16 @@ dependencies = [ "cipher", ] +[[package]] +name = "ctrlc" +version = "3.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82e95fbd621905b854affdc67943b043a0fbb6ed7385fd5a25650d19a8a6cfdf" +dependencies = [ + "nix", + "windows-sys 0.48.0", +] + [[package]] name = "darling" version = "0.14.4" @@ -5253,6 +5263,17 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e4a24736216ec316047a1fc4252e27dabb04218aa4a3f37c6e7ddbf1f9782b54" +[[package]] +name = "nix" +version = "0.27.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2eb04e9c688eff1c89d72b407f168cf79bb9e867a9d3323ed6c01519eb9cc053" +dependencies = [ + "bitflags 2.4.1", + "cfg-if", + "libc", +] + [[package]] name = "nom" version = "7.1.3" @@ -8390,9 +8411,11 @@ dependencies = [ "camino", "chrono", "clap", + "ctrlc", "dojo-types", "dojo-world", "either", + "futures", "http", "http-body", "hyper", diff --git a/crates/torii/server/Cargo.toml b/crates/torii/server/Cargo.toml index 0b6acdfc14..147d3d330f 100644 --- a/crates/torii/server/Cargo.toml +++ b/crates/torii/server/Cargo.toml @@ -12,9 +12,11 @@ base64 = "0.21.2" camino.workspace = true chrono.workspace = true clap.workspace = true +ctrlc = { version = "3.4", features = [ "termination" ] } dojo-types = { path = "../../dojo-types" } dojo-world = { path = "../../dojo-world" } either = "1.9.0" +futures.workspace = true http = "0.2.9" http-body = "0.4.5" hyper-reverse-proxy = { git = "https://github.com/tarrencev/hyper-reverse-proxy" } diff --git a/crates/torii/server/src/cli.rs b/crates/torii/server/src/cli.rs index 23078cc438..a86e95fda9 100644 --- a/crates/torii/server/src/cli.rs +++ b/crates/torii/server/src/cli.rs @@ -11,9 +11,6 @@ use sqlx::SqlitePool; use starknet::core::types::FieldElement; use starknet::providers::jsonrpc::HttpTransport; use starknet::providers::JsonRpcClient; -use tokio::signal::ctrl_c; -#[cfg(target_family = "unix")] -use tokio::signal::unix::{signal, SignalKind}; use tokio::sync::broadcast; use tokio::sync::broadcast::Sender; use tokio_stream::StreamExt; @@ -80,12 +77,11 @@ async fn main() -> anyhow::Result<()> { // Setup cancellation for graceful shutdown let (shutdown_tx, _) = broadcast::channel(1); - let sigint = ctrl_c(); - - #[cfg(target_family = "unix")] - let mut sigterm = signal(SignalKind::terminate())?; - #[cfg(target_family = "windows")] - let (_, sigterm) = futures::channel::mpsc::unbounded::<()>(); + let shutdown_tx_clone = shutdown_tx.clone(); + ctrlc::set_handler(move || { + let _ = shutdown_tx_clone.send(()); + }) + .expect("Error setting Ctrl-C handler"); let database_url = format!("sqlite:{}", &args.database); let options = SqliteConnectOptions::from_str(&database_url)?.create_if_missing(true); @@ -150,13 +146,6 @@ async fn main() -> anyhow::Result<()> { info!("Graphql playground: {}\n", format!("http://{}/graphql", addr)); tokio::select! { - _ = sigterm.recv() => { - let _ = shutdown_tx.send(()); - } - _ = sigint => { - let _ = shutdown_tx.send(()); - } - _ = engine.start() => {}, _ = proxy_server.start(shutdown_tx.subscribe()) => {}, _ = graphql_server => {},