micron

package module
v0.0.0-...-18343cd Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2022 License: MIT Imports: 10 Imported by: 4

README

micron

A minimal implementation of the fault-tolerant job scheduler.

Features

  1. Fault-tolerant

    No single point of failure, just setup multiple Cron instances on different machines.

  2. Exactly-once

    The same job, although scheduled simultaneously by multiple Cron instances, is guaranteed to be executed exactly once per execution time of its schedule.

    This guarantee is provided by leveraging distributed locks. The example implementation is based on Redis with a single instance. For higher safety and reliability, you can use other Raft-based implementations of distributed locks (e.g. Consul, etcd).

  3. Embeddable

    No need to run standalone binaries, you can just embed Cron instances into your existing application, which may consist of one or multiple nodes.

Installation

$ go get -u github.com/RussellLuo/micron

Example

See example.

Documentation

Checkout the Godoc.

License

MIT

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. Three formats are supported currently:
	//
	// 1. Fixed-interval schedule:
	//
	//		@every <duration>
	//
	//	where `<duration>` is a string accepted by `time.ParseDuration` (http://golang.org/pkg/time/#ParseDuration).
	//
	// 2. Predefined cron expression (https://github.com/gorhill/cronexpr#predefined-cron-expressions):
	//
	//		@annually
	//		@yearly
	//		@monthly
	//		@weekly
	//		@daily
	//		@hourly
	//
	// 3. Standard cron expression (https://github.com/gorhill/cronexpr#implementation).
	//
	// Note that the execution interval of the job must be greater than LockTTL.
	Expr string

	// The old-style handler of the job.
	Task func()

	// The new-style handler of the job.
	//
	// Note that Handler will be preferred if both Task and Handler are specified.
	Handler func(context.Context) error
}

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)
}

type Schedule

type Schedule interface {
	Next(time.Time) time.Time
}

func Every

func Every(d time.Duration) Schedule

Every returns a Schedule that will activate once every duration. Duration d will be truncated down to a multiple of one second. If the truncated result is zero, the final duration will be set to one second.

func MustParse

func MustParse(expr string) Schedule

func Parse

func Parse(expr string) (Schedule, error)

type SemaphoreLocker

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

SemaphoreLocker implements a lock for in-process mutual exclusion.

It is intended to be used in scenarios where multiple instances of Cron are run in the same process.

func NewSemaphoreLocker

func NewSemaphoreLocker() *SemaphoreLocker

func (*SemaphoreLocker) Lock

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

Directories

Path Synopsis
cron module
redislocker module

Jump to

Keyboard shortcuts

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