pub fn spawn_background_task<E: Display>(
    span: Span,
    interval_pause: Option<Duration>,
    step_runner: impl FnMut() -> Result<Next, TaskError<E>> + Send + Sync + 'static
) -> TaskHandle
Expand description

Spawn a long-running background task with the given step runner.

The step runner is a FnMut closure that is called repeatedly and returns a Result<(), TaskError<E>>. If the step is executed successfuly, the step runner should return Ok(()) so that it will be called again.

Otherwise if errors occurred or of the task needs to be aborted, the step runner should return a TaskError<E> that instructs the task runner of whether the background task should be aborted.

The function is also given a task name string, which is used for logging information about the execution of the task. An optional Duration argument is also given for the task runner to sleep for the given duration before calling the step runner again.

The function returns a TaskHandle that can be used to shutdown the background task. If the TaskHandle is dropped or if explicit shutdown instruction is sent, the task runner will stop calling the step runner and abort the background task.

If the step runner is receiving commands from other channels, it should use the try_recv function so that the step runner do not get stuck indefinitely even when shutdown instruction has been sent through the TaskHandle.