From 60324f88f2d46bfe729f616f2ecec7fa8dc13a11 Mon Sep 17 00:00:00 2001 From: Sitao Wang Date: Mon, 27 Jul 2026 10:53:59 -0400 Subject: [PATCH] Add sigterm handler in servers --- .../src/bin/execution_manager.rs | 10 ++++++++++ components/spider-scheduler/src/bin/grpc_server.rs | 9 +++++++++ components/spider-storage/src/bin/grpc_server.rs | 9 +++++++++ 3 files changed, 28 insertions(+) diff --git a/components/spider-execution-manager/src/bin/execution_manager.rs b/components/spider-execution-manager/src/bin/execution_manager.rs index 6d5a98251..409132dba 100644 --- a/components/spider-execution-manager/src/bin/execution_manager.rs +++ b/components/spider-execution-manager/src/bin/execution_manager.rs @@ -11,6 +11,8 @@ use spider_execution_manager::client::grpc::GrpcStorageClient; use spider_execution_manager::runtime::Runtime; use spider_utils::config::YamlConfig; use spider_utils::logging::set_up_logging; +use tokio::signal::unix::SignalKind; +use tokio::signal::unix::signal; /// Command-line arguments for the execution manager. #[derive(Debug, Parser)] @@ -63,6 +65,9 @@ async fn main() -> Result<(), Box> { tracing::info!(em_id, "Execution manager started."); let mut run_handle = tokio::spawn(runtime.run()); + let mut sigterm = signal(SignalKind::terminate()).inspect_err( + |error| tracing::error!(em_id, error = % error, "Failed to register SIGTERM handler."), + )?; let () = tokio::select! { result = tokio::signal::ctrl_c() => { @@ -73,6 +78,11 @@ async fn main() -> Result<(), Box> { cancellation_token.cancel(); run_handle.await } + _ = sigterm.recv() => { + tracing::info!(em_id, "Received SIGTERM. Shutting down execution manager."); + cancellation_token.cancel(); + run_handle.await + } result = &mut run_handle => { tracing::info!("Execution manager runtime exited."); result diff --git a/components/spider-scheduler/src/bin/grpc_server.rs b/components/spider-scheduler/src/bin/grpc_server.rs index 30ba4e87a..f8d88f186 100644 --- a/components/spider-scheduler/src/bin/grpc_server.rs +++ b/components/spider-scheduler/src/bin/grpc_server.rs @@ -14,6 +14,8 @@ use spider_utils::config::YamlConfig; use spider_utils::logging::set_up_logging; use tokio::net::TcpListener; use tokio::select; +use tokio::signal::unix::SignalKind; +use tokio::signal::unix::signal; use tonic::transport::Server; use tonic::transport::server::TcpIncoming; @@ -58,6 +60,9 @@ async fn main() -> Result<(), Box> { |error| tracing::error!(error = % error, "Failed to bind scheduler listen address."), )?; let incoming = TcpIncoming::from(listener); + let mut sigterm = signal(SignalKind::terminate()).inspect_err( + |error| tracing::error!(error = % error, "Failed to register SIGTERM handler."), + )?; Server::builder() .add_service(SchedulerServiceServer::new(grpc_service)) @@ -73,6 +78,10 @@ async fn main() -> Result<(), Box> { tracing::info!("Received Ctrl-C. Shutting down scheduler gRPC server."); cancellation_token.cancel(); } + _ = sigterm.recv() => { + tracing::info!("Received SIGTERM. Shutting down scheduler gRPC server."); + cancellation_token.cancel(); + } } }) .await diff --git a/components/spider-storage/src/bin/grpc_server.rs b/components/spider-storage/src/bin/grpc_server.rs index 4e41bac91..b8f0115c2 100644 --- a/components/spider-storage/src/bin/grpc_server.rs +++ b/components/spider-storage/src/bin/grpc_server.rs @@ -19,6 +19,8 @@ use spider_utils::config::YamlConfig; use spider_utils::logging::set_up_logging; use tokio::net::TcpListener; use tokio::select; +use tokio::signal::unix::SignalKind; +use tokio::signal::unix::signal; use tonic::transport::Server; use tonic::transport::server::TcpIncoming; @@ -53,6 +55,9 @@ async fn main() -> Result<(), Box> { |error| tracing::error!(error = % error, "Failed to bind storage listen address."), )?; let incoming = TcpIncoming::from(listener); + let mut sigterm = signal(SignalKind::terminate()).inspect_err( + |error| tracing::error!(error = % error, "Failed to register SIGTERM handler."), + )?; let server_cancellation_token = cancellation_token.clone(); let server = tokio::spawn( Server::builder() @@ -82,6 +87,10 @@ async fn main() -> Result<(), Box> { } server_cancellation_token.cancel(); } + _ = sigterm.recv() => { + tracing::info!("Received SIGTERM. Shutting down storage gRPC server."); + server_cancellation_token.cancel(); + } } }), );