Skip to main content

Module background_task

Module background_task 

Source
Expand description

Trait for standardized background task spawning.

This module provides the BackgroundTask trait that formalizes how background services are spawned and integrated with shutdown handling.

§Example

use crate::shared::background_task::BackgroundTask;
use crate::shared::task_group::TaskGroup;
use crate::shared::shutdown::Shutdown;

struct MyService {
    // ... service state
}

impl BackgroundTask for MyService {
    const NAME: &'static str = "MyService";
     
    fn spawn(self, group: &mut TaskGroup, shutdown: Shutdown) {
        let mut shutdown_rx = shutdown.subscribe();
        group.spawn(Self::NAME, async move {
            loop {
                tokio::select! {
                    _ = shutdown_rx.recv() => break,
                    // ... do work
                }
            }
            Ok(())
        });
    }
}

Traits§

BackgroundTask
Trait for background services that can be spawned into a TaskGroup.
TaskGroupExt
Extension trait for spawning BackgroundTask implementations.