cron

package module
v0.0.0-...-39a731f Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2019 License: MIT Imports: 11 Imported by: 0

README

go-cron

GoDoc CircleCI Go Report Card

configurable cronjobs with persistence

See the godocs for documentation.

This library is heavily inspired/borrows from https://github.com/robfig/cron. The main reason for it's creation was the need to serialize and store jobs, thus closures could not be used.

Documentation

Overview

Package cron is a cron manager partially based on github.com/robfig/cron (same parser semantics and scheduling logic). It adds more fine-grained control on running jobs, such as cancellations, as well as persistent storage of entries.

Usage Callers may register routines to be invoked on a given schedule. Cron will run them in their own goroutines. Package cron provides a global scheduler, as well as constructors for cron.Executor objects.

routine := func(ctx context.Context, args map[string]interface{}) error {
	fmt.Println("called", time.Now())
}
cron.Register("printroutine", routine)

// Start the global executor
cron.Start()

// Add an entry to our global crontab
cron.Add(cron.Entry{
	ID:         "myjob",
	Expression: "@every 1s",
	Job:        "printroutine",
	})

CRON Expression Format A cron expression represents a set of times, using 6 space-separated fields.

Field name   | Mandatory? | Allowed values  | Allowed special characters
----------   | ---------- | --------------  | --------------------------
Seconds      | Yes        | 0-59            | * / , -
Minutes      | Yes        | 0-59            | * / , -
Hours        | Yes        | 0-23            | * / , -
Day of month | Yes        | 1-31            | * / , - ?
Month        | Yes        | 1-12 or JAN-DEC | * / , -
Day of week  | Yes        | 0-6 or SUN-SAT  | * / , - ?

Note: Month and Day-of-week field values are case insensitive. "SUN", "Sun", and "sun" are equally accepted. Special Characters Asterisk ( * ) The asterisk indicates that the cron expression will match for all values of the field; e.g., using an asterisk in the 5th field (month) would indicate every month. Slash ( / ) Slashes are used to describe increments of ranges. For example 3-59/15 in the 1st field (minutes) would indicate the 3rd minute of the hour and every 15 minutes thereafter. The form "*\/..." is equivalent to the form "first-last/...", that is, an increment over the largest possible range of the field. The form "N/..." is accepted as meaning "N-MAX/...", that is, starting at N, use the increment until the end of that specific range. It does not wrap around. Comma ( , ) Commas are used to separate items of a list. For example, using "MON,WED,FRI" in the 5th field (day of week) would mean Mondays, Wednesdays and Fridays. Hyphen ( - ) Hyphens are used to define ranges. For example, 9-17 would indicate every hour between 9am and 5pm inclusive. Question mark ( ? ) Question mark may be used instead of '*' for leaving either day-of-month or day-of-week blank. Predefined schedules You may use one of several pre-defined schedules in place of a cron expression.

Entry                  | Description                                | Equivalent To
-----                  | -----------                                | -------------
@yearly (or @annually) | Run once a year, midnight, Jan. 1st        | 0 0 0 1 1 *
@monthly               | Run once a month, midnight, first of month | 0 0 0 1 * *
@weekly                | Run once a week, midnight between Sat/Sun  | 0 0 0 * * 0
@daily (or @midnight)  | Run once a day, midnight                   | 0 0 0 * * *
@hourly                | Run once an hour, beginning of hour        | 0 0 * * * *

Intervals You may also schedule a job to execute at fixed intervals, starting at the time it's added or cron is run. This is supported by formatting the cron spec like this:

@every <duration>

where "duration" is a string accepted by time.ParseDuration (http://golang.org/pkg/time/#ParseDuration). For example, "@every 1h30m10s" would indicate a schedule that activates after 1 hour, 30 minutes, 10 seconds, and then every interval after that. Note: The interval does not take the job runtime into account. For example, if a job takes 3 minutes to run, and it is scheduled to run every 5 minutes, it will have only 2 minutes of idle time between each run. Time zones All interpretation and scheduling is done in the machine's local time zone (as provided by the Go time package (http://www.golang.org/pkg/time) by default. Setting an entries location will override the default location. Be aware that jobs scheduled during daylight-savings leap-ahead transitions will not be run! Thread safety Since the Cron service runs concurrently with the calling code, some amount of care must be taken to ensure proper synchronization. All cron methods are designed to be correctly synchronized as long as the caller ensures that invocations have a clear happens-before ordering between them. Storage Entries may be persistently stored by writing an implementation for the Tab interface.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrJobExists is returned by the executor instance if the provided job ID is present in the registry.
	ErrJobExists = errors.New("job already registered")
	// ErrJobNotExists is returned/used by the executor if a job is not registered but called/queried.
	ErrJobNotExists = errors.New("job not registered")

	// DefaultLocation is the default locale of the executor.
	DefaultLocation = time.UTC
)

Functions

func Add

func Add(entry Entry) error

Add a new entry to the global crontab

func Cancel

func Cancel(ID string)

Cancel a specific entry in the global executor while running. If it is not running, Cancel is a noop.

func CancelAll

func CancelAll()

