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 model;
45mod observer;
46mod scheduler;
47mod store;
48#[cfg(feature = "valkey-store")]
49mod valkey_store;
50
51pub use error::{
52 InvalidJobError, InvalidJobKind, SchedulerError, StoreError, StoreErrorKind, TaskJoinError,
53 TaskJoinErrorKind,
54};
55pub use model::{
56 CronSchedule, Job, JobFuture, JobResult, JobState, MissedRunPolicy, OverlapPolicy, RunContext,
57 RunRecord, RunStatus, Schedule, SchedulerConfig, SchedulerReport, Task, TaskContext,
58 TerminalStatePolicy,
59};
60pub use observer::{
61 LogObserver, NoopObserver, SchedulerEvent, SchedulerObserver, SchedulerStopReason,
62 StateLoadSource,
63};
64pub use scheduler::{Scheduler, SchedulerHandle};
65pub use store::{
66 InMemoryStateStore, ResilientStateStore, ResilientStoreError, StateStore, StoreEvent,
67 StoreOperation,
68};
69#[cfg(feature = "valkey-store")]
70pub use valkey_store::ValkeyStateStore;