scheduler/lib.rs
1//! Async scheduling for a single logical job at a time.
2//!
3//! The scheduler decides when to trigger work and persists the resulting job
4//! state through a [`StateStore`]. Domain-specific retry, idempotency, and
5//! cursor management remain in the caller.
6//!
7//! Key semantics:
8//!
9//! - [`Schedule::AtTimes`] waits until each planned timestamp and treats an
10//! empty list as a no-op schedule.
11//! - [`Schedule::Interval`] schedules the first run at `now + interval`.
12//! - [`Schedule::Cron`] evaluates a standard 5-field cron expression in
13//! [`SchedulerConfig::timezone`].
14//! - [`Job::with_max_runs`] applies to every schedule kind; `0` exits without
15//! running.
16//! - [`SchedulerConfig::timezone`] is forwarded through [`RunContext`], drives
17//! [`Schedule::Cron`] evaluation, and does not rewrite absolute
18//! [`Schedule::AtTimes`] values.
19//! - Restarts resume by `job_id` from the saved [`JobState::next_run_at`].
20//! - Dependency injection here means passing an explicit `deps` value when the
21//! job is constructed. The scheduler does not auto-resolve parameters.
22//!
23//! ```rust
24//! use std::time::Duration;
25//!
26//! use scheduler::{InMemoryStateStore, Job, Schedule, Scheduler, SchedulerConfig, Task};
27//!
28//! let runtime = tokio::runtime::Runtime::new().unwrap();
29//! runtime.block_on(async {
30//! let scheduler = Scheduler::new(SchedulerConfig::default(), InMemoryStateStore::new());
31//! let job = Job::without_deps(
32//! "doc-simple",
33//! Schedule::Interval(Duration::from_millis(1)),
34//! Task::from_async(|_| async { Ok(()) }),
35//! )
36//! .with_max_runs(1);
37//!
38//! let report = scheduler.run(job).await.unwrap();
39//! assert_eq!(report.history.len(), 1);
40//! });
41//! ```
42
43mod error;
44mod execution_guard;
45mod model;
46mod observer;
47mod scheduler;
48mod store;
49#[cfg(feature = "valkey-guard")]
50mod valkey_guard;
51#[cfg(feature = "valkey-store")]
52mod valkey_store;
53
54pub use error::{
55 ExecutionGuardError, ExecutionGuardErrorKind, InvalidJobError, InvalidJobKind,
56 SchedulerError, StoreError, StoreErrorKind, TaskJoinError, TaskJoinErrorKind,
57};
58pub use execution_guard::{
59 ExecutionGuard, ExecutionGuardAcquire, ExecutionGuardRenewal, ExecutionLease, ExecutionSlot,
60 NoopExecutionGuard,
61};
62pub use model::{
63 CronSchedule, Job, JobFuture, JobResult, JobState, MissedRunPolicy, OverlapPolicy, RunContext,
64 RunRecord, RunStatus, Schedule, SchedulerConfig, SchedulerReport, Task, TaskContext,
65 TerminalStatePolicy,
66};
67pub use observer::{
68 LogObserver, NoopObserver, SchedulerEvent, SchedulerObserver, SchedulerStopReason,
69 StateLoadSource,
70};
71pub use scheduler::{Scheduler, SchedulerHandle};
72pub use store::{
73 InMemoryStateStore, ResilientStateStore, ResilientStoreError, StateStore, StoreEvent,
74 StoreOperation,
75};
76#[cfg(feature = "valkey-guard")]
77pub use valkey_guard::{ValkeyExecutionGuard, ValkeyLeaseConfig};
78#[cfg(feature = "valkey-store")]
79pub use valkey_store::ValkeyStateStore;