scheduler

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultJobTimeout = 5 * time.Minute

DefaultJobTimeout caps a job that does not set a limit of its own.

View Source
const DefaultTickInterval = 5 * time.Second

DefaultTickInterval is used when a configured interval is missing or malformed.

Variables

View Source
var ErrJobNotFound = errors.New("job not found")

ErrJobNotFound is returned when no job carries the requested name.

Functions

func EverySpec

func EverySpec(interval, fallback time.Duration) string

EverySpec turns an interval into a cron spec. A non-positive interval means a broken configuration, and it must not turn into a job running every second, so the caller says what to fall back to; a non-positive fallback lands on DefaultTickInterval.

func Initialize

func Initialize() *cron.Cron

Initialize builds a scheduler that never starts a second copy of a job that is still running: a task ticking every few seconds would otherwise overlap itself and process the same rows twice.

The seconds field is optional, so both "15 3 * * *" and "0 30 3 * * *" are valid specs and a service is free to schedule a job below the minute without rewriting the rest of its specs.

func Names added in v1.2.0

func Names(jobs ...Job) []string

Names lists the job names, in the order the service declared them.

func Register

func Register(ctx context.Context, cronScheduler *cron.Cron, jobs ...Job)

Register adds jobs to the scheduler. The context belongs to the application, not to a single run: canceling it stops the jobs that are in flight.

func RunOnce added in v1.2.0

func RunOnce(ctx context.Context, name string, jobs ...Job) error

RunOnce runs a single job by name, outside of any schedule, and reports what it returned. It exists so a service can try a job from the command line instead of waiting for its next tick.

Types

type Job

type Job struct {
	Name                string
	Spec                string
	Timeout             time.Duration
	Quiet               bool
	FailuresBeforeAlarm int
	Run                 func(ctx context.Context) error
}

Job describes a scheduled task: when it runs, how long it may take and what it does.

FailuresBeforeAlarm is how many failures in a row a frequent job survives at warning level: a job ticking every few seconds recovers from a short database or network outage by itself, and one timeout is not worth an alert. It stays 1 unless the job says otherwise, so a rare job reports the very first failure — three silent nights of a nightly job is not a trade worth making.

Jump to

Keyboard shortcuts

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