Documentation
¶
Overview ¶
Package timers contains calendar expression parsing and monotonic scheduling.
The scheduler is internal to winunitd (DESIGN.md §17–§18). It does not proxy Task Scheduler. OnBootSec is relative to machine boot; OnStartupSec is relative to this winunitd instance (monotonic SinceStart). Clock.Timer / Fake.Advance drive waits without sleeping on the real clock (issue #28). Engine.ClockChanged recomputes OnCalendar / OnUnitActiveSec deadlines after a wall jump and wakes the loop; OnBootSec heap entries are not rewritten. Status reports the scheduled next (a.next), refreshed on Arm, fire, UnitActive, and ClockChanged — not a per-call calendar search. A 30s poll remains as fallback when the host does not deliver TIMECHANGE.
Index ¶
- Constants
- func MarkFired(spec Spec, rt *Runtime, clk Clock, scheduled, actual time.Time)
- func NextDeadline(spec Spec, rt Runtime, clk Clock) (time.Time, bool)
- type Activation
- type Calendar
- type Clock
- type Engine
- func (e *Engine) Arm(spec Spec) uint64
- func (e *Engine) Armed(name string) bool
- func (e *Engine) Clock() Clock
- func (e *Engine) ClockChanged()
- func (e *Engine) Current(name string, token uint64) bool
- func (e *Engine) Disarm(name string)
- func (e *Engine) RecordResult(event Fire, success bool)
- func (e *Engine) Retain(keep map[string]bool)
- func (e *Engine) Retry(event Fire)
- func (e *Engine) Status(name string) Snapshot
- func (e *Engine) Stop()
- func (e *Engine) UnitActive(unit string, when time.Time)
- type Fake
- func (f *Fake) Advance(d time.Duration)
- func (f *Fake) Clock() Clock
- func (f *Fake) JumpWall(to time.Time)
- func (f *Fake) NextWhen() (time.Time, bool)
- func (f *Fake) Now() time.Time
- func (f *Fake) SinceBoot() time.Duration
- func (f *Fake) SinceStart() time.Duration
- func (f *Fake) Suspend(d time.Duration)
- func (f *Fake) Waiting() bool
- func (f *Fake) WaitingAt(d time.Duration) bool
- type Fire
- type FireFunc
- type Runtime
- type Snapshot
- type Spec
- type Store
- type Timer
Constants ¶
const MaxArmedTimers = 1024
const MaxCalendarExpressions = 64
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Activation ¶
type Activation struct {
ID string `json:"id"`
Unit string `json:"unit"`
Result string `json:"result"`
Scheduled time.Time `json:"scheduled"`
Actual time.Time `json:"actual"`
}
Activation is a persistent calendar delivery attempt. A recovered pending record retains its ID; success/failure describes activation, not workload exit.
type Calendar ¶
type Calendar struct {
Raw string
// RestrictWeekdays is true when the expression names weekdays
// (for example Mon..Fri). When false, every weekday matches.
RestrictWeekdays bool
Weekdays [7]bool // indexed by time.Weekday
// Year, Month, Day use -1 as wildcard (*).
Year int
Month int
Day int
Hour int
Minute int
Second int
// contains filtered or unexported fields
}
Calendar is a parsed OnCalendar= expression.
MVP forms: "daily", "Mon..Fri 03:00", and "*-*-* HH:MM:SS" (seconds optional). Weekday names may be abbreviated or full.
func ParseCalendar ¶
ParseCalendar parses an OnCalendar= value.
func (Calendar) AllowsWeekday ¶
AllowsWeekday reports whether d is included in the expression.
type Clock ¶
type Clock struct {
Now func() time.Time
SinceBoot func() time.Duration
SinceStart func() time.Duration
Startup time.Time
NewTimer func(d time.Duration) Timer
Changed <-chan struct{}
}
Clock is the scheduler's view of wall time, machine boot, and this winunitd instance start (DESIGN.md §17–§18).
OnBootSec is measured from machine boot. OnStartupSec is measured from this instance (Clock.SinceStart). They differ after an SCM restart of winunitd.
NewTimer, if set, is used instead of time.NewTimer so a fake can Advance time. Changed, if set, is signaled on a discontinuous wall jump (manual clock, DST, resume) so the engine recalculates calendar deadlines instead of sleeping until a stale monotonic wait. The host also calls Engine.ClockChanged on SERVICE_CONTROL_TIMECHANGE / POWEREVENT (PBT_APMRESUMEAUTOMATIC). A 30s poll remains as fallback.
SinceStart, if set, is monotonic time since this winunitd instance started (OnStartupSec). Nil means now.Sub(Startup) (wall).
func DefaultClock ¶
func DefaultClock() Clock
DefaultClock uses the wall clock, platform boot time, and "now" as the instance start.
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine is an internal timer scheduler (not Task Scheduler).
func (*Engine) Arm ¶
Arm starts or refreshes a timer. Existing FiredBoot/FiredStartup and last unit-active time are kept so refresh does not re-fire one-shot relatives. Each arm receives a new callback identity, returned to the caller.
func (*Engine) ClockChanged ¶
func (e *Engine) ClockChanged()
ClockChanged recomputes wall-clock (OnCalendar / OnUnitActiveSec) deadlines and wakes the loop so missed calendar events fire promptly (DESIGN.md §18). Monotonic OnBootSec / OnStartupSec heap entries are not rewritten. The 30s poll remains as a fallback.
func (*Engine) RecordResult ¶
RecordResult stores last successful execution after the activated unit start attempt.
func (*Engine) Retry ¶
Retry retains an activation rejected before manager admission. Persistent calendar retries keep their durable intent identity; other retries retain their in-memory arm identity. Neither records a successful execution.
func (*Engine) Status ¶
Status copies accepted next/last state without performing calendar searches. Next is absent while storage or calendar planning is pending, and otherwise matches the indexed heap deadline.
type Fake ¶
type Fake struct {
// contains filtered or unexported fields
}
Fake is a test clock. Advance moves wall time and SinceBoot together and fires due NewTimer waits. JumpWall changes only wall time. Suspend models Windows resume: uptime includes sleep, so both clocks and due waits advance. Both discontinuities signal Clock.Changed for wall deadline reconciliation.
func (*Fake) Advance ¶
Advance moves wall time and SinceBoot forward by d and fires every NewTimer whose deadline is at or before the new now.
func (*Fake) Clock ¶
Clock is a Clock bound to this Fake. Startup is the Fake's start instant and does not move with Advance.
func (*Fake) JumpWall ¶
JumpWall sets wall time without moving SinceBoot and signals Changed. Pending NewTimer waits are not fired (those are monotonic).
func (*Fake) SinceStart ¶
SinceStart is monotonic time since this Fake was created. Advance and Suspend move it; JumpWall does not.
func (*Fake) Suspend ¶
Suspend advances wall time and uptime together, as Windows does across sleep and hibernation. Due waits become observable on resume, followed by Changed.
type Fire ¶
type Fire struct {
ActivationID string
Name string
Unit string
Token uint64 // identity of the arm that produced this event
}
Fire captures the source arm and target when a deadline is consumed.
type Runtime ¶
type Runtime struct {
Activation Activation
LastScheduled time.Time
LastActual time.Time
LastSuccess time.Time
LastUnitActive time.Time
FiredBoot bool
FiredStartup bool
}
Runtime is per-activation and persistent timer state (DESIGN.md §17).
type Snapshot ¶
type Snapshot struct {
ScheduleState string
Activation Activation
StorageState string
StorageError string
ConfigRevision string
Unit string
Next time.Time
Last time.Time
}
Snapshot is next/last for status and list-timers.
type Spec ¶
type Spec struct {
ConfigRevision string // opaque identity supplied by the configuration owner
Name string
Unit string // activated unit; empty means same basename .service
OnBootSec time.Duration
OnStartupSec time.Duration
OnUnitActiveSec time.Duration
OnCalendar []Calendar
Persistent bool
OnBootSecSet bool
OnStartupSecSet bool
OnUnitActiveSecSet bool
}
Spec is one timer unit's schedule (DESIGN.md §17, §74).
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store persists last scheduled/actual/successful execution under <dir>/<name>.json (DESIGN.md §17, §32).
func (*Store) Close ¶
Close releases resources owned by the store. Load and Save open each file for one operation, so there is no retained handle to join.
func (*Store) LoadChecked ¶
LoadChecked distinguishes missing state from corrupt or unreadable state. Legacy unversioned timestamps are accepted and upgraded by the next save.