taskvisor 0.1.2

Event-driven task orchestration with restart, backoff, and user-defined subscribers
Documentation
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
//! # TaskActor: single-task supervisor.
//!
//! Supervises execution of one [`Task`] with policies:
//! - restarts per [`RestartPolicy`],
//! - delays per [`BackoffPolicy`],
//! - optional per-attempt timeout,
//!
//! *Cooperative cancellation via `CancellationToken`.*
//!
//! ## Event flow
//!
//! For each attempt, the actor publishes:
//! ```text
//! TaskStarting → [task execution] → TaskStopped (success/cancel)
//!                                 → TimeoutHit (timeout)
//!                                 → TaskFailed (error)
//!
//! If retry scheduled:
//!   → BackoffScheduled → [sleep] → (next attempt with new seq)
//!
//! On exit:
//!   → ActorExhausted (policy forbids restart)
//!   → ActorDead (fatal error)
//!   → (no terminal event if cancelled - runner already published TaskStopped)
//!```
//!
//! ## Architecture
//!
//! ```text
//! TaskSpec ──► Supervisor ──► TaskActor::run()
//!
//! loop {
//!   ├─► check cancellation (fast-path with runtime_token)
//!   ├─► acquire semaphore (cancellable with runtime_token)
//!   ├─► check cancellation (after acquire)
//!   ├─► create child_token (isolated per attempt)
//!   ├─► attempt += 1
//!   ├─► publish TaskStarting
//!   ├─► run_once(child_token) ─► task.spawn()
//!   │       │              ▼
//!   │       │        (one attempt)
//!   │       │              ▼
//!   │       └─► runner publishes:
//!   │             - TaskStopped (success/cancel)
//!   │             - TaskFailed (error)
//!   │             - TimeoutHit (timeout)
//!   ├─► drop permit (release semaphore)
//!   ├─► child_token goes out of scope
//!   ├─► apply RestartPolicy
//!   │     ├─► OnFailure   → if success: publish ActorExhausted → break
//!   │     ├─► Never       → publish ActorExhausted → break
//!   │     └─► Always      → continue (on success)
//!   └─► on retryable failure:
//!        ├─► publish BackoffScheduled
//!        └─► sleep(backoff_delay) with runtime_token (cancellable)
//! }
//! ```
//!
//! ## Rules
//!
//! - Attempts run **sequentially** within one actor (never parallel)
//! - Attempt counter **increments on each spawn** (monotonic, never resets)
//! - Events have **monotonic sequence numbers** (ordering guarantees)
//! - Each attempt gets its own **child_token** for isolation

use std::{sync::Arc, time::Duration};
use tokio::sync::Semaphore;
use tokio_util::sync::CancellationToken;

use crate::{
    TaskError,
    core::runner::run_once,
    events::{Bus, Event, EventKind},
    policies::{BackoffPolicy, RestartPolicy},
    tasks::Task,
};

/// Reason why a task actor exited.
///
/// Used to determine what event to publish and whether to clean up the task from registry.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ActorExitReason {
    /// Actor exhausted its restart policy and will not restart.
    ///
    /// Occurs when:
    /// - `RestartPolicy::Never` and task completed (success or failure)
    /// - `RestartPolicy::OnFailure` and task completed successfully
    PolicyExhausted,

    /// Actor was canceled due to shut down signal or explicit removal.
    ///
    /// The task detected `CancellationToken::is_cancelled()` and exited gracefully.
    Cancelled,

    /// Actor died due to a fatal error that should not be retried.
    ///
    /// Occurs when:
    /// - Task returned `TaskError::Fatal`
    Fatal,
}

/// Configuration parameters for a task actor.
#[derive(Clone)]
pub(crate) struct TaskActorParams {
    /// When to restart the task.
    pub(crate) restart: RestartPolicy,
    /// How to compute retry delays.
    pub(crate) backoff: BackoffPolicy,
    /// Optional per-attempt timeout (`None` = no timeout).
    pub(crate) timeout: Option<Duration>,
    /// Maximum retry attempts after failure (`0` = unlimited).
    pub(crate) max_retries: u32,
}

/// Supervises execution of a single [`Task`] with retries, backoff, and event publishing.
///
/// # Also
///
/// - [`run_once`](super::runner::run_once) - executes a single attempt
/// - [`Registry`](super::registry::Registry) - spawns and owns task actors
/// - [`TaskSpec`](crate::TaskSpec) - user-facing configuration that maps to [`TaskActorParams`]
pub(crate) struct TaskActor {
    /// Task to execute.
    task: Arc<dyn Task>,
    /// Parameters for supervised task executions.
    params: TaskActorParams,
    /// Internal event bus (used to publish lifecycle events).
    bus: Bus,
    /// Optional global tasks concurrency limiter.
    semaphore: Option<Arc<Semaphore>>,
}

