Documentation
¶
Overview ¶
Package cron runs jobs on schedules written in crontab syntax, from a grpcproc process the application starts:
c, err := cron.Start(node, cron.Spec{Jobs: []cron.Job{{
Name: "nightly-report",
Spec: "10 3 * * *",
Location: berlin,
Action: cron.Send(reporter, func(r cron.Run) *reportpb.Make { return &reportpb.Make{Day: r.Time.Format(time.DateOnly)} }),
}}}, grpcproc.WithName("cron"))
The process checks the time at the start of every minute, and starts the jobs due then. Every run is a process of its own, linked to the cron process, so a run that fails is its exit reason, one that is still going when the next is due can be left alone, skipped or replaced (Overlap), one that takes too long is told to exit (Timeout), and every run shows up among the node's processes, labelled "cron:" and its job's name.
A job's Spec is read in its own Location, UTC unless set. A minute that clocks skip when they go forward does not exist, so nothing due then runs; one they repeat when they go back runs the first time only.
Only this node's jobs are its: every node that starts a cron process runs its jobs. For a job that runs once in the cluster, run the cron process as a grpcproc/leader singleton, which resumes on the next leader from the State the last one reported.
Index ¶
- Constants
- Variables
- func Child(name string, spec Spec, opts ...grpcproc.SpawnOption) (actor.ChildSpec, error)
- type Action
- type Crontab
- func (c Crontab) AddJob(ctx context.Context, from grpcproc.Caller, job Job) error
- func (c Crontab) Addr() grpcproc.Addr[*cronv1.Control]
- func (c Crontab) DisableJob(ctx context.Context, from grpcproc.Caller, name string) error
- func (c Crontab) EnableJob(ctx context.Context, from grpcproc.Caller, name string) error
- func (c Crontab) RemoveJob(ctx context.Context, from grpcproc.Caller, name string) error
- func (c Crontab) String() string
- type Job
- type Overlap
- type Run
- type Schedule
- type Spec
Examples ¶
Constants ¶
const ( ReasonTimeout = "timeout" ReasonReplaced = "replaced" )
The exit reasons the cron process gives the runs it tells to exit.
Variables ¶
var ( // ErrNoJob is the answer to a change to a job the cron process does // not have. ErrNoJob = errors.New("cron: no such job") // ErrJobExists is the answer to AddJob with a name the cron process // already has. ErrJobExists = errors.New("cron: a job has that name") )
Functions ¶
Types ¶
type Action ¶
Action is what a run does, in a process of its own spawned by the cron process and linked to it: its return is the run's exit reason. A run told to exit (Overlap Replace, Timeout, or the cron process ending) has its context cancelled. A run that returns context.DeadlineExceeded once its Deadline has passed ends with reason "timeout", as one told to exit at its Deadline does.
func Call ¶
Call is an Action that calls to with what req builds for the run. The run succeeds when to answers without an error, and fails with the error otherwise; the job's Timeout bounds the wait, and to sees the run's Deadline as the call's.
func Func ¶
Func is an Action that calls fn with the run's context, which ends at the run's Deadline.
type Crontab ¶
type Crontab struct {
// contains filtered or unexported fields
}
Crontab addresses a cron process, and changes its jobs: what Start returns, or Named finds. from, in each of its methods, is who asks: the Node, or a Process from inside its handler, as for grpcproc.Addr.Call.
func Start ¶
Start checks spec and spawns a cron process for it on n, and returns its Crontab. opts apply to the process: a name, to reach it by.
func (Crontab) AddJob ¶
AddJob adds job to the cron process. from must be on its node: a job holds Go functions, which no message can carry, and the cron process refuses a caller of another node. The job's runs are due from the minute after it is added.
func (Crontab) Addr ¶
Addr returns the address of the cron process, to monitor it, link to it, or ask it to exit.
func (Crontab) DisableJob ¶
DisableJob keeps a job but runs it no more until EnableJob. Its runs already going go on.
func (Crontab) EnableJob ¶
EnableJob runs a disabled job again, from the minute after. Runs it missed while disabled are not caught up.
type Job ¶
type Job struct {
// Name identifies the job in its cron process.
Name string
// Spec is when the job runs, in crontab syntax: see Parse.
Spec string
// Location is the time zone Spec is read in. Nil means UTC, so that
// the nodes of a cluster read a spec alike whatever their own zone.
Location *time.Location
// Action is what a run does.
Action Action
// Overlap says what happens when a run is due while the job's previous
// run still goes. By default, Allow.
Overlap Overlap
// Timeout, if positive, bounds a run: one that outlives it is told to
// exit, with reason "timeout".
Timeout time.Duration
// StartingDeadline, if positive, is how late a run may start. A run
// missed while the cron process was not running (a node restarted, a
// leader changed) or was held up starts late if it is still within
// the deadline, once however many of the job's runs were missed. By
// default a missed run is skipped.
StartingDeadline time.Duration
// Disabled keeps the job without running it, until EnableJob.
Disabled bool
// OnFailure, if set, is told of every run that ends with a reason
// other than normal: the error its Action returned, a panic, "timeout"
// or "replaced". It runs on the cron process's goroutine, so it must not
// block.
OnFailure func(r Run, reason string)
}
Job is a task run on a schedule.
type Overlap ¶
type Overlap uint8
Overlap says what a job does when a run is due while its previous run still goes.
type Run ¶
type Run struct {
Job string
// Time is the minute the run was due, in its job's Location. A run
// that starts late, within its job's StartingDeadline, keeps it.
Time time.Time
// Deadline is when the run is told to exit, from its job's Timeout:
// zero for a job with none.
Deadline time.Time
}
Run is one run of a job, as its Action sees it.
type Schedule ¶
type Schedule struct {
// contains filtered or unexported fields
}
Schedule is a parsed crontab spec: the minutes it names, in whatever location it is asked about.
func Parse ¶
Parse reads a crontab spec: five fields, minute (0-59), hour (0-23), day of month (1-31), month (1-12 or JAN-DEC) and day of week (0-7 or SUN-SAT, Sunday being 0 and 7). A field is * or a comma-separated list of values, ranges (1-5) and steps (*/15, 0-30/10, 5/20, which is 5-59/20). When both day fields are restricted a day matching either runs, and when either starts with * a day must match both, as in Vixie cron.
Beyond the standard, as in Quartz: L in the day of month is its last day; in the day of week, 5L is the month's last Friday and 5#2 its second. The macros are @yearly (or @annually), @monthly, @weekly, @daily (or @midnight) and @hourly; @reboot is not a schedule.
func (*Schedule) Next ¶
Next is the first minute after t that s names, in t's location, or the zero time if there is none within the next ten years (as for "0 0 30 2 *"). A minute that is skipped when clocks go forward does not exist, so it never matches; one that repeats when clocks go back matches the first time only.
Example ¶
package main
import (
"fmt"
"time"
"github.com/floatdrop/grpcproc/cron"
)
func main() {
s, err := cron.Parse("30 1 * * *")
if err != nil {
panic(err)
}
// On November 1, 2026, New York's clocks go back from 02:00 to 01:00:
// 01:30 comes twice, and the job runs the first time.
ny, _ := time.LoadLocation("America/New_York")
t := time.Date(2026, 10, 31, 12, 0, 0, 0, ny)
for range 3 {
t = s.Next(t)
fmt.Println(t.Format("Mon Jan 2 15:04 MST"))
}
}
Output: Sun Nov 1 01:30 EDT Mon Nov 2 01:30 EST Tue Nov 3 01:30 EST
type Spec ¶
type Spec struct {
// Jobs run from the start of the minute after the process starts.
Jobs []Job
// Resume is the State an earlier cron process reported through
// OnState: each job's last run, from which a job with a
// StartingDeadline catches up on what it missed since.
Resume *cronv1.State
// OnState, if set, is given the process's State every time a job
// starts a run. It runs on the cron process's goroutine, so it must
// not block. A cron process that a supervisor restarts resumes from the
// last State it reported, whether OnState is set or not.
OnState func(*cronv1.State)
}
Spec describes a cron process.