Schedule by Timer
This optional module implements systemd-style OnCalendar expressions. Importing it also imports Metronome's embedded IANA timezone database; an application importing only metronome does not include either feature.
The calendar shape is::
[DayOfWeek] Year-Month-Day Hour:Minute:Second[.Microseconds] [TimeZone]
For example::
import metronome
import metronome/timers
scheduler nightly:
timer(
onCalendar="*-*-* 02:00:00 Europe/Amsterdam",
async=true
):
echo "Running at 02:00 in Amsterdam"
Calendar deadlines are calculated to one microsecond. Metronome uses a millisecond event-loop wait, however, so dispatch is best-effort and is not a real-time guarantee. Positive sub-millisecond waits recheck the clock before dispatch. Nonexistent local times are skipped at DST gaps; ambiguous local times select the earlier occurrence.
Calendar fields accept *, lists separated by commas, ascending .. ranges, and / repetitions with an explicit start value. An optional English weekday prefix is combined with the date using AND semantics. The date also accepts systemd's ~ last-day form. Supported shorthands are minutely, hourly, daily, monthly, weekly, yearly, annually, quarterly, and semiannually.
The timezone suffix may be UTC or an exact name from Metronome's embedded IANA catalog. If omitted, the timezone of the DateTime passed to getNext is used. The next result is returned in that caller timezone.
This module intentionally does not parse complete systemd .timer units and does not implement AccuracySec, persistent catch-up, randomized delay, monotonic boot timers, or wake-from-suspend.
Types
CalendarTimer = object
Procs
proc getNext(timer: CalendarTimer; current: DateTime): Option[DateTime] {. ...gcsafe, raises: [], tags: [], forbids: [].}
-
Return the first matching instant strictly later than current.
When multiple expressions match the same instant, it is returned once. Retains microseconds and returns in current's timezone; no match returns none(DateTime). This evaluates the calendar without creating a job or starting an execution loop.
proc initTimerBeater(timer: CalendarTimer; asyncProc: BeaterAsyncProc; startTime: Option[DateTime] = none(DateTime); endTime: Option[DateTime] = none(DateTime); id: string = ""; throttleNum: int = 1; errorHandler: JobErrorHandler = nil): Beater {. ...raises: [ValueError], tags: [TimeEffect], forbids: [].}
- Initialize an async beater driven by a parsed calendar timer. Retains the timer through a custom-schedule callback. A future startTime can match exactly; endTime is inclusive. Timer evaluation retains microseconds and its input timezone. Bounds, throttling, lifecycle, and error handling follow the core custom-beater API. Calendar targets have no interval jitter. fireTime previews a candidate; nextRun reports the pending deadline once execution has planned it.
proc initTimerBeater(timer: CalendarTimer; threadProc: BeaterThreadProc; startTime: Option[DateTime] = none(DateTime); endTime: Option[DateTime] = none(DateTime); id: string = ""; throttleNum: int = 1; errorHandler: JobErrorHandler = nil): Beater {. ...raises: [ValueError], tags: [TimeEffect], forbids: [].}
- Initialize a thread-backed beater driven by a parsed calendar timer. Uses the same retained timer and calculation rules as the async overload. The job must satisfy Nim's thread/GC-safety rules; a non-nil errorHandler is rejected, as for other thread-backed beaters.
proc newTimer(onCalendar: openArray[string]): CalendarTimer {. ...raises: [ValueError, Exception], tags: [RootEffect], forbids: [].}
- Parse repeated OnCalendar expressions into one timer.
proc newTimer(onCalendar: string): CalendarTimer {. ...raises: [ValueError, Exception], tags: [RootEffect], forbids: [].}
- Parse one systemd-style OnCalendar expression.