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
//! Module: api::timer
//!
//! Responsibility: expose the maintained application and lifecycle timer facade.
//! Does not own: timer state, recurrence, arbitration, or domain scheduling policy.
//! Boundary: macro-expanded downstream code delegates to TimerWorkflow.
use crate::workflow::runtime::timer::{ApplicationTimerId, TimerWorkflow};
use std::{future::Future, time::Duration};
/// Opaque, single-owner handle for a cancellable application timer.
#[derive(Debug, Eq, PartialEq)]
pub struct TimerHandle(ApplicationTimerId);
/// Public timer facade used by Canic's macro-expanded entrypoints.
pub struct TimerApi;
impl TimerApi {
/// Schedule a cancellable application one-shot.
pub fn set(
delay: Duration,
label: impl Into<String>,
task: impl Future<Output = ()> + 'static,
) -> TimerHandle {
TimerHandle(TimerWorkflow::set_application_once(delay, label, task))
}
/// Defer lifecycle work through the same one-shot authority.
pub fn defer_lifecycle(
delay: Duration,
label: impl Into<String>,
task: impl Future<Output = ()> + 'static,
) -> TimerHandle {
Self::set(delay, label, task)
}
/// Schedule a cancellable, non-overlapping after-completion interval.
pub fn set_interval<F, Fut>(
interval: Duration,
label: impl Into<String>,
task: F,
) -> TimerHandle
where
F: FnMut() -> Fut + 'static,
Fut: Future<Output = ()> + 'static,
{
TimerHandle(TimerWorkflow::set_application_interval(
interval, label, task,
))
}
/// Consume a timer handle and suppress any future invocation.
#[must_use]
pub fn cancel(handle: TimerHandle) -> bool {
TimerWorkflow::cancel_application(handle.0)
}
}