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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//! # taskvisor
//!
//! Taskvisor is a task supervisor for Tokio: it restarts background tasks on failure and reports every step through typed events.
//!
//! It helps you run named async tasks with:
//!
//! - restart policies,
//! - retry backoff,
//! - per-attempt timeout,
//! - cooperative cancellation,
//! - lifecycle events,
//! - optional final outcomes through `*_and_watch` APIs.
//!
//! The crate is meant to be a building block for services, agents, workers, and higher-level orchestration SDKs.
//!
//! ## Core Model
//!
//! ```text
//! TaskFn / impl Task
//! |
//! v
//! TaskSpec
//! |
//! v
//! Supervisor ---- publishes ----> Event bus ----> Subscribe
//! |
//! v
//! TaskActor
//! (attempt loop)
//! ```
//!
//! A [`Task`] is the user unit of work.
//! A [`TaskSpec`] configures how that task runs.
//! A [`Supervisor`] owns the runtime and manages task lifecycle.
//! A [`Subscribe`] implementation can observe lifecycle events.
//!
//! ## Task Lifecycle
//!
//! ```text
//! add task
//! -> TaskStarting
//! -> task attempt runs
//! -> TaskStopped / TaskFailed / TimeoutHit / TaskCanceled
//! -> restart, backoff, or finish
//! -> TaskRemoved
//! ```
//!
//! Events are best-effort observability.
//! If you need a guaranteed final result for one task, use [`SupervisorHandle::add_and_watch`] or, with the `controller` feature, `submit_and_watch`.
//!
//! ## Main Types
//!
//! | Area | Types |
//! |----------|------------------------------------------------------------|
//! | Tasks | [`Task`], [`TaskFn`], [`TaskContext`], [`TaskSpec`] |
//! | Runtime | [`Supervisor`], [`SupervisorHandle`], [`SupervisorConfig`] |
//! | Outcomes | [`TaskWaiter`], [`TaskOutcome`] |
//! | Policies | [`RestartPolicy`], [`BackoffPolicy`], [`JitterPolicy`] |
//! | Events | [`Event`], [`EventKind`], [`Subscribe`] |
//! | Errors | [`Error`], [`TaskError`], [`RuntimeError`] |
//!
//! All main types are re-exported at the crate root.
//! For the concept behind each area, read the module pages:
//! [`tasks`], [`policies`], [`events`], [`subscribers`], [`core`], [`identity`], and `controller` (feature-gated).
//!
//! ## Optional Features
//!
//! - `tokio-util-interop`: exposes raw `tokio_util::sync::CancellationToken` interop through [`TaskContext`] and the prelude.
//! - `controller`: slot-based admission control with `ControllerSpec`, `AdmissionPolicy`, and `ControllerConfig`.
//! - `tracing`: built-in `TracingBridge` subscriber that forwards runtime events to the `tracing` ecosystem.
//! - `logging`: built-in `LogWriter` subscriber for examples and simple logs (dev, preview only).
//! - `test-util`: test helpers (`TaskContext::detached()`, `TaskId::for_tests()`, `TaskOutcome::*_for_tests`).
//!
//! ## Quick Start
//!
//! ```rust
//! use taskvisor::prelude::*;
//!
//! #[tokio::main(flavor = "current_thread")]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let sup = Supervisor::new(SupervisorConfig::default(), vec![]);
//!
//! let hello: TaskRef = TaskFn::arc("hello", |_ctx| async move {
//! println!("Hello from task!");
//! Ok(())
//! });
//!
//! sup.run(vec![TaskSpec::once(hello)]).await?;
//! Ok(())
//! }
//! ```
//!
//! ## Examples
//!
//! All examples run as-is, from simple to advanced
//! ([browse them on GitHub](https://github.com/soltiHQ/taskvisor/tree/main/examples)):
//!
//! | Example | What it shows |
//! |---------------------------------------------------------------------------------------------|-------------------------------------------------------------------|
//! | [basic](https://github.com/soltiHQ/taskvisor/blob/main/examples/basic.rs) | Run one task and exit — the minimal wiring |
//! | [worker](https://github.com/soltiHQ/taskvisor/blob/main/examples/worker.rs) | A long-running worker that stops cleanly on Ctrl+C |
//! | [periodic](https://github.com/soltiHQ/taskvisor/blob/main/examples/periodic.rs) | Run a job every N seconds, forever |
//! | [multiple](https://github.com/soltiHQ/taskvisor/blob/main/examples/multiple.rs) | Several tasks with different restart rules under one supervisor |
//! | [queue_consumer](https://github.com/soltiHQ/taskvisor/blob/main/examples/queue_consumer.rs) | A message consumer that reconnects after failures |
//! | [cpu_job](https://github.com/soltiHQ/taskvisor/blob/main/examples/cpu_job.rs) | Run CPU-heavy work on rayon, supervised, without blocking Tokio |
//! | [subscriber](https://github.com/soltiHQ/taskvisor/blob/main/examples/subscriber.rs) | React to lifecycle events with your own handler |
//! | [tracing](https://github.com/soltiHQ/taskvisor/blob/main/examples/tracing.rs) | Send supervisor events into your logs (feature `tracing`) |
//! | [metrics](https://github.com/soltiHQ/taskvisor/blob/main/examples/metrics.rs) | Count lifecycle events as Prometheus metrics |
//! | [dynamic](https://github.com/soltiHQ/taskvisor/blob/main/examples/dynamic.rs) | Add, cancel, and remove tasks while the app is running |
//! | [outcomes](https://github.com/soltiHQ/taskvisor/blob/main/examples/outcomes.rs) | Wait for a task's final result: done, failed, or canceled |
//! | [slots](https://github.com/soltiHQ/taskvisor/blob/main/examples/slots.rs) | Limit concurrency per slot: queue, replace, or drop the newcomer |
//! | [admission](https://github.com/soltiHQ/taskvisor/blob/main/examples/admission.rs) | Find out if your submission ran or was rejected |
/// Compiles runnable Rust code blocks in `README.md` as doctests.
;
pub use TaskId;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use Subscribe;
pub use ;
pub use LogWriter;
pub use TracingBridge;