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§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
🔬This is a nightly-only experimental API. (
clone_to_uninit)Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
Wrap the input message
T in a tonic::RequestSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
Wrap the input message
T in a tonic::Request