pub struct ShutdownAwareTaskSpawner { /* private fields */ }Expand description
Shutdown-aware task spawner
A wrapper around GracefulShutdown for spawning named tasks that respect
shutdown signals. Provides logging and automatic cancellation on shutdown.
§Example
use allframe_core::shutdown::{GracefulShutdown, ShutdownAwareTaskSpawner};
use std::sync::Arc;
#[tokio::main]
async fn main() {
let shutdown = Arc::new(GracefulShutdown::new());
let spawner = ShutdownAwareTaskSpawner::new(shutdown.clone());
// Spawn a task that will be cancelled on shutdown
spawner.spawn("my_task", || async {
loop {
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
println!("Working...");
}
});
// Shutdown will cancel the spawned task
shutdown.shutdown();
}Implementations§
Source§impl ShutdownAwareTaskSpawner
impl ShutdownAwareTaskSpawner
Sourcepub fn new(shutdown: Arc<GracefulShutdown>) -> Self
pub fn new(shutdown: Arc<GracefulShutdown>) -> Self
Create a new shutdown-aware task spawner
Sourcepub fn shutdown(&self) -> &Arc<GracefulShutdown>
pub fn shutdown(&self) -> &Arc<GracefulShutdown>
Get a reference to the underlying shutdown handler
Sourcepub fn spawn<F, Fut>(&self, task_name: &str, future: F) -> JoinHandle<()>
pub fn spawn<F, Fut>(&self, task_name: &str, future: F) -> JoinHandle<()>
Spawn a task that will be cancelled on shutdown
The task will be logged when starting, completing, and cancelling.
Sourcepub fn spawn_background<F, Fut>(
&self,
task_name: &str,
future: F,
) -> JoinHandle<()>
pub fn spawn_background<F, Fut>( &self, task_name: &str, future: F, ) -> JoinHandle<()>
Spawn a long-running background task
This is an alias for spawn - both handle shutdown the same way.
Use this to semantically indicate the task is intended to run
for the lifetime of the application.
Sourcepub fn spawn_with_result<F, Fut, T>(
&self,
task_name: &str,
future: F,
) -> JoinHandle<Option<T>>
pub fn spawn_with_result<F, Fut, T>( &self, task_name: &str, future: F, ) -> JoinHandle<Option<T>>
Spawn a task and return its result (if it completes before shutdown)
Trait Implementations§
Auto Trait Implementations§
impl Freeze for ShutdownAwareTaskSpawner
impl !RefUnwindSafe for ShutdownAwareTaskSpawner
impl Send for ShutdownAwareTaskSpawner
impl Sync for ShutdownAwareTaskSpawner
impl Unpin for ShutdownAwareTaskSpawner
impl !UnwindSafe for ShutdownAwareTaskSpawner
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more