Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
```bash
cargo run --example {{ example_name }} --features {{ features }}
```
| Example | Description | Required Features |
|---------------------------------------------------------|------------------------------------------------|---------------------|
| [basic_one_shot.rs](examples/basic_one_shot.rs) | Single one-shot task, graceful shutdown | - |
| [retry_with_backoff.rs](examples/retry_with_backoff.rs) | Retry loop with exponential backoff and jitter | - |
| [dynamic_add_remove.rs](examples/dynamic_add_remove.rs) | Add/Remove tasks at runtime via API | - |
| [custom_subscriber.rs](examples/custom_subscriber.rs) | Custom subscriber reacting to events | - |
| [task_cancel.rs](examples/task_cancel.rs) | Task cancellation from outside | logging |
| [controller.rs](examples/controller.rs) | Examples with `controller` feature | controller |
| Example | Description | Features |
|---------------------------------------------------------|-----------------------------------------------------|------------|
| [subscriber.rs](examples/subscriber.rs) | Custom subscriber reacting to events | - |
| [control.rs](examples/control.rs) | Tasks add/remove/cancel while supervisor is running | - |
| [controller.rs](examples/controller.rs) | Tasks example with additional admissions | controller |

## Key features
- **[Supervisor](./src/core/supervisor.rs)** manage async tasks, tracks lifecycle, handles requests, and drives graceful shutdown.
Expand Down
71 changes: 0 additions & 71 deletions examples/basic_one_shot.rs

This file was deleted.

92 changes: 92 additions & 0 deletions examples/control.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//! # Runtime Control Example
//!
//! Shows how to add, remove, and cancel tasks while supervisor is running.
//!
//! Demonstrates:
//! - Adding tasks dynamically
//! - Removing tasks by name
//! - Checking task status
//!
//! ## Run
//! ```bash
//! cargo run --example control
//! ```

use std::{sync::Arc, time::Duration};
use taskvisor::{
BackoffPolicy, Config, RestartPolicy, Supervisor, TaskError, TaskFn, TaskRef, TaskSpec,
};
use tokio_util::sync::CancellationToken;

#[tokio::main(flavor = "current_thread")]
async fn main() -> anyhow::Result<()> {
let sup = Arc::new(Supervisor::new(Config::default(), vec![]));
let runner = Arc::clone(&sup);
tokio::spawn(async move {
let _ = runner.run(vec![]).await;
});
sup.wait_ready().await;

// ============================================================
// Demo 1: Add task dynamically
// ============================================================
println!(" ─► Adding 'worker-A'...");

sup.add_task(make_worker("worker-A"))?;
tokio::time::sleep(Duration::from_secs(1)).await;
let tasks = sup.list_tasks().await;
println!(" ─► Active tasks: {tasks:?}");

// ============================================================
// Demo 2: Add second task
// ============================================================
println!(" ─► Adding 'worker-B'...");

sup.add_task(make_worker("worker-B"))?;
tokio::time::sleep(Duration::from_secs(1)).await;
let tasks = sup.list_tasks().await;
println!(" ─► Active tasks: {tasks:?}");

// ============================================================
// Demo 3: Remove specific task
// ============================================================
println!(" ─► Removing 'worker-A'...");

sup.remove_task("worker-A")?;
tokio::time::sleep(Duration::from_millis(500)).await;
let tasks = sup.list_tasks().await;
println!(" ─► Active tasks: {tasks:?}");

// ============================================================
// Demo 4: Cancel task (with confirmation)
// ============================================================
println!(" ─► Cancelling 'worker-B'...");

let cancelled = sup.cancel("worker-B").await?;
println!(" ─► Task cancelled: {cancelled}");

let alive = sup.is_alive("worker-B").await;
println!(" ─► Is alive: {alive}");

println!("Done");
Ok(())
}

fn make_worker(name: &'static str) -> TaskSpec {
let task: TaskRef = TaskFn::arc(name, move |ctx: CancellationToken| async move {
println!("{:>4}[{name}] started", "");

let mut counter = 0u32;
loop {
if ctx.is_cancelled() {
println!("{:>4}[{name}] cancelled", "");
return Err(TaskError::Canceled);
}

counter += 1;
println!("{:>4}[{name}] tick #{counter}", "");
tokio::time::sleep(Duration::from_millis(500)).await;
}
});
TaskSpec::new(task, RestartPolicy::Always, BackoffPolicy::default(), None)
}
Loading