Help us shape the future of Go! Take the 2026 Go Developer Survey

cron

package
v0.19.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package cron runs named jobs once their interval has passed, each under a lease in a store.

Index

Constants

This section is empty.

Variables

View Source
var ErrLeaseLost = errors.New("cron: lease lost")

ErrLeaseLost marks a settle whose lease another run has taken over.

Functions

This section is empty.

Types

type Config

type Config struct {
	Jobs      []Job
	Store     Store
	Clock     func() time.Time
	TimeLimit time.Duration
	Lease     time.Duration
	Logger    *slog.Logger
}

Config is what a runner is built from: the jobs, their store, the clock and the limits.

type Job

type Job struct {
	Name  string
	Every time.Duration
	Run   func(ctx context.Context, now time.Time) error
}

Job is one named piece of work that falls due again each time its interval passes.

type Lease

type Lease struct {
	Name    string
	Token   string
	Taken   time.Time
	Until   time.Time
	NextRun time.Time
}

Lease is one run's hold on one job, proven by its token.

type Outcome

type Outcome struct {
	NextRun  time.Time
	Finished time.Time
	Failure  string
}

Outcome is how one run ended and when its job falls due next.

type Report

type Report struct {
	Ran    int
	Failed int
	Held   int
}

Report counts what one pass over the cron jobs did.

type Runner

type Runner struct {
	// contains filtered or unexported fields
}

Runner runs a list of cron jobs, one at a time.

func New

func New(cfg Config) *Runner

New returns a runner over cfg, leaving out each job it cannot run, and running none under a config it cannot use.

func (*Runner) RunDue

func (r *Runner) RunDue(ctx context.Context) (Report, error)

RunDue runs each job that is due and free in turn until the store fails, erring only when it cannot read the slots.

type Slot

type Slot struct {
	Name        string
	NextRun     time.Time
	LeasedUntil time.Time
}

Slot is where one stored job stands: when it falls due next and until when a run holds it.

type Store

type Store interface {
	Slots(ctx context.Context) ([]Slot, error)
	Take(ctx context.Context, lease Lease) (Lease, bool, error)
	Settle(ctx context.Context, lease Lease, outcome Outcome) error
}

Store keeps each job's next run time and the lease that guards a run.

Directories

Path Synopsis
Package crontest holds the cases every cron job store passes and an in-memory store that passes them.
Package crontest holds the cases every cron job store passes and an in-memory store that passes them.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL