use std::time::Duration;
use taskvisor::prelude::*;
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let worker: TaskRef = TaskFn::arc(|ctx| async move {
let mut tick = 0u32;
loop {
match ctx
.run_until_cancelled(tokio::time::sleep(Duration::from_millis(250)))
.await
{
Ok(()) => {
tick += 1;
println!("[worker] tick #{tick}");
}
Err(canceled) => {
println!("[worker] application shutdown observed");
return Err(canceled);
}
}
}
});
let application_shutdown = async {
tokio::time::sleep(Duration::from_secs(1)).await;
println!("[application] requesting shutdown");
};
let supervisor = Supervisor::new(SupervisorConfig::default(), vec![]);
supervisor
.run_until(
vec![TaskSpec::restartable("worker", worker)],
application_shutdown,
)
.await?;
Ok(())
}