pub trait BackgroundTask: Send + 'static {
const NAME: &'static str;
// Required method
fn spawn(self, group: &mut TaskGroup, shutdown: Shutdown);
}Expand description
Trait for background services that can be spawned into a TaskGroup.
Implement this trait to standardize how services are spawned and how they integrate with the shutdown mechanism.
Required Associated Constants§
Required Methods§
Sourcefn spawn(self, group: &mut TaskGroup, shutdown: Shutdown)
fn spawn(self, group: &mut TaskGroup, shutdown: Shutdown)
Spawn this task into the given TaskGroup.
The implementation should:
- Subscribe to the shutdown signal if needed
- Call
group.spawn(Self::NAME, ...)with a future that:- Respects shutdown signals via
tokio::select! - Returns
Ok(())on successful completion - Returns
Err(...)on failure
- Respects shutdown signals via
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.