xcron

package module
v0.0.0-...-77b38a9 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2024 License: MIT Imports: 9 Imported by: 0

README

xcron

Documentation

Overview

This code is based on work from the cron project

Source: https://github.com/robfig/cron Original author: Rob Figueiredo Licensed under the MIT License: https://opensource.org/licenses/MIT

This code is based on work from the cron project

Source: https://github.com/robfig/cron Original author: Rob Figueiredo Licensed under the MIT License: https://opensource.org/licenses/MIT

This code is based on work from the goim project

Source: https://github.com/Terry-Mao/goim/blob/master/pkg/time/timer.go Original author: Terry.Mao Licensed under the MIT License: https://opensource.org/licenses/MIT

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewEngine

func NewEngine(maxWorkers, rate int, picker Picker) *engine

func WithEngine

func WithEngine(ec EngineCreator) *cronOption

func WithHighLevel

func WithHighLevel() *scheduleOption

func WithID

func WithID(id EntryID) *scheduleOption

func WithJobWrapper

func WithJobWrapper(jobWrapper func(Schedule, Picker, Job) (Job, CancelHandler)) *scheduleOption

func WithLocation

func WithLocation(loc *time.Location) *cronOption

func WithLowLevel

func WithLowLevel() *scheduleOption

func WithParser

func WithParser(sp ScheduleParser) *cronOption

func WithPicker

func WithPicker(pc PickerCreator) *cronOption

Types

type CancelHandler

type CancelHandler func()

type ConstantDelaySchedule

type ConstantDelaySchedule struct {
	Delay time.Duration
}

func Every

func Every(duration time.Duration) ConstantDelaySchedule

func (ConstantDelaySchedule) Next

func (schedule ConstantDelaySchedule) Next(t time.Time) time.Time

type Cron

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

func NewCron

func NewCron(opt ...CronOption) *Cron

func (*Cron) AddFunc

func (c *Cron) AddFunc(spec string, cmd func(), opt ...ScheduleOption) (EntryID, error)

AddFunc adds a func to the Cron to be run on the given schedule. The spec is parsed using the time zone of this Cron instance as the default. An ID is returned that can be used to later remove it.

func (*Cron) AddJob

func (c *Cron) AddJob(spec string, cmd Job, opt ...ScheduleOption) (EntryID, error)

AddJob adds a Job to the Cron to be run on the given schedule. The spec is parsed using the time zone of this Cron instance as the default. An ID is returned that can be used to later remove it.

func (*Cron) Entry

func (c *Cron) Entry(id EntryID) *Entry

Entry returns a snapshot of the given entry, or nil if it couldn't be found.

func (*Cron) Location

func (c *Cron) Location() *time.Location

Location gets the time zone location

func (*Cron) Remove

func (c *Cron) Remove(id EntryID)

func (*Cron) Schedule

func (c *Cron) Schedule(schedule Schedule, cmd Job, opt ...ScheduleOption) EntryID

func (*Cron) Start

func (m *Cron) Start() error

func (*Cron) Stop

func (m *Cron) Stop() (context.Context, error)

type CronOption

type CronOption interface {
	// contains filtered or unexported methods
}

type CronOptions

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

type Engine

type Engine interface {
	Start() error
	Stop() (context.Context, error)
}

type EngineCreator

type EngineCreator func(Picker) Engine

type Entry

type Entry struct {
	Job
	// contains filtered or unexported fields
}

type EntryID

type EntryID string

EntryID identifies an entry within a Cron instance

type FuncJob

type FuncJob func()

FuncJob is a wrapper that turns a func() into a Job

func (FuncJob) Run

func (f FuncJob) Run()

type Job

type Job interface {
	Run()
}

type JobLevel

type JobLevel interface {
	Pick() <-chan Job
	Remove(*JobLevelWrapper)
	Add(*JobLevelWrapper)
	Redo(*JobLevelWrapper)
}

type JobLevelWrapper

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

func (*JobLevelWrapper) Run

func (w *JobLevelWrapper) Run()

type JobPicker

type JobPicker interface {
	Picker
	Start() error
	Stop() error
}

type LevelPicker

type LevelPicker struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewLevelPicker

func NewLevelPicker(loc *time.Location) *LevelPicker

func (*LevelPicker) Add

