turbomcp_server/server/shutdown.rs
1//! Graceful server shutdown coordination
2//!
3//! Provides external control over server shutdown with support for signal handling,
4//! container orchestration, health checks, and multi-service coordination.
5
6use std::sync::Arc;
7
8use crate::lifecycle::ServerLifecycle;
9
10/// Handle for triggering graceful server shutdown
11///
12/// Provides external control over server shutdown with support for:
13/// - **Signal handling**: SIGTERM, SIGINT, custom signals
14/// - **Container orchestration**: Kubernetes graceful termination
15/// - **Health checks**: Coordinated shutdown with load balancers
16/// - **Multi-service coordination**: Synchronized shutdown sequences
17/// - **Testing**: Controlled server lifecycle in tests
18///
19/// The handle is cloneable and thread-safe, allowing multiple components
20/// to coordinate shutdown or check shutdown status.
21#[derive(Debug, Clone)]
22pub struct ShutdownHandle {
23 lifecycle: Arc<ServerLifecycle>,
24}
25
26impl ShutdownHandle {
27 /// Create a new shutdown handle
28 pub(crate) fn new(lifecycle: Arc<ServerLifecycle>) -> Self {
29 Self { lifecycle }
30 }
31
32 /// Trigger graceful server shutdown
33 pub async fn shutdown(&self) {
34 self.lifecycle.shutdown().await;
35 }
36
37 /// Check if shutdown has been initiated
38 pub async fn is_shutting_down(&self) -> bool {
39 use crate::lifecycle::ServerState;
40 matches!(
41 self.lifecycle.state().await,
42 ServerState::ShuttingDown | ServerState::Stopped
43 )
44 }
45}