Expand description

tokio-task-tracker is a simple graceful shutdown solution for tokio.

The basic idea is to use a TaskSpawner to create TaskTracker object, and hold on to them in spawned tasks. Inside the task, you can check tracker.cancelled().await to wait for the task to be cancelled.

The TaskWaiter can be used to wait for an interrupt and then wait for all TaskTrackers to be dropped.

Examples

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let (spawner, waiter) = tokio_task_tracker::new();

    // Start a task
    spawner.spawn(|tracker| async move {
        tokio::select! {
            _ = tracker.cancelled() => {
                // The token was cancelled, task should shut down.
            }
            _ = tokio::time::sleep(Duration::from_secs(9999)) => {
                // Long work has completed
            }
        }
    });

    // Wait for all tasks to complete, or for someone to hit ctrl-c.
    // If tasks down't complete within 5 seconds, we'll quit anyways.
    waiter.wait_for_shutdown(Duration::from_secs(5)).await?;

    Ok(())
}

If you do not wish to allow a task to be aborted, you still need to make sure the task captures the tracker, because TaskWaiter will wait for all trackers to be dropped:

    // Start a task
    spawner.spawn(|tracker| async move {
        // Move the tracker into the task.
        let _tracker = tracker;

        // Do some work that we don't want to abort.
        tokio::time::sleep(Duration::from_secs(9999)).await;
    });

You can also create a tracker via the task method:

    // Start a task
    let tracker = spawner.task();
    tokio::task::spawn(async move {
        // Move the tracker into the task.
        let _tracker = tracker;

        // ...
    });

Trackers can be used to spawn subtasks via tracker.subtask() or tracker.spawn().

Structs

  • Builder is used to create a TaskSpawner and TaskWaiter.
  • TaskSpawner is used to spawn new task trackers.
  • A TaskTracker is used both as a token to keep track of active tasks, and as a cancellation token to check to see if the current task should quit.
  • TaskWaiter is used to wait until all task trackers have been dropped.

Enums

Functions

  • Create a new TaskSpawner and TaskWaiter.