108. Loop Engineering 2 - Scheduling: Cron, Timers and Triggers
Choose the right trigger for a loop - a clock, an event or a person - and know what a cron expression really promises.
By Jacques Botte, founder of Toptronic®. Last updated 17 October 2026.
The lesson
A schedule is a promise about WHEN, and the first design question is whether the work is genuinely time-shaped. Backups, digest emails, nightly evaluations and certificate renewals are time-shaped: they must happen whether or not anything else did. Most other work is event-shaped, and putting event-shaped work on a clock means it runs when there is nothing to do and waits when there is something urgent.
Cron is the oldest and still the clearest notation: five fields for minute, hour, day of month, month and day of week. A star means every value, a list means these values, a range means these, and a step means every nth. The two traps are worth memorising. Day-of-month and day-of-week are OR-ed together when both are restricted, not AND-ed, which surprises almost everyone the first time. And the machine's timezone is whatever the machine believes, so a job that must run at a local midnight needs the timezone stated in the job, not assumed.
A timer inside a program is a different animal. It fires while the program is running and stops when the program stops, which is exactly right for polling a device or flushing a buffer, and exactly wrong for anything that must survive a reboot. A scheduler outside the program - cron, a Windows scheduled task, a container scheduler, an edge runtime's own cron trigger - survives because it is somebody else's process.
Event triggers are the ones people forget to bound. A file watcher fires once per write, and an editor that saves on every keystroke fires hundreds of times for one document. A queue consumer fires per message and will happily process the same message twice after a visibility timeout. Every event trigger therefore needs an idempotent body: doing the work twice must leave the same result as doing it once.
The practical rule for this course: name the trigger in the loop's own description, and state what happens if it fires twice, what happens if it does not fire at all, and where the missed run is noticed. A schedule nobody watches is not reliability, it is hope with a timestamp - and the first quiet failure is the one that costs a week.
Check yourself
Question 1: In a five-field cron expression, how are day-of-month and day-of-week combined when both are restricted?
- They are AND-ed: both must match
- They are OR-ed: either may match — correct
- Day-of-week is ignored
- The job runs only in February
Answer: They are OR-ed: either may match
This is the classic cron trap: restricting both fields makes the job run on either condition, not both. Restrict one and use a star for the other if you mean AND.
Question 2: Which trigger is right for a job that must run even if the machine reboots?
- A timer inside the program
- A scheduler outside the program, such as cron or a Windows scheduled task — correct
- A file watcher
- A button in the interface
Answer: A scheduler outside the program, such as cron or a Windows scheduled task
An in-process timer dies with the process. Only a scheduler that is somebody else's process survives a reboot.
Question 3: Why does every event-triggered loop need an idempotent body?
- Because events are always delivered exactly once
- Because an event can fire twice - a watcher per keystroke, a queue after a timeout - and the result must be the same — correct
- Because idempotence makes the loop faster
- Because the check needs it
Answer: Because an event can fire twice - a watcher per keystroke, a queue after a timeout - and the result must be the same
Delivery is at-least-once in practice. Doing the work twice must leave the same state as doing it once.
← Previous lesson · All 134 lessons · Next lesson →
The full course — 134 lessons and 402 quiz questions — ships inside the app. Get TPEE to study it offline.
This lesson is the written summary of a course that ships inside the TPEE prompt editor for Windows - thirteen sections, a 0-100 score before you send anything, and it works with no network connection. The three one-time prices, and a 14-day free trial.