atomr_patterns/cqrs/scheduled.rs
1//! Scheduled commands helper.
2//!
3//! [`schedule_command`] dispatches a command to a [`crate::Repository`]
4//! after a delay. Useful for aggregates that need a "fire reminder in
5//! N minutes" behaviour without dragging in the full
6//! [`atomr_core::actor::scheduler::Scheduler`] surface.
7
8use std::sync::Arc;
9use std::time::Duration;
10
11use crate::ddd::Repository;
12
13/// Send `cmd` to `repo` after `delay`. Returns immediately; the
14/// dispatch happens in a detached tokio task. Errors from the repo
15/// are logged at `warn` level — callers who care about the outcome
16/// should construct their own scheduling.
17pub fn schedule_command<R: Repository + 'static>(
18 repo: Arc<R>,
19 delay: Duration,
20 cmd: <R::Aggregate as atomr_persistence::Eventsourced>::Command,
21) where
22 <R::Aggregate as atomr_persistence::Eventsourced>::Error: std::fmt::Display,
23{
24 tokio::spawn(async move {
25 tokio::time::sleep(delay).await;
26 if let Err(e) = repo.send(cmd).await {
27 tracing::warn!(error = %e, "scheduled command failed");
28 }
29 });
30}