cron

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 12 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>).

Usage

sched := cron.New(log, cron.Config{
    Name:           "reports",
    Metrics:        d.Metrics,
    TracerProvider: d.TracerProvider,
})
if err := sched.Add("0 3 * * *", "nightly-report", report.Run); err != nil {
    return err
}
group.Add(sched) // a worker.Runnable

Metrics

With Config.Metrics set, every run is recorded under its job name: skit_cron_runs_total by cron_job and result (ok, error, panic), skit_cron_run_duration_seconds and skit_cron_last_success_timestamp_seconds by cron_job. The label is cron_job, not job: Prometheus puts its own job label on every series.

Tracing

Every run is the root span of its own trace, named after the job and taken from Config.TracerProvider: a run has no request behind it, and what the job does — queries, calls, published events — nests under that span. A failed run sets the span's error; a panic sets error.type "panic".

Options

  • Config: Name (default "cron"), Metrics, TracerProvider.
  • WithOnPanic: a handler called when a job panics; the panic is recovered, logged and counted either way.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config added in v0.14.0

type Config struct {
	// Name identifies the scheduler in logs (defaults to "cron").
	Name string
	// Metrics is the registry of the run metrics of the jobs; nil disables
	// them.
	Metrics prometheus.Registerer
	// TracerProvider records the root span of every run; nil → the global
	// provider.
	TracerProvider trace.TracerProvider
}

Config configures a Scheduler.

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 Option added in v0.14.0

type Option func(*Scheduler)

Option customizes a Scheduler.

func WithOnPanic added in v0.14.0

func WithOnPanic(h safetick.PanicHandler) Option

WithOnPanic sets a handler invoked when a job panics. The panic is always recovered, logged and counted regardless.

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, cfg Config, opts ...Option) *Scheduler

New builds an empty Scheduler. 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 and metrics; give each job its own.

func (*Scheduler) Name

func (s *Scheduler) Name() string

Name implements worker.Runnable.

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