pub struct BackgroundTasks { /* private fields */ }Expand description
Spawns and tracks background tasks tied to the server lifecycle.
Tasks receive a Shutdown handle and are expected to return soon after
it triggers. During graceful shutdown the server waits (up to the
shutdown timeout) for every tracked task to finish.
This is intentionally small: it is not a job queue, it has no retries and no persistence.
use std::time::Duration;
use kerniq_runtime::{BackgroundTasks, Shutdown};
let shutdown = Shutdown::new();
let tasks = BackgroundTasks::new(shutdown.clone());
tasks.spawn("ticker", |shutdown| async move {
let mut interval = tokio::time::interval(Duration::from_millis(10));
loop {
tokio::select! {
_ = interval.tick() => { /* periodic work */ }
() = shutdown.wait() => break,
}
}
});
shutdown.trigger();
assert!(tasks.shutdown(Duration::from_secs(1)).await);Implementations§
Trait Implementations§
Source§impl Clone for BackgroundTasks
impl Clone for BackgroundTasks
Auto Trait Implementations§
impl Forget for BackgroundTasks
impl Freeze for BackgroundTasks
impl RefUnwindSafe for BackgroundTasks
impl Send for BackgroundTasks
impl Sync for BackgroundTasks
impl Unpin for BackgroundTasks
impl UnsafeUnpin for BackgroundTasks
impl UnwindSafe for BackgroundTasks
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