Documentation
¶
Overview ¶
Package scheduler runs concurrent jobs with OS signal-aware cancellation, including cron-style scheduling and parsing of traditional crontab specs.
Index ¶
- func LoggerFromContext(ctx context.Context) *slog.Logger
- func Run(ctx context.Context, jobs ...Job) error
- type Cron
- func (c *Cron) Invoke(ctx context.Context) error
- func (c *Cron) WithExitOnError(enabled bool) *Cron
- func (c *Cron) WithImmediate(enabled bool) *Cron
- func (c *Cron) WithInterval(interval time.Duration) *Cron
- func (c *Cron) WithLogger(logger *slog.Logger) *Cron
- func (c *Cron) WithSchedule(schedule string) *Cron
- type FrequencySchedule
- type Job
- type JobFunc
- type JobLoggerFunc
- type Schedule
- type SpecSchedule
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LoggerFromContext ¶
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 ¶
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 ¶
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 ¶
Invoke runs the cron job. This is typically not called directly, but rather via Run.
func (*Cron) WithExitOnError ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 JobLoggerFunc ¶
JobLoggerFunc is a function that can be used to create a Job that has a logger, in addition to the context.
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.
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