Documentation
¶
Overview ¶
Package cron implements a crontab-like service to execute and schedule repeative tasks/jobs.
Example:
c := cron.New()
c.MustAdd("dailyReport", "0 0 * * *", func() { ... })
c.Start()
Index ¶
- type ClaimFunc
- type Cron
- func (c *Cron) Add(jobId string, cronExpr string, fn func()) error
- func (c *Cron) HasStarted() bool
- func (c *Cron) Jobs() []*Job
- func (c *Cron) MustAdd(jobId string, cronExpr string, run func())
- func (c *Cron) Remove(jobId string)
- func (c *Cron) RemoveAll()
- func (c *Cron) SetClaimFunc(fn ClaimFunc)
- func (c *Cron) SetInterval(d time.Duration)
- func (c *Cron) SetTimezone(l *time.Location)
- func (c *Cron) Start()
- func (c *Cron) Stop()
- func (c *Cron) Total() int
- type Job
- type Moment
- type Schedule
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ClaimFunc ¶
ClaimFunc is called before running a scheduled job.
slot is the scheduled tick time (truncated to the cron interval) and it is the same for all instances with the same schedule.
The job execution is skipped if it returns false (eg. because the job was already executed for the same slot by another app instance).
type Cron ¶
type Cron struct {
// contains filtered or unexported fields
}
Cron is a crontab-like struct for tasks/jobs scheduling.
func New ¶
func New() *Cron
New create a new Cron struct with default tick interval of 1 minute and timezone in UTC.
You can change the default tick interval with Cron.SetInterval(). You can change the default timezone with Cron.SetTimezone().
func (*Cron) Add ¶
Add registers a single cron job.
If there is already a job with the provided id, then the old job will be replaced with the new one.
cronExpr is a regular cron expression, eg. "0 */3 * * *" (aka. at minute 0 past every 3rd hour). Check cron.NewSchedule() for the supported tokens.
func (*Cron) HasStarted ¶
HasStarted checks whether the current Cron ticker has been started.
func (*Cron) SetClaimFunc ¶
SetClaimFunc registers an optional function that is called before running each scheduled job (see ClaimFunc).
It is not called for the jobs that are executed manually with Job.Run.
func (*Cron) SetInterval ¶
SetInterval changes the current cron tick interval (it usually should be >= 1 minute).
func (*Cron) SetTimezone ¶
SetTimezone changes the current cron tick timezone.
func (*Cron) Start ¶
func (c *Cron) Start()
Start starts the cron ticker.
Calling Start() on already started cron will restart the ticker.
type Job ¶
type Job struct {
// contains filtered or unexported fields
}
Job defines a single registered cron job.
func (*Job) Expression ¶
Expression returns the plain cron job schedule expression.
func (Job) MarshalJSON ¶
MarshalJSON implements json.Marshaler and export the current jobs data into valid JSON.
type Moment ¶
type Moment struct {
Minute int `json:"minute"`
Hour int `json:"hour"`
Day int `json:"day"`
Month int `json:"month"`
DayOfWeek int `json:"dayOfWeek"`
}
Moment represents a parsed single time moment.
type Schedule ¶
type Schedule struct {
Minutes map[int]struct{} `json:"minutes"`
Hours map[int]struct{} `json:"hours"`
Days map[int]struct{} `json:"days"`
Months map[int]struct{} `json:"months"`
DaysOfWeek map[int]struct{} `json:"daysOfWeek"`
// contains filtered or unexported fields
}
Schedule stores parsed information for each time component when a cron job should run.
func NewSchedule ¶
NewSchedule creates a new Schedule from a cron expression.
A cron expression could be a macro OR 5 segments separated by space, representing: minute, hour, day of the month, month and day of the week.
The following segment formats are supported:
- wildcard: *
- range: 1-30
- step: */n or 1-30/n
- list: 1,2,3,10-20/n
The following macros are supported:
- @yearly (or @annually)
- @monthly
- @weekly
- @daily (or @midnight)
- @hourly