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.
Example ¶
A Scheduler is a worker.Runnable: the Group starts it and stops it with the rest of the process. The job runs at 03:00 every day until the signal.
package main
import (
"context"
"log/slog"
"os"
"os/signal"
"syscall"
"time"
"github.com/assanoff/skit/cron"
"github.com/assanoff/skit/worker"
)
func main() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
log := slog.Default()
sched := cron.New(log, cron.Config{Name: "reports"})
err := sched.Add("0 3 * * *", "nightly-report", func(ctx context.Context) error {
log.InfoContext(ctx, "building the nightly report")
return nil
})
if err != nil {
log.ErrorContext(ctx, "cron", "err", err)
return
}
g := worker.NewGroup(log, worker.GroupConfig{ShutdownTimeout: 10 * time.Second})
g.Add(sched)
if err := g.Run(ctx); err != nil {
log.ErrorContext(ctx, "group stopped", "err", err)
}
}
Output:
Index ¶
Examples ¶
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 ¶
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 (*Scheduler) Add ¶
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.
Example ¶
Add parses the spec at once, so a typo fails at wiring time instead of never firing.
package main
import (
"context"
"fmt"
"github.com/assanoff/skit/cron"
)
func main() {
sched := cron.New(nil, cron.Config{Name: "reports"})
report := func(context.Context) error { return nil }
for _, spec := range []string{"0 3 * * *", "@hourly", "@every 1h30m", "0 3 * *"} {
if err := sched.Add(spec, "report", report); err != nil {
fmt.Println(err)
continue
}
fmt.Println("added", spec)
}
}
Output: added 0 3 * * * added @hourly added @every 1h30m cron "reports": invalid spec "0 3 * *" for "report": expected exactly 5 fields, found 4: [0 3 * *]