Skip to content

Commit

Permalink
feat: also gracefully shutdown on SIGTERM
Browse files Browse the repository at this point in the history
Signed-off-by: Bugen Zhao <[email protected]>
  • Loading branch information
BugenZhao committed Jul 25, 2024
1 parent 8c20bad commit 5a55937
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/utils/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ mod panic_hook;
pub use panic_hook::*;
mod prof;
use prof::*;
use tokio::signal::unix::SignalKind;

/// Start RisingWave components with configs from environment variable.
///
Expand Down Expand Up @@ -96,24 +97,26 @@ where
spawn_prof_thread(profile_path);
}

let mut sigint = tokio::signal::unix::signal(SignalKind::interrupt()).unwrap();
let mut sigterm = tokio::signal::unix::signal(SignalKind::terminate()).unwrap();

let future_with_shutdown = async move {
let shutdown = CancellationToken::new();
let mut fut = pin!(f(shutdown.clone()));

tokio::select! {
biased;
result = tokio::signal::ctrl_c() => {
result.expect("failed to receive ctrl-c signal");
tracing::info!("received ctrl-c, shutting down... (press ctrl-c again to force shutdown)");

// Send shutdown signal.
// Watch SIGINT, typically originating from user pressing ctrl-c.
// Attempt to shutdown gracefully and force shutdown on the next signal.
_ = sigint.recv() => {
tracing::info!("received ctrl-c, shutting down... (press ctrl-c again to force shutdown)");
shutdown.cancel();

// While waiting for the future to finish, listen for the second ctrl-c signal.
tokio::select! {
biased;
result = tokio::signal::ctrl_c() => {
result.expect("failed to receive ctrl-c signal");
_ = sigint.recv() => {
tracing::warn!("forced shutdown");

// Directly exit the process **here** instead of returning from the future, since
Expand All @@ -123,6 +126,16 @@ where
_ = &mut fut => {},
}
}

// Watch SIGTERM, typically originating from Kubernetes.
// Attempt to shutdown gracefully. No need to force shutdown since it will send SIGKILL after a timeout.
_ = sigterm.recv() => {
tracing::info!("received SIGTERM, shutting down...");
shutdown.cancel();
fut.await;
}

// Proceed with the future.
_ = &mut fut => {},
}
};
Expand Down

0 comments on commit 5a55937

Please sign in to comment.