Rust

trigger

Module

When a schedule runs: Cron, Interval, At, and Shifted, which moves another trigger's occurrences by a fixed offset.

Contents

4 submodules, and what the module defines itself.

use utopia_schedule::Trigger;
Rust
pub trait Trigger: Send + Sync

When a schedule should run, expressed as pure occurrence math.

A trigger never consults the current time: callers hand it an explicit half-open window and it answers which occurrences fall inside. That is what makes a scheduler immune to evaluation-time races: an occurrence belongs to exactly one window no matter how long the caller's loop has been running when the trigger is evaluated.

Implement it to add a kind of schedule; the scheduler takes any trigger as an Arc<dyn Trigger>.

Implemented byAtCronIntervalArcBoxShifted

trigger::at

Own page
use utopia_schedule::At;
Rust
pub struct At

A one-shot trigger: a single occurrence at a fixed moment.

The moment is absolute from construction onward, so "in 30 seconds" captured through At::after means 30 seconds from when it was scheduled, not from whenever the scheduler happens to evaluate it. The scheduler retires it once the occurrence is delivered.

Rust
use php_std::datetime::{DateTime, Timestamp, TzInfo};
use utopia_schedule::{At, Trigger};

let at = |s: &str| DateTime::parse(s.as_bytes(), None, &TzInfo::utc(), Timestamp { sec: 0, usec: 0 }).unwrap();
let delayed = At::after(30, &at("2026-08-17 16:00:00"));

let due = delayed.occurrences_between(&at("2026-08-17 16:00:29"), &at("2026-08-17 16:00:31"));
assert_eq!(due[0].format(b"H:i:s"), b"16:00:30");
assert!(!delayed.recurring());

trigger::cron

Own page
use utopia_schedule::Cron;
Rust
pub struct Cron

A recurring trigger described by a five-field cron expression: minute, hour, day of month, month, day of week.

The portable cron core is supported: *, values, ranges (a-b), steps (every n-th value over * or a range, a-b/n), lists (,), month and day names (JAN, FRI), 7 as Sunday, and the @hourly … @yearly macros. Following vixie cron, a day matches when either day field hits if both day-of-month and day-of-week are restricted, and both otherwise.

The Quartz extensions to the two day fields are supported too, also inside a list alongside plain values:

FieldSyntaxMeaning
either?no specific value, same as *
day of monthLlast day of the month
day of monthL-3three days before the last day
day of monthLWlast weekday of the month
day of month15Wweekday nearest the 15th, never crossing the month
day of week5Llast Friday of the month
day of weekFRI#3third Friday of the month
Rust
use php_std::datetime::{DateTime, Timestamp, TzInfo};
use utopia_schedule::{Cron, Trigger};

let at = |s: &str| DateTime::parse(s.as_bytes(), None, &TzInfo::utc(), Timestamp { sec: 0, usec: 0 }).unwrap();
let cron = Cron::new("15,45-50/2 8 * * *")?;

let due = cron.occurrences_between(&at("2026-08-17 00:00:00"), &at("2026-08-18 00:00:00"));
let due: Vec<_> = due.iter().map(|d| String::from_utf8(d.format(b"H:i")).unwrap()).collect();
assert_eq!(due, ["08:15", "08:45", "08:47", "08:49"]);

assert_eq!(Cron::new("0 0 31 2 *").unwrap_err().to_string(), "Cron expression \"0 0 31 2 *\" never matches a date");

trigger::interval

Own page
use utopia_schedule::Interval;
Rust
pub struct Interval

A recurring trigger that fires every fixed number of seconds.

Occurrences sit on a deterministic grid, anchor + k × seconds, so the cadence survives restarts instead of re-phasing to whenever the process happened to boot. The anchor defaults to the Unix epoch; pass one to phase the grid (for example to a resource's creation time).

Rust
use php_std::datetime::{DateTime, Timestamp, TzInfo};
use utopia_schedule::{Interval, Trigger};

let at = |s: &str| DateTime::parse(s.as_bytes(), None, &TzInfo::utc(), Timestamp { sec: 0, usec: 0 }).unwrap();
let quarterly = Interval::new(900, None)?;

let due = quarterly.occurrences_between(&at("2026-08-17 16:07:00"), &at("2026-08-17 16:35:00"));
let due: Vec<_> = due.iter().map(|d| String::from_utf8(d.format(b"H:i:s")).unwrap()).collect();
assert_eq!(due, ["16:15:00", "16:30:00"]);

trigger::shifted

Own page
use utopia_schedule::Shifted;
Rust
pub struct Shifted<T>

Another trigger, every occurrence moved by a fixed number of seconds.

A fleet of schedules sharing an expression shares its due second, so * * * * * across three thousand rows is three thousand dispatches at :00 and nothing for the rest of the minute. Give each row an offset derived from its id and the burst spreads across the minute while every row keeps its slot across deploys.

The shift belongs to the schedule rather than to delivery, so an occurrence's due is the moment it runs: the window that covers it covers the shifted time and the watermark commits the shifted time. The window is shifted back and the results forward, so occurrences still land in exactly one window.

Rust
use php_std::datetime::{DateTime, Timestamp, TzInfo};
use utopia_schedule::{Cron, Shifted, Trigger};

let at = |s: &str| DateTime::parse(s.as_bytes(), None, &TzInfo::utc(), Timestamp { sec: 0, usec: 0 }).unwrap();
let spread = Shifted::new(Cron::new("*/15 * * * *")?, 40);

let due = spread.occurrences_between(&at("2026-08-18 03:00:00"), &at("2026-08-18 03:16:00"));
let due: Vec<_> = due.iter().map(|d| String::from_utf8(d.format(b"H:i:s")).unwrap()).collect();
assert_eq!(due, ["03:00:40", "03:15:40"]);