cron

package module
v0.0.0-...-f60128a Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2022 License: MIT Imports: 6 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAlreadyExists = errors.New("already exists")
)

Functions

This section is empty.

Types

type Cron

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

Cron is a fault-tolerant job scheduler.

func New

func New(locker Locker, opts *Options) *Cron

New creates an instance of Cron.

func (*Cron) Add

func (c *Cron) Add(name, expr string, task func()) error

Add adds a job with the given properties. If name already exists, Add will return ErrAlreadyExists, otherwise it will return nil.

Note that the execution interval of the job, which is specified by expr, must be greater than LockTTL.

func (*Cron) AddJob

func (c *Cron) AddJob(job ...Job) error

AddJob adds one or more jobs into Cron c. If the name of any job already exists, AddJob will return ErrAlreadyExists, otherwise it will return nil.

func (*Cron) Start

func (c *Cron) Start()

Start starts to schedule all jobs.

func (*Cron) Stop

func (c *Cron) Stop()

Stop stops all the jobs. For simplicity now, it does not wait for the inner goroutines (which have been started before) to exit.

type Job

type Job struct {
	// The unique name of the job.
	Name string

	// The cron expression. See https://github.com/gorhill/cronexpr#implementation.
	//
	// Note that the execution interval of the job must be greater than LockTTL.
	Expr string

	// The handler of the job.
	Task func()
}

Job represents a normal job, which will be scheduled by Cron.

type Locker

type Locker interface {
	// Lock obtains the lock to execute the job named job. If the lock is
	// successfully obtained, Lock will return true, otherwise it will return false.
	//
	// The implementation of Locker must release the obtained lock automatically
	// after ttl elapses.
	Lock(job string, ttl time.Duration) (bool, error)
}

Locker is a distributed lock.

type NilLocker

type NilLocker struct{}

NilLocker implements a fake lock that is always obtainable.

It is intended to be used in scenarios where only one instance of Cron is needed.

func NewNilLocker

func NewNilLocker() *NilLocker

func (*NilLocker) Lock

func (l *NilLocker) Lock(job string, ttl time.Duration) (bool, error)

type Options

type Options struct {
	// The location name, which must be "Local", "UTC" or a location name corresponding
	// to a file in the IANA Time Zone database, such as "Asia/Shanghai".
	//
	// Defaults to "UTC".
	Timezone string

	// LockTTL is the time duration after which the successfully obtained lock
	// will be released. It is a time window used to protect a job from
	// being executed more than once per execution time of its schedule,
	// which may be caused by the clock error among different machines.
	//
	// Defaults to 1s.
	LockTTL time.Duration

	// The handler for errors.
	ErrHandler func(error)
}

Jump to

Keyboard shortcuts

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