cardinal

The simulated clock

Why seeded records carry past dates, and what reads the clock.

A Seed Run writes two years of history in an afternoon. Without a clock, every record would be stamped with that afternoon: a list sorted by Last Updated would show one day, and 2024's invoices would be numbered under 2026.

Cardinal gives each event a simulated instant instead. A Sales Invoice seeded for 14 May 2024 at 11:20 has creation and modified of that instant, a posting date of 14 May 2024, and a name from that year's series.

How a run moves through time

The run queues one job for its first simulated day.
The job plans the day's events for each domain, from the company's position at 00:00 that day.
Each event runs under the clock set to its instant, as the seeded user who would do it, and commits with the run's cursor.
At 23:00 site time, the run executes the installed apps' scheduler entries under that day's clock.
The job queues the next day, until the run reaches its To date.

The day's tick follows the live scheduler's calendar: all, hourly and daily entries every day, weekly ones on Mondays, monthly ones on the 1st and yearly ones on 1 January. This is what expires quotations, marks invoices Overdue, turns checkins into Attendance and depreciates assets in the seeded history. A failing entry is logged on the run's Scheduler Errors and the run goes on.

What reads the clock

Uses the simulated instantKeeps the real time
creation and modified on insert, save, submit and cancelJob scheduling, retries and backoff
Versions and Comments the save writesSessions and auth tokens
Naming series parts .YYYY., .YY., .MM., .DD.Rate limits
Today defaults, today() in expressions, Today filtersThe scheduler's own ticks
Jobs enqueued during the event, which carry the clock in their argsPlatform tables written outside the record service

Who can set it

Only system code can set the clock, through Cardinal.DB.at/2 on a context from Cardinal.DB.as_system/1. Any other context raises, so no request and no job a user enqueued can backdate a record.

ctx = ctx |> Cardinal.DB.as_system() |> Cardinal.DB.at(~U[2024-05-14 05:50:00Z])

The clock is a value on the context and in a job's arguments. No node holds it, so a job retried on another node runs under the same instant.

On this page