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//! - [`JobTimeWindow`] can restrict execution by local weekday and time
15//! segments; outside-window occurrences are skipped with
16//! [`RunSkipReason::OutsideTimeWindow`].
17//! - [`Job::with_max_runs`] applies to every schedule kind; `0` exits without
18//! running.
19//! - [`SchedulerConfig::timezone`] is forwarded through [`RunContext`], drives
20//! [`Schedule::Cron`] evaluation, and does not rewrite absolute
21//! [`Schedule::AtTimes`] values.
22//! - Restarts resume by `job_id` from the saved [`JobState::next_run_at`].
23//! - [`SchedulerHandle::pause`] pauses future scheduling without interrupting
24//! the current run. [`SchedulerHandle::resume`] recomputes immediately and
25//! applies the existing missed-run policy.
26//! - Pause scope is backend-specific: legacy schedulers pause locally, while
27//! coordinated schedulers persist a shared pause state per `job_id`.
28//! - Dependency injection here means passing an explicit `deps` value when the
29//! job is constructed. The scheduler does not auto-resolve parameters.
30//!
31//! ```rust
32//! use std::time::Duration;
33//!
34//! use scheduler::{InMemoryStateStore, Job, Schedule, Scheduler, SchedulerConfig, Task};
35//!
36//! let runtime = tokio::runtime::Runtime::new().unwrap();
37//! runtime.block_on(async {
38//! let scheduler = Scheduler::new(SchedulerConfig::default(), InMemoryStateStore::new());
39//! let job = Job::without_deps(
40//! "doc-simple",
41//! Schedule::Interval(Duration::from_millis(1)),
42//! Task::from_async(|_| async { Ok(()) }),
43//! )
44//! .with_max_runs(1);
45//!
46//! let report = scheduler.run(job).await.unwrap();
47//! assert_eq!(report.history.len(), 1);
48//! });
49//! ```
50
51mod coordinated_store;
52mod error;
53mod execution_guard;
54mod guarded_runner;
55mod model;
56mod observer;
57mod scheduler;
58mod store;
59#[cfg(feature = "valkey-store")]
60mod valkey_coordinated_store;
61#[cfg(any(feature = "valkey-guard", feature = "valkey-store"))]
62mod valkey_execution_support;
63#[cfg(feature = "valkey-guard")]
64mod valkey_guard;
65#[cfg(feature = "valkey-store")]
66mod valkey_store;
67
68pub use coordinated_store::{
69 CoordinatedClaim, CoordinatedLeaseConfig, CoordinatedPendingTrigger, CoordinatedRuntimeState,
70 CoordinatedStateStore, NoopCoordinatedStateStore,
71};
72pub use error::{
73 ExecutionGuardError, ExecutionGuardErrorKind, InvalidJobError, InvalidJobKind, SchedulerError,
74 StoreError, StoreErrorKind, TaskJoinError, TaskJoinErrorKind,
75};
76pub use execution_guard::{
77 ExecutionGuard, ExecutionGuardAcquire, ExecutionGuardRenewal, ExecutionGuardScope,
78 ExecutionLease, ExecutionSlot, NoopExecutionGuard,
79};
80pub use guarded_runner::{GuardedRunResult, GuardedRunner};
81pub use model::{
82 CronSchedule, Job, JobFuture, JobResult, JobState, JobTimeWindow, MissedRunPolicy,
83 OverlapPolicy, RunContext, RunRecord, RunSkipReason, RunStatus, Schedule, SchedulerConfig,
84 SchedulerReport, Task, TaskContext, TerminalStatePolicy, TimeWindowSegment,
85};
86pub use observer::{
87 LogObserver, NoopObserver, PauseScope, SchedulerEvent, SchedulerObserver, SchedulerStopReason,
88 StateLoadSource,
89};
90pub use scheduler::{Scheduler, SchedulerHandle};
91pub use store::{
92 InMemoryStateStore, ResilientStateStore, ResilientStoreError, StateStore, StoreEvent,
93 StoreOperation,
94};
95#[cfg(feature = "valkey-store")]
96pub use valkey_coordinated_store::ValkeyCoordinatedStateStore;
97#[cfg(feature = "valkey-guard")]
98pub use valkey_guard::{ValkeyExecutionGuard, ValkeyLeaseConfig};
99#[cfg(feature = "valkey-store")]
100pub use valkey_store::ValkeyStateStore;