scheduler

package
v0.0.0-...-98de524 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package scheduler runs concurrent jobs with OS signal-aware cancellation, including cron-style scheduling and parsing of traditional crontab specs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoggerFromContext

func LoggerFromContext(ctx context.Context) *slog.Logger

LoggerFromContext returns the logger from the context. If no logger is found, the default logger is returned, which is slog.Default. A logger will only be available if invoked through Run.

func Run

func Run(ctx context.Context, jobs ...Job) error

Run invokes all jobs concurrently, and listens for any termination signals (SIGINT, SIGTERM, SIGQUIT, etc).

If any jobs return an error, all jobs will terminate (assuming they listen to the provided context), and the first known error will be returned. We will wait for all jobs to finish before returning.

Types

type Cron

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

func NewCron

func NewCron(name string, job Job) *Cron

NewCron creates a new cron job with the provided name and underlying job. The cron job will run the job at the provided interval, and will exit on error if the Cron.WithExitOnError flag is set. The default interval is 5 minutes, which can be changed with Cron.WithInterval (for standard time.Duration's), or Cron.WithSchedule (for crontab-style schedules).

func (*Cron) Invoke

func (c *Cron) Invoke(ctx context.Context) error

Invoke runs the cron job. This is typically not called directly, but rather via Run.

func (*Cron) WithExitOnError

func (c *Cron) WithExitOnError(enabled bool) *Cron

WithExitOnError sets whether the cron job should exit on error. This defaults to false. If true, the job will exit if the underlying job returns an error.

func (*Cron) WithImmediate

func (c *Cron) WithImmediate(enabled bool) *Cron

WithImmediate sets whether the cron job should run the underlying job immediately upon creation. This defaults to false. If true, the job will also exit on error if the initial immediate run fails.

func (*Cron) WithInterval

func (c *Cron) WithInterval(interval time.Duration) *Cron

WithInterval sets the interval at which the cron job will run the underlying job. Defaults to 5 minutes, and cannot be less than 1 second.

func (*Cron) WithLogger

func (c *Cron) WithLogger(logger *slog.Logger) *Cron

WithLogger sets the logger for the cron job. This defaults to the default logger. You can obtain the logger from the context via LoggerFromContext.

func (*Cron) WithSchedule

func (c *Cron) WithSchedule(schedule string) *Cron

WithSchedule sets the schedule at which the cron job will run the underlying job. It supports standard crontab-style schedules (e.g. "0 5 * * *") as well as "@every 1h30m", "@hourly", "@daily", "@midnight", "@weekly", "@monthly", "@yearly", and "@annually".

type FrequencySchedule

type FrequencySchedule struct {
	Delay time.Duration
}

FrequencySchedule represents a simple recurring duty cycle, e.g. "Every 5 minutes". It does not support jobs more frequent than once a second.

func Every

func Every(dur time.Duration) FrequencySchedule

Every returns a crontab Schedule that activates once every duration. Delays of less than a second are not supported (will round up to 1 second). Any fields less than a Second are truncated.

func (FrequencySchedule) Next

func (s FrequencySchedule) Next(t time.Time) time.Time

Next returns the next time this should be run. This rounds so that the next activation time will be on the second.

func (FrequencySchedule) String

func (s FrequencySchedule) String() string

type Job

type Job interface {
	Invoke(ctx context.Context) error
}

Job is a generic runnable entity. See also JobFunc.

type JobFunc

type JobFunc func(ctx context.Context) error

JobFunc is a function that can be used to create a Job.

func (JobFunc) Invoke

func (r JobFunc) Invoke(ctx context.Context) error

type JobLoggerFunc

type JobLoggerFunc func(ctx context.Context, l *slog.Logger) error

JobLoggerFunc is a function that can be used to create a Job that has a logger, in addition to the context.

func (JobLoggerFunc) Invoke

func (f JobLoggerFunc) Invoke(ctx context.Context) error

type Schedule

type Schedule interface {
	// Next returns the next activation time, later than the given time. Next is
	// invoked initially, and then each time the job is run.
	Next(time.Time) time.Time

	// String returns the string representation of the schedule.
	String() string
}

Schedule describes a job's duty cycle.

func Parse

func Parse(spec string) (Schedule, error)

Parse returns a new crontab schedule representing the given spec. It requires 5 entries representing: minute, hour, day of month, month and day of week, or descriptors, e.g. "@midnight", "@every 1h30m".

type SpecSchedule

type SpecSchedule struct {
	Source     string
	Minute     uint64
	Hour       uint64
	DayOfMonth uint64
	Month      uint64
	DayOfWeek  uint64

	// Override location for this schedule.
	Location *time.Location
}

SpecSchedule specifies a duty cycle (to the second granularity), based on a traditional crontab specification. It is computed initially and stored as bit sets.

func (*SpecSchedule) Next

func (s *SpecSchedule) Next(t time.Time) time.Time

Next returns the next time this schedule is activated, greater than the given time. If no time can be found to satisfy the schedule, return the zero time.

func (SpecSchedule) String

func (s SpecSchedule) String() string

Jump to

Keyboard shortcuts

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