Documentation
¶
Overview ¶
Package scheduler provides primitives for defining and running scheduled jobs.
Index ¶
- Variables
- type AdaptiveJob
- type BackoffFactory
- type Clock
- type Event
- type Hooks
- type IntervalSchedule
- type Job
- type JobFunc
- type JobID
- type NextScheduleFunc
- type Option
- type OverlapPolicy
- type RegistrationOption
- type Schedule
- type ScheduleFunc
- type Scheduler
- func (s *Scheduler) Add(schedule Schedule, job Job, options ...RegistrationOption) (JobID, error)
- func (s *Scheduler) AddAdaptiveFunc(schedule Schedule, run JobFunc, next NextScheduleFunc, ...) (JobID, error)
- func (s *Scheduler) AddCronFunc(spec string, fn JobFunc, options ...RegistrationOption) (JobID, error)
- func (s *Scheduler) AddCronJob(spec string, job Job, options ...RegistrationOption) (JobID, error)
- func (s *Scheduler) AddFunc(schedule Schedule, fn JobFunc, options ...RegistrationOption) (JobID, error)
- func (s *Scheduler) AddIntervalFunc(interval time.Duration, fn JobFunc, options ...RegistrationOption) (JobID, error)
- func (s *Scheduler) AddIntervalJob(interval time.Duration, job Job, options ...RegistrationOption) (JobID, error)
- func (s *Scheduler) FrozenIDs() []JobID
- func (s *Scheduler) Pause()
- func (s *Scheduler) Remove(id JobID) error
- func (s *Scheduler) Resume()
- func (s *Scheduler) Run(ctx context.Context) error
- func (s *Scheduler) Stop(ctx context.Context) error
- type SchedulerHooks
- type TickEvent
- type Timer
Constants ¶
This section is empty.
Variables ¶
var ( // ErrSchedulerRunning indicates that Run was called while the scheduler is active. ErrSchedulerRunning = errors.New("scheduler is already running") // ErrNilSchedule indicates that a registration has no schedule. ErrNilSchedule = errors.New("scheduler schedule must not be nil") // ErrNilJob indicates that a registration has no job. ErrNilJob = errors.New("scheduler job must not be nil") // ErrInvalidSchedule indicates that a schedule did not advance in time. ErrInvalidSchedule = errors.New("scheduler schedule must return a future time") // ErrNilContext indicates that Run received a nil context. ErrNilContext = errors.New("scheduler context must not be nil") // ErrUnknownJob indicates that Remove did not find the requested registration. ErrUnknownJob = errors.New("scheduler job ID was not found") )
var ErrInvalidInterval = errors.New("interval must be greater than zero")
ErrInvalidInterval indicates that an interval is not positive.
Functions ¶
This section is empty.
Types ¶
type AdaptiveJob ¶ added in v0.1.0
AdaptiveJob is a Job that may provide a replacement Schedule after each execution.
The scheduler calls Run and then NextSchedule with the current Schedule. A nil Schedule preserves the current schedule. A non-nil Schedule replaces it for the next execution. An error means that the replacement could not be calculated.
type BackoffFactory ¶
BackoffFactory creates isolated backoff state for one job execution.
type Clock ¶
Clock provides the current time and creates timers for scheduled work.
Implementations let the scheduler control time deterministically in tests.
type Hooks ¶
type Hooks struct {
OnStart func(context.Context, Event)
OnSuccess func(context.Context, Event)
OnFailure func(context.Context, Event)
OnRetry func(context.Context, Event)
OnSkip func(context.Context, Event)
}
Hooks receives lifecycle events for one job registration. Hook functions run synchronously and must return promptly.
type IntervalSchedule ¶
type IntervalSchedule struct {
// contains filtered or unexported fields
}
IntervalSchedule calculates execution times separated by a fixed interval.
func NewIntervalSchedule ¶
func NewIntervalSchedule(interval time.Duration) (IntervalSchedule, error)
NewIntervalSchedule returns a schedule with a positive fixed interval.
type Job ¶
Job performs one unit of scheduled work.
Run receives a context that is canceled when the scheduler stops. It returns a non-nil error when the scheduled work fails.
type NextScheduleFunc ¶ added in v0.2.0
NextScheduleFunc adapts a function to the schedule-selection part of an AdaptiveJob.
type Option ¶
type Option func(*Scheduler)
Option configures a Scheduler during construction.
func WithClock ¶
WithClock configures the clock used by the scheduler and its timers.
The clock must be set before the scheduler starts. Passing a nil clock is a programmer error.
func WithSchedulerHooks ¶ added in v0.2.0
func WithSchedulerHooks(hooks SchedulerHooks) Option
WithSchedulerHooks attaches lifecycle hooks to the scheduler.
type OverlapPolicy ¶
type OverlapPolicy uint8
OverlapPolicy controls what happens when a job is due while it is running.
const ( // AllowOverlap starts every due run, even if an earlier run is still active. AllowOverlap OverlapPolicy = iota // SkipOverlap discards a due run while an earlier run is still active. SkipOverlap )
type RegistrationOption ¶
type RegistrationOption func(*registrationOptions)
RegistrationOption configures one registered job.
func WithHooks ¶
func WithHooks(hooks Hooks) RegistrationOption
WithHooks attaches lifecycle hooks to one job registration.
func WithID ¶
func WithID(id JobID) RegistrationOption
WithID assigns a stable ID to one registered job.
func WithOverlap ¶
func WithOverlap(policy OverlapPolicy) RegistrationOption
WithOverlap configures overlap behavior for one job registration.
func WithRetry ¶
func WithRetry(attempts int, factory BackoffFactory) RegistrationOption
WithRetry retries a failed job up to attempts total attempts. factory creates a new backoff for each scheduled execution.
func WithTimeout ¶
func WithTimeout(timeout time.Duration) RegistrationOption
WithTimeout limits each job attempt to timeout.
type Schedule ¶
Schedule calculates the next time to run after a reference time.
Callers should pass the previous scheduled time so interval schedules do not drift when job execution takes longer than expected.
func NewCronSchedule ¶
NewCronSchedule parses expr with intervalok and returns it as a Schedule.
type ScheduleFunc ¶
ScheduleFunc adapts a function to Schedule.
type Scheduler ¶
type Scheduler struct {
// contains filtered or unexported fields
}
Scheduler coordinates registered jobs and schedules.
func (*Scheduler) Add ¶
Add registers a Job to run according to a Schedule. It may be called before or after Run starts; a registration added while running is scheduled from the current time and wakes the scheduler's timer loop.
func (*Scheduler) AddAdaptiveFunc ¶ added in v0.1.0
func (s *Scheduler) AddAdaptiveFunc( schedule Schedule, run JobFunc, next NextScheduleFunc, options ...RegistrationOption, ) (JobID, error)
AddAdaptiveFunc adapts separate execution and schedule-selection functions to AdaptiveJob and registers them according to schedule. A nil next function registers run as a regular Job and preserves the current schedule.
func (*Scheduler) AddCronFunc ¶
func (s *Scheduler) AddCronFunc( spec string, fn JobFunc, options ...RegistrationOption, ) (JobID, error)
AddCronFunc parses spec as an intervalok cron schedule and registers fn.
func (*Scheduler) AddCronJob ¶
func (s *Scheduler) AddCronJob( spec string, job Job, options ...RegistrationOption, ) (JobID, error)
AddCronJob parses spec as an intervalok cron schedule and registers job.
func (*Scheduler) AddFunc ¶
func (s *Scheduler) AddFunc( schedule Schedule, fn JobFunc, options ...RegistrationOption, ) (JobID, error)
AddFunc adapts fn to Job and registers it according to schedule.
func (*Scheduler) AddIntervalFunc ¶
func (s *Scheduler) AddIntervalFunc( interval time.Duration, fn JobFunc, options ...RegistrationOption, ) (JobID, error)
AddIntervalFunc creates a fixed interval schedule and registers fn.
func (*Scheduler) AddIntervalJob ¶
func (s *Scheduler) AddIntervalJob( interval time.Duration, job Job, options ...RegistrationOption, ) (JobID, error)
AddIntervalJob creates a fixed interval schedule and registers job.
func (*Scheduler) FrozenIDs ¶ added in v0.1.0
FrozenIDs returns the IDs of registrations whose Schedule stopped advancing. A frozen registration stays registered and excluded from due consideration until Remove is called explicitly.
func (*Scheduler) Pause ¶ added in v0.2.0
func (s *Scheduler) Pause()
Stop gracefully stops the scheduler. New jobs are not dispatched, active jobs are allowed to finish, and the scheduler can be started again with Run. The context controls how long the caller waits for shutdown. Pause keeps the scheduler loop alive while preventing job dispatches. Existing schedules are preserved and Resume re-evaluates them.
func (*Scheduler) Remove ¶
Remove stops future runs for id. A job that is already running is not interrupted and remains subject to normal shutdown handling.
func (*Scheduler) Resume ¶ added in v0.2.0
func (s *Scheduler) Resume()
Resume allows dispatches again. A schedule that became due while paused is dispatched once, then its normal scheduling policy continues.
func (*Scheduler) Run ¶
Run starts the scheduler and blocks until ctx is cancelled or Stop is called. Context cancellation is propagated to active jobs; Stop waits for active jobs without cancelling them.
type SchedulerHooks ¶ added in v0.2.0
type SchedulerHooks struct {
OnStart func(context.Context)
OnStopping func(context.Context)
OnStopped func(context.Context)
OnTick func(context.Context, TickEvent)
}
SchedulerHooks receives lifecycle events for the scheduler itself. Hook functions run synchronously and must return promptly.