CancelAll stops all running cronjobs

func CancelFromContext

func CancelFromContext(ctx context.Context) context.CancelFunc

CancelFromContext returns the cancel func.

func Configure

func Configure(opts ...Option) error

Configure the global executor.

func ContextRunning

func ContextRunning(ctx context.Context) bool

ContextRunning returns the job running state.

func ContextStart

func ContextStart(ctx context.Context) context.Context

ContextStart sets the state of the job ctx to running. If already started, Start is a noop.

func ContextWithCancel

func ContextWithCancel(ctx context.Context, cfunc context.CancelFunc) context.Context

ContextWithCancel sets the cancel func in a context.

func ContextWithEntry

func ContextWithEntry(ctx context.Context, e Entry) context.Context

ContextWithEntry sets the job Entry.

func ContextWithReport

func ContextWithReport(ctx context.Context, rep string) context.Context

ContextWithReport sets the job report.

func Err

func Err() chan error

Err returns the global executor error channel

func GetLog

func GetLog() chan Log

GetLog returns the global executor log channel

func IsRunning

func IsRunning() bool

IsRunning returns true if the global executor is running.

func Location

func Location() time.Location

Location returns the global exectutor timezone locale

func NewContext

func NewContext(ctx context.Context, entry Entry) (context.Context, context.CancelFunc)

NewContext creates a cron Context from existing contexts and the entry. The contexts defaults to a running status of False.

func Register

func Register(ID string, routine Routine) error

Register a routine with the global executor.

func Remove

func Remove(ID string) error

Remove an entry from the global executor tab. Does not cancel running job nor removes it from the current queue.

func ReportFromContext

func ReportFromContext(ctx context.Context) string

ReportFromContext returns the job report.

func Start

func Start() error

Start the global executor.

func Stop

func Stop()

Stop the global cron executor.

func StopAll

func StopAll()

StopAll stops the global cron executor and all running cronjobs.

Types

type ByTimeAsc

type ByTimeAsc []*Entry

ByTimeAsc defines ordering for []*Entry

func (ByTimeAsc) Len

func (b ByTimeAsc) Len() int

func (ByTimeAsc) Less

func (b ByTimeAsc) Less(i, j int) bool

func (ByTimeAsc) RemoveCons

func (b ByTimeAsc) RemoveCons(i int) ByTimeAsc

RemoveCons removes the ith element in constant time, does not preserves order

func (ByTimeAsc) RemoveLin

func (b ByTimeAsc) RemoveLin(i int) ByTimeAsc

RemoveLin removes the ith element in linear time, preserves order

func (ByTimeAsc) Swap

func (b ByTimeAsc) Swap(i, j int)

func (ByTimeAsc) Unique

func (b ByTimeAsc) Unique() ByTimeAsc

Unique ensures all items are unique by removing entries with duplicate IDs, this kills the ordering.

type ConstantDelaySchedule

type ConstantDelaySchedule struct {
	Delay time.Duration
}

ConstantDelaySchedule represents a simple recurring duty cycle, e.g. "Every 5 minutes". It does not support jobs more frequent than once a second.

func Every

func Every(duration time.Duration) ConstantDelaySchedule

Every returns a crontab Schedule that activates once every duration. Delays of less than a second are not supported (will round up to 1 second). Any fields less than a Second are truncated.

func (ConstantDelaySchedule) Next

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

Next returns the next time this should be run. This rounds so that the next activation time will be on the second.

type Entry

type Entry struct {
	// Globally unique ID
	ID string

	// cron expression
	Expression string

	// Routine-identifier to be executed
	Routine string

	// Arguments passed to the Job. Arguments should be JSON serializeable if a persistent store is used.
	Args map[string]interface{}

	// Location overrides the executors location if provided. Not required.
	Location *time.Location

	Policy RunPolicy

	NextRun     time.Time
	PreviousRun time.Time
}

Entry specifies a single (crontab) entry, a function which is executed periodically.

func EntryFromContext

func EntryFromContext(ctx context.Context) Entry

EntryFromContext returns the job entry.

func (Entry) MustNext

func (e Entry) MustNext(t time.Time) time.Time

MustNext calls Next and panics if an error is returned.

func (Entry) Next

func (e Entry) Next(t time.Time) (time.Time, error)

Next returns the time when the entry should be scheduled next.

If the entry has it's own location,

type ErrCronFailure

type ErrCronFailure error

ErrCronFailure indicates the cron executor should cease running.

type ErrNotFound

type ErrNotFound error

type ErrPermanentFailure

type ErrPermanentFailure error

ErrPermanentFailure indicates the cron should be removed from the schedule.

type ErrRepeatNextCron

type ErrRepeatNextCron error

ErrRepeatNextCron indicates the job may be retried when the schedule allows.

type ErrRepeatable

type ErrRepeatable error

ErrRepeatable indicates the job should be retried immediately.

type Executor

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

Executor is the godlevel struct.

func New

func New(opts ...Option) (*Executor, error)

New is the constructor for executor

func (*Executor) Add

func (e *Executor) Add(entry Entry) error

Add a cron entry to the executor

func (*Executor) Cancel

