pub struct ShutdownCoordinator { /* private fields */ }Expand description
Coordinates graceful shutdown of agent workloads.
The shutdown coordinator manages a state machine with three states:
Running: Normal operation, new work can be registeredDraining: Shutdown initiated, no new work accepted, existing work completesTerminated: All work completed or timeout expired
§Example
let coordinator = Arc::new(ShutdownCoordinator::new(Duration::from_secs(30)));
let guard = coordinator.register_work();
// ... do work ...
drop(guard); // Work completes
coordinator.shutdown().await.unwrap();
assert_eq!(coordinator.state(), ShutdownState::Terminated);Implementations§
Source§impl ShutdownCoordinator
impl ShutdownCoordinator
Sourcepub fn new(drain_timeout: Duration) -> Self
pub fn new(drain_timeout: Duration) -> Self
Creates a new shutdown coordinator with the specified drain timeout.
§Arguments
drain_timeout- Maximum duration to wait for in-flight work to complete
Sourcepub fn register_work(self: &Arc<Self>) -> ShutdownResult<WorkGuard>
pub fn register_work(self: &Arc<Self>) -> ShutdownResult<WorkGuard>
Registers a unit of in-flight work.
Returns a WorkGuard that automatically decrements the in-flight counter
when dropped. If shutdown is already in progress, returns an error.
§Errors
Returns ShutdownError::AlreadyShuttingDown if shutdown has been initiated.
Sourcepub async fn shutdown(&self) -> ShutdownResult<()>
pub async fn shutdown(&self) -> ShutdownResult<()>
Initiates graceful shutdown.
Transitions the coordinator to Draining state and waits for in-flight work
to complete up to the configured drain timeout. If the timeout expires,
returns an error but still transitions to Terminated.
§Errors
Returns ShutdownError::DrainTimeout if in-flight work doesn’t complete
within the drain timeout.
Sourcepub fn state(&self) -> ShutdownState
pub fn state(&self) -> ShutdownState
Returns the current shutdown state.
Sourcepub fn subscribe(&self) -> Receiver<()>
pub fn subscribe(&self) -> Receiver<()>
Subscribes to shutdown signal.
Returns a receiver that will be notified when shutdown is initiated.
Sourcepub fn in_flight_count(&self) -> u32
pub fn in_flight_count(&self) -> u32
Returns the number of in-flight work items.