Skip to content

Commit

Permalink
graceful-shutdown-on-sigterm
Browse files Browse the repository at this point in the history
  • Loading branch information
kariy committed Sep 16, 2024
1 parent f4e15f1 commit cf0c10a
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 32 deletions.
4 changes: 2 additions & 2 deletions bin/katana/src/cli/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use tracing::{info, Subscriber};
use tracing_subscriber::{fmt, EnvFilter};
use url::Url;

use crate::utils::{parse_genesis, parse_seed, wait_signal};
use crate::utils::{parse_genesis, parse_seed};

#[derive(Parser, Debug)]
pub struct NodeArgs {
Expand Down Expand Up @@ -235,7 +235,7 @@ impl NodeArgs {

// Wait until an OS signal is received or TaskManager shutdown
tokio::select! {
_ = wait_signal() => {},
_ = dojo_utils::signal::wait_signals() => {},
_ = node.task_manager.wait_for_shutdown() => {}
}

Expand Down
23 changes: 0 additions & 23 deletions bin/katana/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,6 @@ pub fn parse_genesis(value: &str) -> Result<Genesis, anyhow::Error> {
Ok(genesis)
}

pub async fn wait_signal() {
use tokio::signal::ctrl_c;

#[cfg(unix)]
tokio::select! {
_ = ctrl_c() => {},
_ = sigterm() => {},
}

#[cfg(not(unix))]
tokio::select! {
_ = ctrl_c() => {},
}
}

/// Returns a future that can be awaited to wait for the SIGTERM signal.
#[cfg(unix)]
async fn sigterm() -> std::io::Result<()> {
use tokio::signal::unix::{signal, SignalKind};
signal(SignalKind::terminate())?.recv().await;
Ok(())
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
9 changes: 4 additions & 5 deletions bin/saya/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
use clap::Parser;
use console::Style;
use saya_core::{Saya, SayaConfig};
use tokio::signal::ctrl_c;

mod args;

Expand All @@ -20,11 +19,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
print_intro(&config);

let mut saya = Saya::new(config).await?;
saya.start().await?;

// Wait until Ctrl + C is pressed, then shutdown
ctrl_c().await?;
// handle.stop()?;
tokio::select! {
res = saya.start() => res?,
_ = dojo_utils::signal::wait_signals() => {}
}

Check warning on line 26 in bin/saya/src/main.rs

View check run for this annotation

Codecov / codecov/patch

bin/saya/src/main.rs#L23-L26

Added lines #L23 - L26 were not covered by tests

Ok(())
}
Expand Down
1 change: 1 addition & 0 deletions bin/torii/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ async fn main() -> anyhow::Result<()> {
_ = graphql_server => {},
_ = grpc_server => {},
_ = libp2p_relay_server.run() => {},
_ = dojo_utils::signal::wait_signals() => {},

Check warning on line 273 in bin/torii/src/main.rs

View check run for this annotation

Codecov / codecov/patch

bin/torii/src/main.rs#L273

Added line #L273 was not covered by tests
};

Ok(())
Expand Down
4 changes: 2 additions & 2 deletions crates/dojo-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ version.workspace = true
anyhow.workspace = true
futures.workspace = true
reqwest.workspace = true
rpassword.workspace = true
starknet.workspace = true
thiserror.workspace = true
rpassword.workspace = true
tokio = { version = "1", features = [ "time" ], default-features = false }
tokio = { version = "1", features = [ "signal", "time" ], default-features = false }

[dev-dependencies]
assert_matches.workspace = true
Expand Down
2 changes: 2 additions & 0 deletions crates/dojo-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ pub use tx::{TransactionExt, TxnAction, TxnConfig};

pub mod env;
pub mod keystore;

pub mod signal;
27 changes: 27 additions & 0 deletions crates/dojo-utils/src/signal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use std::io;

use tokio::signal::ctrl_c;

/// Returns a future for awaiting on OS signals to be received - SIGTERM, SIGINT.
///
/// Used for handling graceful shutdowns.
pub async fn wait_signals() {

Check warning on line 8 in crates/dojo-utils/src/signal.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-utils/src/signal.rs#L8

Added line #L8 was not covered by tests
#[cfg(unix)]
tokio::select! {
_ = ctrl_c() => {},
_ = sigterm() => {},
}

#[cfg(not(unix))]
tokio::select! {
_ = ctrl_c() => {},
}
}

Check warning on line 19 in crates/dojo-utils/src/signal.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-utils/src/signal.rs#L19

Added line #L19 was not covered by tests

/// Returns a future that can be awaited to wait for the SIGTERM signal.
#[cfg(unix)]
async fn sigterm() -> io::Result<()> {
use tokio::signal::unix::{signal, SignalKind};
signal(SignalKind::terminate())?.recv().await;
Ok(())
}

Check warning on line 27 in crates/dojo-utils/src/signal.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-utils/src/signal.rs#L23-L27

Added lines #L23 - L27 were not covered by tests

0 comments on commit cf0c10a

Please sign in to comment.