func (e *Executor) Cancel(ID string) error

Cancel a specific entry while running. If it is not running, Cancel returns ErrNotFound

func (*Executor) CancelAll

func (e *Executor) CancelAll()

CancelAll stops all running cronjobs

func (*Executor) Err

func (e *Executor) Err() chan error

Err returns the error channel

func (*Executor) IsRunning

func (e *Executor) IsRunning() bool

IsRunning returns true if the executor is running.

func (*Executor) Location

func (e *Executor) Location() time.Location

Location returns the exectutor timezone locale

func (*Executor) Log

func (e *Executor) Log() chan Log

Log returns the log channel

func (*Executor) Refresh

func (e *Executor) Refresh() <-chan Update

Refresh blocks on the refresh channel,which returns updates on the internal executor queue

func (*Executor) Register

func (e *Executor) Register(ID string, routine Routine) error

Register a routine with the global executor.

func (*Executor) Remove

func (e *Executor) Remove(ID string) error

Remove an entry from the tab. Does not cancel running job nor removes it from the current queue.

func (*Executor) Start

func (e *Executor) Start() error

Start the executor. Function registry should have completed before calling start.

func (*Executor) Stop

func (e *Executor) Stop()

Stop the cron executor.

func (*Executor) StopAll

func (e *Executor) StopAll()

StopAll stops the cron executor and all running cronjobs.

type Log

type Log struct {
	ID      string
	Entry   Entry
	Started time.Time
	Ended   time.Time
	Report  string
	Err     error
}

Log is emitted after a job has run

type MemoryTab

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

MemoryTab is a simple storage backend.

func NewMemoryTab

func NewMemoryTab() *MemoryTab

NewMemoryTab returns an in-memory Tab. This is a non-persistent storage.

func (*MemoryTab) All

func (m *MemoryTab) All() (map[string]*Entry, error)

All returns all entries from the tab.

func (*MemoryTab) Clear

func (m *MemoryTab) Clear() error

Clear deletes all entries from the tab.

func (*MemoryTab) Put

func (m *MemoryTab) Put(e Entry) error

Put overrides an entry in the tab.

func (*MemoryTab) Refresh

func (m *MemoryTab) Refresh() <-chan Update

Refresh returns update on the tab contents

func (*MemoryTab) Remove

func (m *MemoryTab) Remove(ID string) error

Remove deletes an entry from the tab.

func (*MemoryTab) Update

func (m *MemoryTab) Update(ID string, e Entry) error

Update upserts an entry

type Option

type Option func(*Executor) error

Option is a constructor function

func WithError

func WithError(errors chan error) Option

WithError sets an error channel

func WithLocation

func WithLocation(location time.Location) Option

WithLocation sets the location.

Location defaults to time.Local.

func WithLog

func WithLog(log chan Log) Option

WithLog sets a log channel

func WithTab

func WithTab(tab Tab) Option

WithTab sets a storage backend for the executor

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

Creates a custom Parser with custom options.

// 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
subsParser := NewParser(Dom | Month | Dow)
sched, err := specParser.Parse("15 */3 *")

// Same as above, just makes Dow optional
subsParser := 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 Routine

type Routine func(ctx context.Context, args map[string]interface{}) error

Routine is a registerable function. Passed arguments are obtained from the Entry.

type RunPolicy

type RunPolicy int

RunPolicy defines running behaviour of an entry.

const (
	// RunParallel runs an entry while ignoring other running instances.
	RunParallel RunPolicy = 0

	// CancelRunning cancels previously running entries before starting.
	CancelRunning RunPolicy = 1

	// SingleInstanceOnly cancels previously running entries, awaits cancellation confirmation before starting.
	SingleInstanceOnly RunPolicy = 2

	// SkipIfRunning ignores the entry if another instance is currently running
	SkipIfRunning RunPolicy = 3
)

type Schedule

type Schedule interface {
	// Return 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
}

The Schedule describes a job's duty cycle.

func Parse

func 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

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

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 differs from Parse requiring to always pass 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 SpecSchedule

type SpecSchedule struct {
	Second, Minute, Hour, Dom, Month, Dow uint64
}

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 Tab

type Tab interface {
	// Stores a new entry
	Put(Entry) error

	// Returns all entries
	All() (map[string]*Entry, error)

	// Removes a single entry
	Remove(ID string) error

	// Update a single entry.
	//
	// ID is the ID of the current entry. If the ID does not match the provided entry.ID, the ID will be updated as well.
	Update(ID string, entry Entry) error

	// Clears all entries
	Clear() error

	// Refresh provides any new or updated entries. The executor selects on Refresh to update it's own internal queue.
	//
	// Do not read of Tab.Refresh as package user; instead listen on the Refresh method of executor if you wish to obtain
	// updated/new entries.
	Refresh() <-chan Update
}

Tab (crontab is short for cron table) defines the storage backend for the scheduler.

type Update

type Update struct {
	UpdateType
	Entry
}

type UpdateType

type UpdateType int
const (
	NewAltered UpdateType = 0
	Deleted    UpdateType = 1
)

Jump to

Keyboard shortcuts

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