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>) -> ShutdownAwareTaskSpawner
pub fn new(shutdown: Arc<GracefulShutdown>) -> ShutdownAwareTaskSpawner
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§
Source§impl Clone for ShutdownAwareTaskSpawner
impl Clone for ShutdownAwareTaskSpawner
Source§fn clone(&self) -> ShutdownAwareTaskSpawner
fn clone(&self) -> ShutdownAwareTaskSpawner
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl !RefUnwindSafe for ShutdownAwareTaskSpawner
impl !UnwindSafe for ShutdownAwareTaskSpawner
impl Forget for ShutdownAwareTaskSpawner
impl Freeze for ShutdownAwareTaskSpawner
impl Move for ShutdownAwareTaskSpawner
impl Send for ShutdownAwareTaskSpawner
impl Sync for ShutdownAwareTaskSpawner
impl Unpin for ShutdownAwareTaskSpawner
impl UnsafeUnpin 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