impl TaskActor {
    /// Creates a new task actor.
    pub(crate) fn new(
        bus: Bus,
        task: Arc<dyn Task>,
        params: TaskActorParams,
        semaphore: Option<Arc<Semaphore>>,
    ) -> Self {
        Self {
            task,
            params,
            bus,
            semaphore,
        }
    }

    /// Runs the actor until completion, restart exhaustion, or cancellation.
    ///
    /// We run each attempt under a **child** cancellation token so that cancelling the current attempt
    /// (including backoff sleep) results in a clean exit without restarting, even with `RestartPolicy::Always`.
    pub(crate) async fn run(self, runtime_token: CancellationToken) -> ActorExitReason {
        let task_name: Arc<str> = Arc::from(self.task.name());
        let mut attempt: u32 = 0;
        let mut backoff_attempt: u32 = 0;

        loop {
            if runtime_token.is_cancelled() {
                return ActorExitReason::Cancelled;
            }
            let permit = match &self.semaphore {
                Some(sem) => {
                    let fut = sem.clone().acquire_owned();
                    tokio::pin!(fut);

                    tokio::select! {
                        res = &mut fut => match res {
                            Ok(p) => Some(p),
                            Err(_closed) => {
                                self.bus.publish(
                                    Event::new(EventKind::ActorExhausted)
                                        .with_task(task_name.clone())
                                        .with_attempt(attempt)
                                        .with_reason("semaphore_closed")
                                );
                                return ActorExitReason::Cancelled;
                            }
                        },
                        _ = runtime_token.cancelled() => {
                            return ActorExitReason::Cancelled;
                        }
                    }
                }
                None => None,
            };
            if runtime_token.is_cancelled() {
                drop(permit);
                return ActorExitReason::Cancelled;
            }

            let child = runtime_token.child_token();
            attempt += 1;

            self.bus.publish(
                Event::new(EventKind::TaskStarting)
                    .with_task(task_name.clone())
                    .with_attempt(attempt),
            );
            let res = run_once(
                self.task.as_ref(),
                &child,
                self.params.timeout,
                attempt,
                &self.bus,
            )
            .await;

            drop(permit);
            match res {
                Ok(()) => {
                    backoff_attempt = 0;

                    match self.params.restart {
                        RestartPolicy::Always { interval } => {
                            if let Some(d) = interval {
                                self.bus.publish(
                                    Event::new(EventKind::BackoffScheduled)
                                        .with_backoff_success()
                                        .with_task(task_name.clone())
                                        .with_attempt(attempt)
                                        .with_delay(d),
                                );
                                if !Self::sleep_cancellable(d, &runtime_token).await {
                                    return ActorExitReason::Cancelled;
                                }
                            }
                            continue;
                        }
                        RestartPolicy::OnFailure | RestartPolicy::Never => {
                            self.bus.publish(
                                Event::new(EventKind::ActorExhausted)
                                    .with_task(task_name.clone())
                                    .with_attempt(attempt)
                                    .with_reason("policy_exhausted_success"),
                            );
                            return ActorExitReason::PolicyExhausted;
                        }
                    }
                }
                Err(e) if e.is_fatal() => {
                    let mut ev = Event::new(EventKind::ActorDead)
                        .with_task(task_name.clone())
                        .with_attempt(attempt)
                        .with_reason(e.to_string());
                    if let Some(code) = e.exit_code() {
                        ev = ev.with_exit_code(code);
                    }
                    self.bus.publish(ev);
                    return ActorExitReason::Fatal;
                }
                Err(TaskError::Canceled) => {
                    return ActorExitReason::Cancelled;
                }
                Err(e) => {
                    let policy_allows_retry = matches!(
                        self.params.restart,
                        RestartPolicy::OnFailure | RestartPolicy::Always { .. }
                    );
                    let error_is_retryable = e.is_retryable();
                    let retries_exhausted =
                        self.params.max_retries > 0 && backoff_attempt >= self.params.max_retries;

                    if !(policy_allows_retry && error_is_retryable) || retries_exhausted {
                        let reason = if retries_exhausted {
                            format!(
                                "max_retries_exceeded({}/{}): {}",
                                backoff_attempt, self.params.max_retries, e
                            )
                        } else {
                            e.to_string()
                        };
                        let mut ev = Event::new(EventKind::ActorExhausted)
                            .with_task(task_name.clone())
                            .with_attempt(attempt)
                            .with_reason(reason);
                        if let Some(code) = e.exit_code() {
                            ev = ev.with_exit_code(code);
                        }
                        self.bus.publish(ev);
                        return ActorExitReason::PolicyExhausted;
                    }

                    let delay = self.params.backoff.next(backoff_attempt);
                    backoff_attempt += 1;

                    self.bus.publish(
                        Event::new(EventKind::BackoffScheduled)
                            .with_backoff_failure()
                            .with_task(task_name.clone())
                            .with_delay(delay)
                            .with_attempt(attempt)
                            .with_reason(e.to_string()),
                    );
                    if !Self::sleep_cancellable(delay, &runtime_token).await {
                        return ActorExitReason::Cancelled;
                    }
                }
            }
        }
    }

