Repository navigation
Fire an every at a wall-clock time in the record's own zone - #102
Merged
Merged
Conversation
C-Sinclair
force-pushed
the
feat/every-at-timezone
branch
from
October 22, 2026 11:27
b4cd6d4 to
7a49470
Compare
C-Sinclair
marked this pull request as ready for review
November 6, 2026 11:14
`every` measures `interval` from its last fire, so `interval {1, :days}`
drifts to whatever time the record entered the step. `at` names a local
time instead, `on` names the days, and `time_zone` places it:
every :daily_digest do
at ~T[09:00:00]
on [:mon, :tue, :wed, :thu, :fri]
time_zone :candidate_time_zone
action :send_digest
end
`time_zone` takes a literal, an attribute, or an expression calculation,
which is how a record reads its zone off a related one. A per-record zone
is the case a global cron cannot express, so the Oban trigger's `where`
clause reads the zone out of each row with an `AT TIME ZONE` fragment.
`AshWorkflow.Scheduler.Precise` asks when a record is next due instead,
and computes the same occurrence in Elixir through `AshWorkflow.WallClock`.
An `interval` declared alongside `at` is a stride: how many local days
must separate two fires, so `interval {14, :days}` with `on [:mon]` is
09:00 every second Monday. The stride counts local dates rather than
elapsed duration, since firing writes the last-fired column microseconds
after 09:00 and a duration comparison would slip the next fire a week.
A local time daylight saving skipped still fires that day, at the instant
the clock jumped to. One that happens twice fires on the first of the two.
A missed occurrence fires late rather than being skipped.
`AshWorkflow.Charts.Format.every/1` labels a wall-clock `every` by its schedule, and `AshWorkflow.Clarity.Timeline` lists it with the moments not timed from entry. `AshWorkflow.Scheduler.Work.deadline/0` gains the optional `wall_clock` key. `AshWorkflowTest.DataCase.set_field/3` shifts a zoned `DateTime` to UTC before writing it.
Resolves #98
Precise's wall-clock sweep and recheck now apply max_fires, which they previously ignored. The Oban trigger's AT TIME ZONE fragment skips a row whose zone Postgres does not know rather than failing the whole query. WallClock.time_zone_for/2 unwraps a ci_string zone attribute.
The Oban trigger now looks back eight local dates, so a single-day on whose last occurrence fell a week ago fires as soon as polling resumes, as Precise already does. The Every and WallClock docs now describe the stride's first fire and the daylight saving difference between Postgres and WallClock. ValidateEvery explains a too-short until on a stride.
A last fire from an earlier visit to the step made the record due as soon as it re-entered. Both schedulers and pending_deadlines now measure from whichever of the last fire and state_entered_at is later, as the fire count already does. The Oban fragment checks pg_timezone_names only for a zone read from the row, since a literal zone is checked at compile time. The topic doc shows the SQL the transformer now generates.
C-Sinclair
force-pushed
the
feat/every-at-timezone
branch
from
November 10, 2026 14:46
576fbdc to
6cb94bf
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolves #98.
at,onandtime_zonetoevery, so a recurring action can fire at a wall-clock time in each record's own zone instead of measuringintervalfrom its last fire.AshWorkflow.WallClock, which computes occurrences in Elixir forAshWorkflow.Scheduler.PreciseandAshWorkflow.Calculations.PendingDeadlines.AshWorkflow.Transformers.AddSchedulerasks the same question in SQL for the Oban trigger.AshWorkflow.Verifiers.ValidateEveryrules for the new options, and exempts a wall-clockeveryfromAshWorkflow.Verifiers.ValidateTimeoutPrecision.AshWorkflow.Charts.Format.every/1labels a wall-clockeveryas "every 14 days at 09:00 on mon in candidate_time_zone", so the Mermaid, DOT and JSON charts show its schedule.AshWorkflow.Clarity.Timelinelists it with the moments not timed from entry, since its first fire depends on the local time the record entered the step.Why the zone comes out of the row
interval {1, :days}fires a day after whatever time the record entered the step, and polling lag compounds the drift. A daily digest is not that. It goes out at 09:00 where the recipient is.A global cron can send every digest at one instant. It cannot send each record's digest at 09:00 in that record's own zone. Two records in the same step, one in
Australia/Sydneyand one inEurope/London, are due nine to eleven hours apart depending on the season.time_zonetakes three things:time_zone "Australia/Sydney"time_zone :candidate_time_zonecalculate :candidate_time_zone, :string, expr(candidate.time_zone)The calculation covers a zone living on a related record. There is no path syntax for relationships, because an expression calculation already is one and it composes.
ValidateEveryrejects a module calculation, the same check and the same reasoningAshWorkflow.Verifiers.ValidateTimeoutFieldsapplies to a timeout'sfield: the zone is read from the trigger's filter, which the data layer evaluates.Two readers, one schedule
flowchart TD DSL["every :daily_digest<br/>at / on / time_zone"] --> AS[AshWorkflow.Transformers.AddScheduler] AS -->|Work.match| OB["AshWorkflow.Scheduler.Oban<br/>is this record due now?"] AS -->|Work.deadline| PR["AshWorkflow.Scheduler.Precise<br/>when is this record next due?"] OB --> SQL["AT TIME ZONE fragment,<br/>evaluated per row"] PR --> WC[AshWorkflow.WallClock] PD[AshWorkflow.Calculations.PendingDeadlines] --> WCWork.matchbecomes a Postgres fragment, since the zone is per row, soatunderAshWorkflow.Scheduler.Obanneeds a SQL data layer.Work.deadlinecarries the schedule itself, andAshWorkflow.Scheduler.due_at/2resolves it throughWallClock.next_occurrence/3, soAshWorkflow.Scheduler.Preciseruns on any data layer.Timeline.recheck_filter/1andTimeline.due_now?/2are what keep it off the fragment.intervalalongsideatis a strideonsays which days carry an occurrence. It cannot say every other Monday, because it is a set of weekdays. Anintervalbeside anatsays that instead, and the two compose:A stride counts local dates rather than elapsed duration, and
ValidateEveryrejects any unit but:daysbeside anat. Both follow from one fact: firing at 09:00 writes the last-fired column microseconds after 09:00.last_fire + 14 daysDate.diff/2Daylight saving and missed occurrences
A local time daylight saving skipped still fires that day, at the instant the clock jumped to.
~T[02:30:00]on a day whose clocks go 02:00 to 03:00 fires at 03:00 local. Skipping the day instead would mean a digest missing one day a year, in whichever zones the rows happen to name. A local time that happens twice fires on the first of its two instants, and the second is then no longer after the last fire.A missed occurrence fires late rather than being skipped, on that day or a later one, and once however many were missed. The Oban fragment finds the most recent occurrence among the last seven local dates in the row's zone.
onis never empty, so the most recent occurrence is always one of those seven. A stride compares that occurrence's local date with the last fire's, not today's date.Why no
cronoptionA
cron "0 9 * * 1-5"option would add three thingsat,onand a stride cannot express: several times a day, minute-level patterns such as*/15 9-17 * * 1-5, and day-of-month rules such as0 9 1 * *. This PR leaves it out.atneeds only a seven-daygenerate_series. A general cron expression would need a cron evaluator in SQL or a scan at minute resolution.AshWorkflow.Scheduler.Precisecould compute the next cron run in Elixir, butathas to work under both schedulers.fire_at. If aneverystored its next fire instant after each run, cron,at, business days and calendar months would all reduce to computing the next instant in Elixir, with no per-row SQL. That design has its own costs: a record whosetime_zonechanges needs its column recomputed, and a schedule change in the DSL needs a backfill for records already in the step.Cron belongs with that stored-next-fire design, decided once alongside #79, rather than added to the fragment approach here.
What #79 will need
#79 adds business-day and calendar-month durations, and both issues ask for one
time_zoneoption. This PR putstime_zoneoneveryonly. Its value is a literal zone string, or an atom naming an attribute or an expression calculation. A business-daytimeoutcan take the same value unchanged.time_zoneontimeout, validated the same way asEvery.validate_time_zone/1. That function lives onAshWorkflow.Entities.Every, andAshWorkflow.WallClock.time_zone_for/2takes aWallClockstruct. Durations cannot express business days or calendar months #79 should move the validation and the record lookup to a module both entities share.calendar.ValidateEveryrequirestime_zonealongsideat. A default only relaxes that rule, so it is an additive change.ValidateEveryrejects a module calculation fortime_zone, because the Oban trigger reads the zone in SQL. Durations cannot express business days or calendar months #79 resolves a business-day deadline in Elixir when the record enters the step, so it does not need that restriction. Keeping the same rule on both entities keeps one meaning for the option.Notes for the reviewer
AshWorkflowTest.Postgres.RelatedZoneObanTestis the only place the calculation has to inline into SQL and join.everywrites itslast_fired_fieldon each fire, like anintervalone. It has noWork.fired_field, soAshWorkflow.Scheduler.Work.not_fired/1is always true for it, as for anyevery.AshWorkflow.Clarity.Diagram.step_timeline/2replaced every:with a space, because a Mermaid timeline splits on it. It now writes a colon between two digits as U+2236 RATIO, so "09:00" does not render as "09 00".atneeds an IANA database in the host application, sotzis added as a dev and test dependency andconfig/config.exssets:time_zone_database.ValidateEverychecks a literaltime_zoneagainst it at compile time.state_entered_atand the last-fired column with an explicitAT TIME ZONE 'UTC'before shifting into the row's zone. Both are stored as naive timestamps holding UTC, and leaving the conversion implicit would resolve against the sessionTimeZonesetting.mix spark.formatteraddedat,on,time_zoneand the previously missinglast_fired_fieldto.formatter.exs, which reformatted one line inAshWorkflowTest.EveryUntilWorkflow.no_index_workflows,own_index_workflowsand four index renames, none of which belongs here. Index names match their snapshots.