func (lp *LevelPicker) Add(j Job, fn func()) error

func (*LevelPicker) Del

func (lp *LevelPicker) Del(j Job)

func (*LevelPicker) PickSize

func (lp *LevelPicker) PickSize(ctx context.Context, size int) ([]Job, int)

func (*LevelPicker) Start

func (lp *LevelPicker) Start() error

func (*LevelPicker) Stop

func (lp *LevelPicker) Stop() error

type ParseOption

type ParseOption int

Configuration options for creating a parser. Most options specify which fields should be included, while others enable features. If a field is not included the parser will assume a default value. These options do not change the order fields are parse in.

const (
	Second         ParseOption = 1 << iota // Seconds field, default 0
	SecondOptional                         // Optional seconds field, default 0
	Minute                                 // Minutes field, default 0
	Hour                                   // Hours field, default 0
	Dom                                    // Day of month field, default *
	Month                                  // Month field, default *
	Dow                                    // Day of week field, default *
	DowOptional                            // Optional day of week field, default *
	Descriptor                             // Allow descriptors such as @monthly, @weekly, etc.
)

type Parser

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

A custom Parser that can be configured.

func NewParser

func NewParser(options ParseOption) Parser

NewParser creates a Parser with custom options.

It panics if more than one Optional is given, since it would be impossible to correctly infer which optional is provided or missing in general.

Examples

// Standard parser without descriptors
specParser := NewParser(Minute | Hour | Dom | Month | Dow)
sched, err := specParser.Parse("0 0 15 */3 *")

// Same as above, just excludes time fields
specParser := NewParser(Dom | Month | Dow)
sched, err := specParser.Parse("15 */3 *")

// Same as above, just makes Dow optional
specParser := NewParser(Dom | Month | DowOptional)
sched, err := specParser.Parse("15 */3")

func (Parser) Parse

func (p Parser) Parse(spec string) (Schedule, error)

Parse returns a new crontab schedule representing the given spec. It returns a descriptive error if the spec is not valid. It accepts crontab specs and features configured by NewParser.

type Picker

type Picker interface {
	PickSize(ctx context.Context, size int) ([]Job, int)
}

type PickerCreator

type PickerCreator func() JobPicker

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
}

Schedule describes a job's duty cycle.

func ParseStandard

func ParseStandard(standardSpec string) (Schedule, error)

ParseStandard returns a new crontab schedule representing the given standardSpec (https://en.wikipedia.org/wiki/Cron). It requires 5 entries representing: minute, hour, day of month, month and day of week, in that order. It returns a descriptive error if the spec is not valid.

It accepts

  • Standard crontab specs, e.g. "* * * * ?"
  • Descriptors, e.g. "@midnight", "@every 1h30m"

type ScheduleOption

type ScheduleOption interface {
	// contains filtered or unexported methods
}

type ScheduleOptions

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

type ScheduleParser

type ScheduleParser interface {
	Parse(spec string) (Schedule, error)
}

ScheduleParser is an interface for schedule spec parsers that return a Schedule

type SpecSchedule

type SpecSchedule struct {
	Second, Minute, Hour, Dom, Month, Dow 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.

type Timer

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

Timer timer.

func NewTimer

func NewTimer(num int, ch chan Job) (t *Timer)

NewTimer new a timer. A heap must be initialized before any of the heap operations can be used. Init is idempotent with respect to the heap invariants and may be called whenever the heap invariants may have been invalidated. Its complexity is O(n) where n = h.Len().

func (*Timer) Add

func (t *Timer) Add(expire time.Time, job Job) (td *TimerData)

Add add the element x onto the heap. The complexity is O(log(n)) where n = h.Len().

func (*Timer) Del

func (t *Timer) Del(td *TimerData)

Del removes the element at index i from the heap. The complexity is O(log(n)) where n = h.Len().

func (*Timer) Init

func (t *Timer) Init(num int, ch chan Job)

Init init the timer.

func (*Timer) Set

func (t *Timer) Set(td *TimerData, expire time.Time)

Set update timer data.

type TimerData

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

TimerData timer data.

func (*TimerData) Delay

func (td *TimerData) Delay() time.Duration

Delay delay duration.

func (*TimerData) ExpireString

func (td *TimerData) ExpireString() string

ExpireString expire string.

Jump to

Keyboard shortcuts

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