grpcproc
Documentation

Cron jobs

Jobs on crontab schedules, run by a process you start: every run is a process of its own, whose exit reason is its result.

grpcproc/cron is a package of the core module. The cron process runs on a node or under a supervisor, like any other.

Install and start

go get github.com/floatdrop/grpcproc # cron is a package of it
c, err := cron.Start(node, cron.Spec{Jobs: []cron.Job{{
	Name:     "nightly-report",
	Spec:     "10 3 * * *",
	Location: berlin, // UTC if nil
	Action: cron.Call(reporter, func(r cron.Run) *reportpb.Make {
		return &reportpb.Make{Day: r.Time.Format(time.DateOnly)}
	}),
	Overlap:   cron.Forbid,
	Timeout:   10 * time.Minute,
	OnFailure: func(r cron.Run, reason string) { alerts.Page("report", reason) },
}}}, grpcproc.WithName("cron"))

Start checks the whole spec first, every schedule parsed and every job complete, and returns the cron process's address. Under a supervisor, cron.Child(name, spec) returns the actor.ChildSpec instead, checked the same way. Every node that starts a cron process runs its jobs; for a job that runs once in a cluster, see Once in a cluster.

Schedules

A spec is five fields: minute, hour, day of month, month and day of week. A field is * or a list of values, ranges and steps: 0,30, 9-17, */15, 0-30/10, and 5/20 for 5-59/20. Months and weekdays take names, JAN or mon-fri, and Sunday is 0 or 7. When both day fields are restricted, a day that matches either runs; when one of them starts with *, a day must match both, as in Vixie cron.

Beyond the standardMeans
LIn the day of month: the month’s last day.
5LIn the day of week: the month’s last Friday.
5#2In the day of week: the month’s second Friday.
@hourly, @daily, @weekly, @monthly, @yearlyThe whole spec: 0 * * * *, 0 0 * * *, 0 0 * * 0, 0 0 1 * *, 0 0 1 1 *. @midnight and @annually are the same as @daily and @yearly; @reboot is not a schedule.

cron.Parse(spec) returns the Schedule, and Next(t) the first minute after t it names, in t's zone, by the rules the cron process runs by.

Time zones and clock changes

A job's Spec is read in its Location, UTC when it is nil, so the nodes of a cluster read a spec alike whatever their own zone, and each job of a process can have its own.

Where clocks change, the wall clock decides. A job due in a minute the clocks skip when they go forward does not run that day. A minute they repeat when they go back runs the first time only. Next answers the same way:

cron/example_test.go
func ExampleSchedule_Next() {
	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
}

The cron process checks the time at the start of every minute by the wall clock. A clock stepped back runs no minute twice, and one stepped forward skips minutes as a process that was stopped would (Missed runs).

Runs are processes

Each run is spawned by the cron process, linked to it (it ends if the cron process does), and labelled cron:<job>, so the node's process list shows every run going. Its Action is one of:

ActionThe run
cron.Send(to, build)Sends build(run) to to, and succeeds once it is sent.
cron.Call(to, build)Calls to, on this node or another, and succeeds if the answer is not an error: the work runs where the process that does it runs.
cron.Func(fn)Runs fn(ctx, run); ctx ends when the run is told to exit.
func(*grpcproc.Process[proto.Message], cron.Run) errorIs the run’s process: it can monitor, call and spawn like any other.

Run.Time is the minute the run was due, in its job's zone, whenever it actually starts. A run fails with any exit reason but normal: the error it returned, a panic, timeout or replaced. OnFailure hears of each failure on the cron process's goroutine, so it must not block; the process also logs it, and keeps the reason where the Inspector shows it.

OverlapA run due while the job’s last run still goes
AllowStarts beside it. The default.
ForbidIs skipped: the last run goes on.
ReplaceStarts, once the last run is told to exit with reason replaced.

A job's Timeout bounds its runs: one that outlives it is told to exit, with reason timeout. A goroutine cannot be killed, so an Action that does I/O should pass its context on. That context ends at the run's Deadline, so a call made with it, cron.Call's included, carries the time left to the callee.

Missed runs

A run is due at the start of its minute. One missed while the cron process was not running, or was held up, is skipped, unless its job has a StartingDeadline: then the latest run it missed starts late, if it is still within the deadline, and once, however many it missed. A job added to a running process owes nothing from before it was added, and one enabled again nothing from while it was disabled.

Across processes, the cron process's state carries it. Whenever a run starts, the process reports each job's last run, a *cronv1.State, through Spec.OnState; a process started with that state as Spec.Resume catches up from there. It checks the minute it starts in at once: a run due in it, which the process it takes over from did not start, starts then rather than a minute late. A supervisor that restarts a cron process resumes it from its last state by itself.

Changing jobs

c.AddJob(ctx, node, job)           // from c's node only: a job holds Go functions
c.DisableJob(ctx, node, "nightly") // from any node
c.EnableJob(ctx, node, "nightly")  // runs from the next minute; what it missed is not caught up
c.RemoveJob(ctx, node, "nightly")

c = cron.Named("a", "cron")        // a cron process by its name

c is a cron.Crontab, what cron.Start returns. Each change takes who asks, the node or a process from inside its handler, as an address's Call does.

Runs already going go on. An unknown name is cron.ErrNoJob, a name taken cron.ErrJobExists, whichever node asked. A cron process a supervisor restarts starts again from its spec's jobs: what AddJob added is gone with the process that had it.

Inspecting jobs

The cron process publishes a line per job, which grpcprocctl inspect shows: its spec and zone, when it runs next, when it last ran, how many runs are going, and why the last one that failed did. grpcprocctl cron and the Web UI list the jobs of every cron process.

  every-quarter:   */15 * * * * UTC, disabled
  nightly-report:  10 3 * * * Europe/Berlin, next 2026-09-29T03:10:00+02:00, last 2026-09-28T03:10:00+02:00, running 1, failed: timeout

Each run is in the process list, under its label, with the cron process as its parent.

Once in a cluster

A job that must run once in the cluster, not once per node, runs where the cluster's leader runs: the cron process as the singleton of grpcproc/leader. The singleton's state is the cron process's, saved as each run starts, and the next leader's cron process resumes from it:

examples/singleton/singleton_test.go
func election(jobs []cron.Job) leader.Spec[*cronv1.State] {
	return leader.Spec[*cronv1.State]{
		Cluster: "cron",
		Voters:  []string{"a", "b", "c"},
		Singleton: func(l *leader.Lease[*cronv1.State], last *cronv1.State) (actor.ChildSpec, error) {
			return cron.Child("cron", cron.Spec{Jobs: jobs, Resume: last, OnState: l.Save})
		},
	}
}

The two packages do not import each other: they meet at actor.ChildSpec, which cron.Child returns and a singleton is, and at the state, which OnState reports and Lease.Save takes. The example's test crashes the leader a moment before a job is due, and checks that every minute ran once, the one due during the election included.

That test, and the package's own, run in a testing/synctest bubble, whose fake clock makes the minutes and hours a schedule spans pass at once.