| Crates.io | tokio-warden-restartable |
| lib.rs | tokio-warden-restartable |
| version | 0.1.0 |
| created_at | 2025-12-27 08:53:06.753507+00 |
| updated_at | 2025-12-27 08:53:06.753507+00 |
| description | A utility for creating auto-restarting tokio tasks |
| homepage | |
| repository | |
| max_upload_size | |
| id | 2006899 |
| size | 9,197 |
tokio-warden-restartableA utility for creating auto-restarting tokio tasks.
This crate provides [Restartable], a future that wraps a spawned tokio task
and automatically restarts it whenever it completes or is aborted. This is useful
for long-running background tasks that should be resilient to failures.
use std::time::Duration;
use tokio_warden_restartable::restartable;
#[tokio::main]
async fn main() {
// Create a restartable task that automatically restarts on completion
let restartable = restartable(|prev| {
if let Some(result) = prev {
println!("Previous task ended with: {:?}", result);
}
tokio::spawn(async {
println!("Task started");
tokio::time::sleep(Duration::from_secs(5)).await;
println!("Task completed");
})
});
// Get a handle to manually trigger restarts
let restart_handle = restartable.restart_handle();
// Spawn the restartable as a background task
let handle = tokio::spawn(restartable);
// Trigger a restart from anywhere
tokio::time::sleep(Duration::from_secs(1)).await;
restart_handle.restart();
// The restartable runs forever until its outer handle is aborted
handle.abort();
}