use std::sync::Arc;
use std::sync::atomic::{AtomicU32, Ordering};
use std::time::Duration;
use taskvisor::prelude::*;
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let runtime = SupervisorConfig::default()
.try_with_max_concurrent(4)?
.try_with_max_registered_tasks(64)?
.try_with_ownership_capacity(128)?
.with_grace(Duration::from_secs(5));
let defaults = TaskDefaults::default()
.with_backoff(BackoffPolicy::constant(Duration::from_millis(100)))
.with_timeout(Duration::from_secs(2))
.try_with_max_retries(3)?;
let attempts = Arc::new(AtomicU32::new(0));
let task: TaskRef = TaskFn::arc(move |_ctx| {
let attempts = Arc::clone(&attempts);
async move {
let attempt = attempts.fetch_add(1, Ordering::Relaxed) + 1;
println!("[configured-job] attempt #{attempt}");
if attempt == 1 {
Err(TaskError::fail("temporary failure"))
} else {
Ok(())
}
}
});
let spec = TaskSpec::from_defaults("configured-job", task).with_timeout(Duration::from_secs(1));
let supervisor = Supervisor::builder(runtime)
.with_task_defaults(defaults)
.try_build()?;
supervisor.run(vec![spec]).await?;
Ok(())
}