trigger
ModuleContents
4 submodules, and what the module defines itself.
use utopia_schedule::Trigger;pub trait Trigger: Send + SyncWhen 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>.
trigger::at
Own pageuse utopia_schedule::At;pub struct AtA 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.
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 pageuse utopia_schedule::Cron;pub struct CronA 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:
| Field | Syntax | Meaning |
|---|---|---|
| either | ? | no specific value, same as * |
| day of month | L | last day of the month |
| day of month | L-3 | three days before the last day |
| day of month | LW | last weekday of the month |
| day of month | 15W | weekday nearest the 15th, never crossing the month |
| day of week | 5L | last Friday of the month |
| day of week | FRI#3 | third Friday of the month |
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 pageuse utopia_schedule::Interval;pub struct IntervalA 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).
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 pageuse utopia_schedule::Shifted;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.
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"]);