cron

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const MaxJobNameBytes = 256

MaxJobNameBytes caps the length of a job name. The cap exists to keep names cheap to log; it is not a security boundary on the name itself (names are opaque labels — sanitise them at any rendering call site).

Variables

View Source
var ErrInvalidJobName = errors.New("cron: invalid job name")

ErrInvalidJobName is returned by Register when Name is empty or longer than MaxJobNameBytes.

View Source
var ErrNilJobRun = errors.New("cron: job Run is nil")

ErrNilJobRun is returned by Register when Run is nil. A nil Run would nil-pointer at execution time; rejecting at registration surfaces the bug at startup instead of inside a tick callback.

Functions

func ParseCron

func ParseCron(spec string) (cronExpr, error)

ParseCron accepts the standard 5-field syntax plus the shortcuts @hourly / @daily / @weekly / @monthly / @yearly. Step values (*/N) and ranges (a-b) are supported.

Types

type CronJob

type CronJob struct {
	Name string
	Spec string
	Run  func(ctx context.Context) error
}

CronJob is the unit of scheduled work.

Run is invoked on every firing — its context is derived from the scheduler's parent context, so cancelling the scheduler cancels in-flight runs at the next yield point.

If Run returns an error it is forwarded to the scheduler's OnError callback (if set); otherwise it is silently dropped — jobs should not crash the process.

type Scheduler

type Scheduler struct {
	OnError func(jobName string, err error)
	// contains filtered or unexported fields
}

Scheduler is a tiny in-process cron driver. It is intentionally minimal: no persistence, no distributed locks, no overlap protection across replicas. For single-instance background work it is sufficient; for horizontally scaled deployments use the DB-backed queue instead.

func NewScheduler

func NewScheduler() *Scheduler

NewScheduler returns a Scheduler that wakes once per minute. The tick interval is a deliberate choice: cron resolution is one minute, and waking more often would burn CPU for no gain.

func (*Scheduler) Register

func (s *Scheduler) Register(job CronJob) error

Register adds a job. Returns an error if the spec is invalid, the name is empty or oversize, or Run is nil — callers catch typos at registration time rather than silently failing forever or nil-pointering at firing.

func (*Scheduler) RunOnce

func (s *Scheduler) RunOnce(ctx context.Context, now time.Time)

RunOnce fires every job whose schedule matches the given minute. Exported for tests that drive the tick manually instead of waiting on the wall clock; production code lets the loop call this.

Iterates under the lock without copying the slice. Jobs that mutate state (Register during tick) are safe because the mutex is held only for the read — new jobs appear on the next tick.

func (*Scheduler) Start

func (s *Scheduler) Start(ctx context.Context)

Start begins the tick loop in a goroutine. Returns immediately. Idempotent: repeated Start calls are no-ops once the loop is running.

func (*Scheduler) Stop

func (s *Scheduler) Stop()

Stop signals the loop to exit and blocks until it has. Safe to call multiple times, and safe to call before Start — if the loop was never launched there is nothing to wait for, so Stop returns immediately instead of blocking forever on a channel the loop never closes (which would hang graceful shutdown when boot aborts before Start runs).

Jump to

Keyboard shortcuts

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