pub struct ShutdownManager { /* private fields */ }Expand description
A manager for coordinating the graceful shutdown of background tasks.
Implementations§
Source§impl ShutdownManager
impl ShutdownManager
Sourcepub fn spawn_task<F>(&mut self, task: F)
pub fn spawn_task<F>(&mut self, task: F)
Spawns a new task that will be managed by the shutdown coordinator.
The provided future will be spawned onto the Tokio runtime. The task must
be Send and 'static because it may outlive the current scope.
If the ShutdownManager is dropped, all tasks spawned by it are
immediately aborted.
Sourcepub fn subscribe(&self) -> Receiver<()>
pub fn subscribe(&self) -> Receiver<()>
Returns a new receiver for the shutdown signal.
Each background task should subscribe to this signal to know when to begin its graceful termination.
Sourcepub fn abort_all(&mut self)
pub fn abort_all(&mut self)
Immediately aborts all tasks managed by the ShutdownManager.
This is a forced shutdown and does not wait for tasks to complete their cleanup. It is useful for situations where a quick, non-graceful termination is required.
Sourcepub async fn graceful_shutdown(
self,
timeout: Duration,
) -> Result<(), ShutdownError>
pub async fn graceful_shutdown( self, timeout: Duration, ) -> Result<(), ShutdownError>
Initiates a graceful shutdown of all managed tasks.
This method first broadcasts the shutdown signal. It then waits for all tasks to complete, up to the specified timeout.
This method consumes the ShutdownManager, preventing it from being used again.
Returns Ok(()) if all tasks shut down cleanly within the timeout.
Returns Err(ShutdownError) if a task panicked or if the timeout is reached.