Rust

scheduler

Module

The loop: reconcile schedules from a Source, select due occurrences over tiled windows, hand them over, and commit coverage to a Store.

Contents

use utopia_schedule::Options;
Rust
pub struct Options

Undocumented.

Also implements Default.
use utopia_schedule::Scheduler;
Rust
pub struct Scheduler<S: Source, St: Store = MemoryStore, C: Clock = SystemClock>

A running loop with two duties: reconcile schedules into memory from a source of truth, and dispatch their occurrences at the right time.

The correctness rules it enforces:

  • Selection happens over an explicit half-open window, never against a moving "now": a minute boundary the loop crosses mid-evaluation cannot drop the occurrence sitting on it.
  • Windows tile: each opens where the previous committed window closed (the watermark, part of the Claim held in the Store). Nothing falls between ticks, and with shared state nothing falls across a restart either.
  • Delivery is at-least-once: Scheduler::commit advances the watermark only after the caller has handled a tick's occurrences.
  • Catch-up is bounded by recover_seconds.
  • Coverage extends to late discoveries: an entry that appears or changes between syncs is covered once from the moment it took effect (Row::active_from), so a one-shot due sooner than the sync lag runs late instead of never.
  • The watermark never rewinds: a clock stepping backwards produces empty windows.
  • Reconciliation is level-based: a full snapshot diff converges additions, updates and removals, including hard deletes.
  • Leadership and the watermark share one claim: replicas sharing a Store elect one dispatcher, a standby takes over when the claim expires, and a deposed leader's late commit is fenced. Failover trades duplicates, never losses; Occurrence::key lets consumers drop them.

One instance is one loop: reconciliation, selection and the commit all run on the caller's task, so the schedule map needs no locking.

Rust
use std::sync::Arc;

use php_std::datetime::{DateTime, Timestamp, TzInfo};
use utopia_schedule::{BoxError, Cron, Entry, MemoryStore, Options, Row, Scheduler, Source, TestClock};

/// Every row is a minutely cron.
struct Minutely(Vec<&'static str>);

impl Source for Minutely {
    type Data = ();
    type Payload = ();

    async fn snapshot(&self) -> Result<Vec<Row<()>>, BoxError> {
        Ok(self.0.iter().map(|id| Row::new(*id, "v1", ())).collect())
    }

    async fn make(&self, _: &Row<()>) -> Result<Entry<()>, BoxError> {
        Ok(Entry::new(Cron::new("* * * * *")?, ()))
    }
}

let start = DateTime::parse(b"2026-08-18 03:00:30", None, &TzInfo::utc(), Timestamp { sec: 0, usec: 0 }).unwrap();
let clock = Arc::new(TestClock::new(&start));
let mut scheduler = Scheduler::new(Minutely(vec!["fn"]), MemoryStore::new(), clock.clone(), Options::default())?;

scheduler.reconcile(false).await?;
scheduler.tick().await?;
scheduler.commit().await?;

clock.advance(90.0);
let due = scheduler.tick().await?;
assert_eq!(due[0].key(), "fn@1787022060.000000");
scheduler.commit().await?;
use utopia_schedule::Stop;
Rust
pub struct Stop(Arc<AtomicBool>)

Makes a running Scheduler::run return after the current tick, from anywhere (a handler, a signal, another task).