cron

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package cron runs jobs on a wall-clock schedule as a supervised worker.Runnable. It is a thin, production-shaped wrapper over robfig/cron: it validates specs up front, recovers panics so one bad run cannot crash the process, threads the run context into every job, and stops gracefully when its context is canceled.

It complements the worker package: a worker.Loop fires on a fixed interval since boot ("every 5m"); a cron Scheduler fires on a wall-clock spec ("at 03:00 daily", "@hourly", "@every 1h30m"). For a job that must run on at most one replica, gate it with a lock.Locker (see `skit add cron --lock`).

Specs accept the standard 5-field cron syntax (minute hour dom month dow) and the @-descriptors (@hourly, @daily, @weekly, @monthly, @every <dur>).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Job

type Job func(ctx context.Context) error

Job is one scheduled unit of work. A returned error is logged; it does not stop the schedule. Jobs receive the scheduler's run context, so a long job should honor cancellation for a prompt shutdown.

type Scheduler

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

Scheduler fires registered Jobs on their cron specs. Build it, Add jobs, then hand it to a worker.Group as a Runnable. It is not safe to Add after Start.

func New

func New(log *slog.Logger, name string) *Scheduler

New builds an empty Scheduler named name (used in logs and panic metrics). log may be nil.

func (*Scheduler) Add

func (s *Scheduler) Add(spec, jobName string, job Job) error

Add registers job to run on spec. The spec is validated immediately, so a typo fails at wiring time rather than silently never firing. jobName labels the job in logs.

func (*Scheduler) Name

func (s *Scheduler) Name() string

Name implements worker.Runnable.

func (*Scheduler) OnPanic

func (s *Scheduler) OnPanic(h safetick.PanicHandler) *Scheduler

OnPanic sets a handler invoked when a job panics (e.g. to bump a metric). The panic is always recovered and logged regardless.

func (*Scheduler) Start

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

Start implements worker.Runnable: it runs the scheduler until ctx is canceled, then waits for any in-flight jobs to finish before returning. Each firing runs in its own goroutine (robfig default), wrapped in panic recovery and given ctx.

func (*Scheduler) Stop

func (s *Scheduler) Stop(context.Context) error

Stop implements worker.Runnable. The scheduler stops when its Start context is canceled (the worker.Group cancels it on shutdown), so Stop is a no-op.

Jump to

Keyboard shortcuts

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