Skip to content

Commit

Permalink
feat: also shutdown gracefully on sigterm on unix (#5023)
Browse files Browse the repository at this point in the history
* feat: support SIGTERM on unix

* chore: log

* fix: Result type
  • Loading branch information
discord9 authored Nov 19, 2024
1 parent 63bbfd0 commit 3633f25
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/cmd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,31 @@ lazy_static::lazy_static! {
prometheus::register_int_gauge_vec!("greptime_app_version", "app version", &["version", "short_version", "app"]).unwrap();
}

/// wait for the close signal, for unix platform it's SIGINT or SIGTERM
#[cfg(unix)]
async fn start_wait_for_close_signal() -> std::io::Result<()> {
use tokio::signal::unix::{signal, SignalKind};
let mut sigint = signal(SignalKind::interrupt())?;
let mut sigterm = signal(SignalKind::terminate())?;

tokio::select! {
_ = sigint.recv() => {
info!("Received SIGINT, shutting down");
}
_ = sigterm.recv() => {
info!("Received SIGTERM, shutting down");
}
}

Ok(())
}

/// wait for the close signal, for non-unix platform it's ctrl-c
#[cfg(not(unix))]
async fn start_wait_for_close_signal() -> std::io::Result<()> {
tokio::signal::ctrl_c().await
}

#[async_trait]
pub trait App: Send {
fn name(&self) -> &str;
Expand All @@ -69,9 +94,9 @@ pub trait App: Send {
self.start().await?;

if self.wait_signal() {
if let Err(e) = tokio::signal::ctrl_c().await {
error!(e; "Failed to listen for ctrl-c signal");
// It's unusual to fail to listen for ctrl-c signal, maybe there's something unexpected in
if let Err(e) = start_wait_for_close_signal().await {
error!(e; "Failed to listen for close signal");
// It's unusual to fail to listen for close signal, maybe there's something unexpected in
// the underlying system. So we stop the app instead of running nonetheless to let people
// investigate the issue.
}
Expand Down

0 comments on commit 3633f25

Please sign in to comment.