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 ¶
- Constants
- type Cron
- func (c *Cron) Add(jobId string, cronExpr string, run func()) error
- func (c *Cron) HasStarted() bool
- func (c *Cron) MustAdd(jobId string, cronExpr string, run func())
- func (c *Cron) Remove(jobId string)
- func (c *Cron) RemoveAll()
- 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 Moment
- type Schedule
Constants ¶
const ( Yearly = "@yearly" Annually = "@annually" Monthly = "@monthly" Weekly = "@weekly" Daily = "@daily" Midnight = "@midnight" Hourly = "@hourly" Minutely = "@minutely" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cron ¶
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) 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 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"` }
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
- @minutely