From 09a64c51e0e51148e198f4a8c1a7a952a7c00600 Mon Sep 17 00:00:00 2001 From: discord9 Date: Tue, 19 Nov 2024 20:20:22 +0800 Subject: [PATCH 1/3] feat: support SIGTERM on unix --- src/cmd/src/lib.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/cmd/src/lib.rs b/src/cmd/src/lib.rs index 731f527daf7c..154a51476280 100644 --- a/src/cmd/src/lib.rs +++ b/src/cmd/src/lib.rs @@ -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::result::Result<(), Box> { + 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::result::Result<(), Box> { + tokio::signal::ctrl_c().await +} + #[async_trait] pub trait App: Send { fn name(&self) -> &str; @@ -69,7 +94,7 @@ pub trait App: Send { self.start().await?; if self.wait_signal() { - if let Err(e) = tokio::signal::ctrl_c().await { + if let Err(e) = start_wait_for_close_signal().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 // the underlying system. So we stop the app instead of running nonetheless to let people From f98936f28e1fcb5c8f7c02ebbfdecf3b2fa22453 Mon Sep 17 00:00:00 2001 From: discord9 Date: Tue, 19 Nov 2024 20:27:16 +0800 Subject: [PATCH 2/3] chore: log --- src/cmd/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cmd/src/lib.rs b/src/cmd/src/lib.rs index 154a51476280..ff853f92c4d6 100644 --- a/src/cmd/src/lib.rs +++ b/src/cmd/src/lib.rs @@ -95,8 +95,8 @@ pub trait App: Send { if self.wait_signal() { if let Err(e) = start_wait_for_close_signal().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 + 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. } From 6de01f81c5eeaef29e8fe5a99f33518a49e2479a Mon Sep 17 00:00:00 2001 From: discord9 Date: Tue, 19 Nov 2024 20:42:45 +0800 Subject: [PATCH 3/3] fix: Result type --- src/cmd/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cmd/src/lib.rs b/src/cmd/src/lib.rs index ff853f92c4d6..3a719b9589a7 100644 --- a/src/cmd/src/lib.rs +++ b/src/cmd/src/lib.rs @@ -45,7 +45,7 @@ lazy_static::lazy_static! { /// wait for the close signal, for unix platform it's SIGINT or SIGTERM #[cfg(unix)] -async fn start_wait_for_close_signal() -> std::result::Result<(), Box> { +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())?; @@ -64,7 +64,7 @@ async fn start_wait_for_close_signal() -> std::result::Result<(), Box std::result::Result<(), Box> { +async fn start_wait_for_close_signal() -> std::io::Result<()> { tokio::signal::ctrl_c().await }