Crate tokio_tasks_shutdown
source ·Expand description
Easily manage and gracefully shutdown tokio tasks while monitoring their return results
Example
use {std::time::Duration, tokio::time::sleep, tokio_tasks_shutdown::*};
// By default this will catch Ctrl+C.
// You may have your tasks return your own error type.
let tasks: TasksMainHandle<anyhow::Error> = TasksBuilder::default()
.timeouts(Some(Duration::from_secs(2)), Some(Duration::from_millis(500)))
.build();
// Spawn tasks
tasks
.spawn("gracefully_shutting_down_task", |tasks_handle| async move {
loop {
match tasks_handle
.on_shutdown_or({
// Simulating another future running concurrently,
// e.g. listening on a channel...
sleep(Duration::from_millis(100))
})
.await
{
ShouldShutdownOr::ShouldShutdown => {
// We have been kindly asked to shutdown, let's exit
break;
}
ShouldShutdownOr::ShouldNotShutdown(res) => {
// Got result of channel listening
}
}
}
Ok(())
// Note that if a task were to error, graceful shutdown would be initiated.
// This behavior can be disabled.
})
.unwrap();
// Note that calls can be chained since `spawn` returns `&TasksHandle`
// Let's simulate a Ctrl+C after some time
let tasks_handle: TasksHandle<_> = tasks.handle();
tokio::task::spawn(async move {
sleep(Duration::from_millis(150)).await;
tasks_handle.start_shutdown();
});
// Let's make sure there were no errors
tasks.join_all().await.unwrap();
// Make sure we have shut down when expected
assert!(
test_duration > Duration::from_millis(145) && test_duration < Duration::from_millis(155)
);
In this example, the task will have run one loop already (sleep has hit at t=100ms) when asked for graceful shutdown at t=150ms, which will immediately make it gracefully shut down.
Re-exports
pub use on_shutdown_or::ShouldShutdownOr;
Modules
- For advanced usage of
on_shutdown_or
- Public error/success types that may be returned by functions of this crate
Structs
- Builder for the set of tasks
TasksMainHandle
- Handle to a set of tasks. Can be used to spawn new tasks, initiate a shutdown or check shutdown status.
- Main handle to the set of tasks. This is the only one that may be used to collect their results.