Skip to main content

BackgroundTask

Trait BackgroundTask 

Source
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§

Source

const NAME: &'static str

A static name for this task, used for logging and error reporting.

Required Methods§

Source

fn spawn(self, group: &mut TaskGroup, shutdown: Shutdown)

Spawn this task into the given TaskGroup.

The implementation should:

  1. Subscribe to the shutdown signal if needed
  2. Call group.spawn(Self::NAME, ...) with a future that:
    • Respects shutdown signals via tokio::select!
    • Returns Ok(()) on successful completion
    • Returns Err(...) on failure

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.

Implementors§