Documentation
¶
Overview ¶
Package cron is the schedule grammar: human-readable trigger declarations, their canonical five-field form, and the pure value model of triggers and occurrences that the scheduler, its store and the Liquid Proto contract share. It starts no goroutines and crosses no boundary; the service that fires triggers and records occurrences is candace/services/cron.
Index ¶
- Constants
- Variables
- func NormalizeTriggerDefinition(definition TriggerDefinition, now time.Time) (TriggerDefinition, Schedule, error)
- func OccurrenceID(triggerName string, scheduledAt time.Time) string
- func ValidateTriggerName(name string) error
- type CatchUpPolicy
- type MeridiemTime
- type OccurrenceRecord
- type OccurrenceStatus
- type OverlapPolicy
- type Rule
- type Schedule
- func (schedule Schedule) Anchor(anchor time.Time) Schedule
- func (schedule Schedule) Canonical() (string, error)
- func (schedule Schedule) Definition() (ScheduleDefinition, error)
- func (schedule Schedule) In(location *time.Location) Schedule
- func (schedule Schedule) IntervalAnchor() (time.Time, bool)
- func (schedule Schedule) Location() *time.Location
- func (schedule Schedule) Next(after time.Time) (time.Time, error)
- func (schedule Schedule) String() string
- func (schedule Schedule) Validate() error
- type ScheduleDefinition
- type ScheduleKind
- type StoreSnapshot
- type TimeOfDay
- type TriggerDefinition
- type TriggerState
Constants ¶
const ( // MaxTriggerNameBytes bounds a trigger name, as the store's check // constraint does. MaxTriggerNameBytes = 128 )
Variables ¶
var ErrInvalidTrigger = errors.New("cron: invalid trigger")
ErrInvalidTrigger reports a trigger declaration the grammar rejects: its name, one of its policies or its schedule definition.
Functions ¶
func NormalizeTriggerDefinition ¶ added in v0.2.0
func NormalizeTriggerDefinition(definition TriggerDefinition, now time.Time) (TriggerDefinition, Schedule, error)
NormalizeTriggerDefinition validates a declaration and returns it in its persisted form together with its schedule. An interval schedule that states no anchor is anchored at now, so its cadence survives a restart; an anchor is kept at PostgreSQL's microsecond precision.
func OccurrenceID ¶
OccurrenceID is the stable identity of one trigger's scheduled instant, and so the idempotency key of the operation that runs it.
func ValidateTriggerName ¶ added in v0.2.0
ValidateTriggerName accepts the names the store's check constraint accepts: one to MaxTriggerNameBytes bytes of lower-case ASCII letters, digits, dots, underscores, slashes and dashes, starting with a letter.
Types ¶
type CatchUpPolicy ¶
type CatchUpPolicy string
CatchUpPolicy says what a trigger does with the occurrences it missed while its scheduler was not running.
const ( // CatchUpNone advances past every missed occurrence without invoking it, // as traditional cron does. It is the default. CatchUpNone CatchUpPolicy = "none" // CatchUpLatest invokes only the most recent missed occurrence. CatchUpLatest CatchUpPolicy = "latest" // CatchUpAll invokes every missed occurrence, up to the scheduler's // catch-up limit; the overlap policy still applies while they run. CatchUpAll CatchUpPolicy = "all" )
func (CatchUpPolicy) Valid ¶ added in v0.2.0
func (policy CatchUpPolicy) Valid() bool
Valid reports whether the policy is one the grammar defines.
type MeridiemTime ¶
type MeridiemTime struct {
// contains filtered or unexported fields
}
MeridiemTime is deliberately distinct from TimeOfDay. It can only become a schedule time after AM or PM is selected, preventing Daily(At(3)).
func At ¶
func At(hour int, minute ...int) MeridiemTime
At starts a 12-hour clock declaration. It accepts an optional minute.
func (MeridiemTime) AM ¶
func (value MeridiemTime) AM() TimeOfDay
AM completes a 12-hour declaration.
func (MeridiemTime) PM ¶
func (value MeridiemTime) PM() TimeOfDay
PM completes a 12-hour declaration.
type OccurrenceRecord ¶
type OccurrenceRecord struct {
ID string `json:"id"`
TriggerName string `json:"trigger_name"`
ScheduledAt time.Time `json:"scheduled_at"`
Status OccurrenceStatus `json:"status"`
Attempt uint32 `json:"attempt"`
StartedAt time.Time `json:"started_at,omitempty"`
FinishedAt time.Time `json:"finished_at,omitempty"`
LeaseOwner string `json:"lease_owner,omitempty"`
LeaseToken string `json:"-"`
LeaseUntil time.Time `json:"lease_until,omitempty"`
Error string `json:"error,omitempty"`
SkipReason string `json:"skip_reason,omitempty"`
LastModified time.Time `json:"last_modified"`
}
OccurrenceRecord is the durable execution record of one trigger's scheduled instant. LeaseToken is the fencing secret the store checks and is deliberately omitted from JSON snapshots.
type OccurrenceStatus ¶
type OccurrenceStatus string
OccurrenceStatus is the durable state of one occurrence: running under a lease, or one of the four terminal states.
const ( OccurrenceRunning OccurrenceStatus = "running" OccurrenceSucceeded OccurrenceStatus = "succeeded" OccurrenceFailed OccurrenceStatus = "failed" OccurrenceCanceled OccurrenceStatus = "canceled" OccurrenceSkipped OccurrenceStatus = "skipped" )
func (OccurrenceStatus) Completion ¶ added in v0.2.0
func (status OccurrenceStatus) Completion() bool
Completion reports whether the status is one an operation's completion records: succeeded, failed or canceled. A skipped occurrence never ran.
type OverlapPolicy ¶
type OverlapPolicy string
OverlapPolicy says whether two occurrences of one trigger may run at the same time. The store enforces it, so it holds across processes sharing one database, not only within one.
const ( // OverlapSkip records a skipped occurrence while another occurrence of // the trigger holds a live lease. It is the default. OverlapSkip OverlapPolicy = "skip" // OverlapAllow lets occurrences of one trigger run concurrently. OverlapAllow OverlapPolicy = "allow" )
func (OverlapPolicy) Valid ¶ added in v0.2.0
func (policy OverlapPolicy) Valid() bool
Valid reports whether the policy is one the grammar defines.
type Rule ¶
type Rule struct {
// contains filtered or unexported fields
}
Rule is an opaque schedule declaration constructed by the fluent helpers. It intentionally has no exported fields so a caller cannot construct an unvalidated wire-shaped schedule.
func Every ¶
Every returns an interval rule. Its cadence is anchored when Anchor is supplied by the durable runtime; absent an explicit anchor, Next uses the Unix epoch as a stable default rather than process start time.
func LastDayOfMonth ¶
LastDayOfMonth returns a rule that fires on each month's final local day.
func Monthly ¶
Monthly returns a rule that fires on day (1 through 31). Months without that day are skipped.
type Schedule ¶
type Schedule struct {
// contains filtered or unexported fields
}
Schedule is an immutable, validated-on-use scheduling definition. A zero Schedule is invalid. Its location defaults to UTC unless In is called.
Typed-rule DST semantics are deliberately instant-based: Next walks real UTC minutes and matches their local civil representation. Spring-forward civil times do not run because they do not exist; a matching repeated fall-back time runs once for each real instant. Raw rules use robfig/cron's semantics.
func ScheduleFromDefinition ¶
func ScheduleFromDefinition(definition ScheduleDefinition) (Schedule, error)
ScheduleFromDefinition reconstructs and validates a Schedule from its neutral domain projection. It rejects a mismatched Canonical value so an adapter cannot silently reinterpret persisted schedule fields.
func Spec ¶
Spec wraps a rule in a Schedule. It does not panic: invalid declarations are retained and reported by Validate, Canonical, or Next.
func (Schedule) Anchor ¶
Anchor returns a copy with a durable interval anchor. It applies only to Every rules and lets a runtime retain cadence across restarts. The anchor is normalized to PostgreSQL's microsecond timestamp precision.
func (Schedule) Canonical ¶
Canonical returns a normalized five-field cron expression, or @every for an interval. The separate location and anchor are intentionally not encoded in that expression.
func (Schedule) Definition ¶
func (schedule Schedule) Definition() (ScheduleDefinition, error)
Definition returns a structured, normalized, persistence-ready projection.
func (Schedule) In ¶
In returns a copy scheduled in location. A nil location is reported by Validate instead of panicking.
func (Schedule) IntervalAnchor ¶
IntervalAnchor reports the explicit durable anchor, if any.
func (Schedule) Location ¶
Location returns the schedule location after validation. UTC is the default.
func (Schedule) Next ¶
Next returns the first matching real instant strictly after after. Raw rules delegate five-field parsing and occurrence evaluation to robfig/cron. Typed calendar rules are bounded to five years so malformed state cannot spin.
type ScheduleDefinition ¶
type ScheduleDefinition struct {
Kind ScheduleKind `json:"kind"`
Timezone string `json:"timezone"`
Canonical string `json:"canonical"`
Hour int `json:"hour,omitempty"`
Minute int `json:"minute,omitempty"`
Weekday time.Weekday `json:"weekday,omitempty"`
MonthDay int `json:"month_day,omitempty"`
Interval time.Duration `json:"interval,omitempty"`
Anchor time.Time `json:"anchor,omitempty"`
HasAnchor bool `json:"has_anchor,omitempty"`
}
ScheduleDefinition is the neutral persistence and boundary projection of a Schedule. It deliberately contains ordinary typed Go values: adapters map this into relational SQLC parameters or API messages at their own boundary. Canonical is a normalized expression, while the typed fields preserve the pleasant DSL shape for schedule kinds that have one.
type ScheduleKind ¶
type ScheduleKind string
ScheduleKind identifies the structured scheduling form in a Definition. It is a domain value, deliberately independent of protobuf and SQLC types.
const ( ScheduleKindDaily ScheduleKind = "daily" ScheduleKindWeekly ScheduleKind = "weekly" ScheduleKindMonthly ScheduleKind = "monthly" ScheduleKindLastDayOfMonth ScheduleKind = "last_day_of_month" ScheduleKindEvery ScheduleKind = "every" ScheduleKindRaw ScheduleKind = "raw" )
type StoreSnapshot ¶
type StoreSnapshot struct {
Triggers []TriggerState `json:"triggers"`
Occurrences []OccurrenceRecord `json:"occurrences"`
}
StoreSnapshot is a point-in-time copy of the active triggers and the most recent occurrences a store holds.
type TimeOfDay ¶
type TimeOfDay struct {
// contains filtered or unexported fields
}
TimeOfDay is a 24-hour local civil time.
type TriggerDefinition ¶ added in v0.2.0
type TriggerDefinition struct {
Name string `json:"name"`
Schedule ScheduleDefinition `json:"schedule"`
CatchUp CatchUpPolicy `json:"catch_up"`
Overlap OverlapPolicy `json:"overlap"`
}
TriggerDefinition is the static, persistence-neutral declaration of one trigger: its name, its schedule and its two policies. The operation a trigger invokes is registered in the process by name and never persisted; a store maps the definition to its own rows at its boundary.
func PreserveIntervalAnchor ¶ added in v0.2.0
func PreserveIntervalAnchor(incoming, existing TriggerDefinition) TriggerDefinition
PreserveIntervalAnchor keeps an established interval anchor when a trigger is declared again without one and nothing about its interval changed, so a restart does not restart the cadence.
type TriggerState ¶ added in v0.2.0
type TriggerState struct {
Definition TriggerDefinition `json:"definition"`
NextRunAt time.Time `json:"next_run_at"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
TriggerState is the durable scheduling cursor of one active trigger.