    /// Sleep helper that can be interrupted by a cancellation token.
    ///
    /// Returns `true` if sleep completed, `false` if cancelled.
    #[inline]
    async fn sleep_cancellable(duration: Duration, token: &CancellationToken) -> bool {
        let sleep = tokio::time::sleep(duration);
        tokio::pin!(sleep);

        tokio::select! {
            _ = &mut sleep => true,
            _ = token.cancelled() => false,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::future::Future;
    use std::pin::Pin;
    use std::sync::atomic::{AtomicU32, Ordering};

    type BoxFut = Pin<Box<dyn Future<Output = Result<(), TaskError>> + Send + 'static>>;

    fn fast_backoff() -> BackoffPolicy {
        BackoffPolicy {
            first: Duration::from_millis(1),
            max: Duration::from_millis(1),
            factor: 1.0,
            jitter: crate::JitterPolicy::None,
        }
    }

    fn params(restart: RestartPolicy, max_retries: u32) -> TaskActorParams {
        TaskActorParams {
            restart,
            backoff: fast_backoff(),
            timeout: None,
            max_retries,
        }
    }

    fn actor(task: Arc<dyn Task>, restart: RestartPolicy, max_retries: u32) -> TaskActor {
        TaskActor::new(
            Bus::new(16),
            Arc::clone(&task),
            params(restart, max_retries),
            None,
        )
    }

    struct OkTask;
    impl Task for OkTask {
        fn name(&self) -> &str {
            "ok"
        }
        fn spawn(&self, _ctx: CancellationToken) -> BoxFut {
            Box::pin(async { Ok(()) })
        }
    }

    struct FailTask;
    impl Task for FailTask {
        fn name(&self) -> &str {
            "fail"
        }
        fn spawn(&self, _ctx: CancellationToken) -> BoxFut {
            Box::pin(async {
                Err(TaskError::Fail {
                    reason: "boom".into(),
                    exit_code: None,
                })
            })
        }
    }

    struct FatalTask;
    impl Task for FatalTask {
        fn name(&self) -> &str {
            "fatal"
        }
        fn spawn(&self, _ctx: CancellationToken) -> BoxFut {
            Box::pin(async {
                Err(TaskError::Fatal {
                    reason: "fatal".into(),
                    exit_code: None,
                })
            })
        }
    }

    struct CountedTask {
        remaining: AtomicU32,
    }
    impl CountedTask {
        fn new(fail_count: u32) -> Self {
            Self {
                remaining: AtomicU32::new(fail_count),
            }
        }
    }
    impl Task for CountedTask {
        fn name(&self) -> &str {
            "counted"
        }
        fn spawn(&self, _ctx: CancellationToken) -> BoxFut {
            let prev = self.remaining.fetch_sub(1, Ordering::SeqCst);
            if prev > 0 {
                Box::pin(async {
                    Err(TaskError::Fail {
                        reason: "transient".into(),
                        exit_code: None,
                    })
                })
            } else {
                Box::pin(async { Ok(()) })
            }
        }
    }

    #[tokio::test]
    async fn never_ok_returns_policy_exhausted() {
        let a = actor(Arc::new(OkTask), RestartPolicy::Never, 0);
        let reason = a.run(CancellationToken::new()).await;
        assert_eq!(reason, ActorExitReason::PolicyExhausted);
    }

    #[tokio::test]
    async fn on_failure_ok_returns_policy_exhausted() {
        let a = actor(Arc::new(OkTask), RestartPolicy::OnFailure, 0);
        let reason = a.run(CancellationToken::new()).await;
        assert_eq!(reason, ActorExitReason::PolicyExhausted);
    }

    #[tokio::test]
    async fn fatal_error_returns_fatal() {
        let a = actor(Arc::new(FatalTask), RestartPolicy::OnFailure, 0);
        let reason = a.run(CancellationToken::new()).await;
        assert_eq!(reason, ActorExitReason::Fatal);
    }

    #[tokio::test]
    async fn max_retries_exhausted_returns_policy_exhausted() {
        let a = actor(Arc::new(FailTask), RestartPolicy::OnFailure, 3);
        let reason = a.run(CancellationToken::new()).await;
        assert_eq!(reason, ActorExitReason::PolicyExhausted);
    }

    #[tokio::test]
    async fn cancellation_returns_cancelled() {
        let token = CancellationToken::new();
        token.cancel();
        let a = actor(
            Arc::new(OkTask),
            RestartPolicy::Always { interval: None },
            0,
        );
        let reason = a.run(token).await;
        assert_eq!(reason, ActorExitReason::Cancelled);
    }

    #[tokio::test]
    async fn on_failure_retries_then_succeeds() {
        let task = Arc::new(CountedTask::new(2));
        let a = actor(task, RestartPolicy::OnFailure, 0);
        let reason = a.run(CancellationToken::new()).await;
        assert_eq!(reason, ActorExitReason::PolicyExhausted);
    }
}