1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//! # Periodic (Cron-Like)
//!
//! A task that runs, completes, waits 2 seconds, and runs again: forever.
//! Think of it as a lightweight cron job without external schedulers.
//!
//! ## What this shows
//!
//! - `RestartPolicy::Always { interval: Some(2s) }` - the supervisor waits `interval` after each successful completion before spawning the next run.
//! *With `interval: None`, restarts happen immediately.*
//! - The task itself is short-lived (print and exit).
//! The supervisor handles the scheduling loop: your task doesn't need its own `loop {}`.
//! - `CancellationToken` is unused here because the task completes instantly.
//!
//! ## How it differs from a loop inside the task
//!
//! You could write `loop { do_work(); sleep(2s); }` inside the task, but then:
//! - The supervisor sees one long-running task, not periodic completions.
//! - You lose per-attempt events (`TaskStarting`, `TaskStopped`) in subscribers.
//! - Backoff on failure doesn't apply (you'd have to handle it yourself).
//!
//! **With `RestartPolicy::Always`, each cycle is a separate attempt with full lifecycle observability.**
//!
//! ## Runtime flavor
//!
//! We use `current_thread` here because a single-threaded runtime is enough for examples and tests.
//!
//! *It can be used with `#[tokio::main]` (defaults to multi-thread): taskvisor works with both.*
//!
//!
//! ## Run
//! ```bash
//! cargo run --example periodic
//! # Press Ctrl+C to stop
//! ```
//!
//! ## Next
//!
//! | Example | What it adds |
//! |----------------------------|--------------------------------------------|
//! | [`multiple`](multiple) | Combine different policies |
//! | [`metrics.rs`](metrics.rs) | Observe lifecycle events with a subscriber |
use Duration;
use *;
async