Skip to content

Commit

Permalink
Merge pull request #647 from nervosnetwork/graceful-shutdown-sigterm
Browse files Browse the repository at this point in the history
feat: graceful shutdown on SIGTERM too
  • Loading branch information
Yin Guanhao authored Apr 11, 2022
2 parents 439242b + d264fa9 commit 0b1756d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
11 changes: 0 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/block-producer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ serde = { version = "1.0", features = ["derive"] }
async-channel = "1.4.2"
async-jsonrpc-client = { version = "0.3.0", default-features = false, features = ["http-tokio"] }
clap = "2.33.3"
ctrlc = {version = "3.2.1", features = ["termination"]}
futures = "0.3.13"
log = "0.4.14"
serde_json = "1.0"
Expand Down
20 changes: 18 additions & 2 deletions crates/block-producer/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -954,15 +954,15 @@ pub async fn run(config: Config, skip_config_check: bool) -> Result<()> {
match rpc_ws_task {
Some(rpc_ws_task) => {
tokio::select! {
_ = tokio::signal::ctrl_c() => { },
_ = sigint_or_sigterm() => { },
_ = chain_task => {},
_ = rpc_ws_task => {},
_ = rpc_task => {},
}
}
None => {
tokio::select! {
_ = tokio::signal::ctrl_c() => { },
_ = sigint_or_sigterm() => { },
_ = chain_task => {},
_ = rpc_task => {},
};
Expand Down Expand Up @@ -1123,3 +1123,19 @@ fn is_l1_query_error(err: &anyhow::Error) -> bool {
err.downcast_ref::<RPCRequestError>().is_some()
|| err.downcast_ref::<QueryL1TxError>().is_some()
}

async fn sigint_or_sigterm() {
let int = tokio::signal::ctrl_c();
#[cfg(unix)]
let mut term = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
.expect("creating SIGTERM stream");
#[cfg(unix)]
tokio::select! {
_ = int => {}
_ = term.recv() => {}
}
#[cfg(not(unix))]
let _ = int.await;

log::info!("received sigint or sigterm, shutting down");
}

0 comments on commit 0b1756d

Please sign in to comment.