scheduler

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: May 18, 2021 License: MPL-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package scheduler allows callers to register recurring jobs on the controller. The scheduler will periodically query the repository for registered jobs that should be run.

Before a job can be invoked by the scheduler, it must be made known to the scheduler by being registered on the instance of the scheduler that is running.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Job

type Job interface {
	// Status reports the job’s current status.  The status is periodically persisted by
	// the scheduler when a job is running, and will be used to verify a job is making progress.
	Status() JobStatus

	// Run performs the required work depending on the implementation.
	// The context is used to notify the job that it should exit early.
	Run(ctx context.Context) error

	// NextRunIn returns the duration until the next job run should be scheduled.  This
	// method is invoked after a run has successfully completed and the next run time
	// is being persisted by the scheduler.
	NextRunIn() time.Duration

	// Name is the human readable name of the job.
	Name() string

	// Description is the human readable description of the job.
	Description() string
}

Job defines an interface for jobs that can be invoked by the scheduler.

type JobStatus

type JobStatus struct {
	// Completed and Total are used to indicate job progress,
	// each job implementation will determine the definition of
	// progress by calculating both Completed and Total.
	Completed, Total int
}

JobStatus defines the struct that must be returned by the Job.Status() method.

type Option

type Option func(*options)

Option - how Options are passed as arguments

func WithInterruptThreshold

func WithInterruptThreshold(l time.Duration) Option

WithInterruptThreshold provides an option to provide the duration after which a controller will interrupt a running job that is not updating its status. If WithInterruptThreshold == 0, then default duration is used.

func WithMonitorInterval

func WithMonitorInterval(l time.Duration) Option

WithMonitorInterval provides an option to provide the interval at which the scheduler will query running jobs for status and update the repository accordingly. If WithMonitorInterval == 0, then default interval is used.

func WithRunJobsInterval

func WithRunJobsInterval(l time.Duration) Option

WithRunJobsInterval provides an option to provide the interval at which the scheduler will query the repository for jobs to run. If WithRunJobsInterval == 0, then default interval is used.

func WithRunJobsLimit

func WithRunJobsLimit(l uint) Option

WithRunJobsLimit provides an option to provide the number of jobs that will be requested by the scheduler when querying for jobs to run. If WithRunJobsLimit == 0, then default run jobs limit is used.

type Scheduler

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

Scheduler is used to register and run recurring jobs on the controller.

func New

func New(serverId string, jobRepoFn jobRepoFactory, logger hclog.Logger, opt ...Option) (*Scheduler, error)

New creates a new Scheduler

• serverId must be provided and is the private id of the server that will run the scheduler

• jobRepoFn must be provided and is a function that returns the job repository

• logger must be provided and is used to log errors that occur during scheduling and running of jobs

WithRunJobsLimit, WithRunJobsInterval, WithMonitorInterval and WithInterruptThreshold are the only valid options.

func (*Scheduler) RegisterJob

func (s *Scheduler) RegisterJob(ctx context.Context, j Job, _ ...Option) error

RegisterJob registers a job with the scheduler and persists the job into the repository.

• job must be provided and is an implementer of the Job interface.

All options are ignored.

func (*Scheduler) Start

func (s *Scheduler) Start(ctx context.Context) error

Start begins the scheduling loop that will query the repository for jobs to run and run them in a goroutine, the scheduler will stop all running jobs and stop requesting new jobs once the ctx past in is canceled. The scheduler cannot be started again once the ctx is canceled, a new scheduler will need to be instantiated in order to begin scheduling again.

func (*Scheduler) UpdateJobNextRun

func (s *Scheduler) UpdateJobNextRun(ctx context.Context, name string, nextRunIn time.Duration, _ ...Option) error

UpdateJobNextRun sets the next scheduled run time for the provided name to the current database time incremented by the nextRunIn parameter. If nextRunIn == 0 the job will be available to run immediately.

All options are ignored.

Directories

Path Synopsis
job
Package job provides a Job and a Run struct suitable for persisting to the repository.
Package job provides a Job and a Run struct suitable for persisting to the repository.

Jump to

Keyboard shortcuts

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