pub struct DeadlineScheduler<DS: DeadlineStore> { /* private fields */ }Expand description
A background task that polls DeadlineStore::due_now and dispatches
deadline commands to the owning processes via a caller-supplied function.
Obtain via EngineContext::run_deadline_scheduler and drive by spawning
DeadlineScheduler::run in a Tokio task.
§Dispatch function
The dispatch function receives a fired Deadline and returns a future
that dispatches the appropriate timeout command to the process. The function
is responsible for resuming the correct workflow and calling execute.
After the future completes, the scheduler cancels the deadline from the
store regardless of the dispatch outcome (to prevent re-firing).
use std::time::Duration;
let scheduler = ctx.run_deadline_scheduler(
|deadline| async move {
tracing::warn!(
deadline_id = %deadline.deadline_id(),
label = %deadline.label(),
"deadline fired",
);
Ok(())
},
100,
Duration::from_secs(30),
);
tokio::spawn(async move { scheduler.run().await });Implementations§
Source§impl<DS: DeadlineStore> DeadlineScheduler<DS>
impl<DS: DeadlineStore> DeadlineScheduler<DS>
Sourcepub async fn run(self)
pub async fn run(self)
Run the deadline poll loop until the shutdown token is cancelled.
Cancellation is observed between deadlines and during the idle sleep, so
a deadline already being dispatched runs to completion. A deadline left
undispatched stays registered and fires on the next start — it is due, so
the next due_now returns it again.
Source§impl<DS: DeadlineStore> DeadlineScheduler<DS>
impl<DS: DeadlineStore> DeadlineScheduler<DS>
Sourcepub fn with_heartbeat(self, heartbeat: Arc<AtomicI64>) -> Self
pub fn with_heartbeat(self, heartbeat: Arc<AtomicI64>) -> Self
Attach a liveness heartbeat to this scheduler.
The scheduler will store the current UTC Unix timestamp (seconds) into
heartbeat at the end of every poll cycle.
Sourcepub fn with_shutdown(self, shutdown: CancellationToken) -> Self
pub fn with_shutdown(self, shutdown: CancellationToken) -> Self
Attach a graceful-shutdown token.
Cancelling it makes DeadlineScheduler::run return at the next
deadline boundary or immediately out of its idle sleep, so the caller can
close the event store once the scheduler has stopped writing to it.