1. The basic format of cron expression
I have used crontab in Linux, and I should have some understanding of scheduled tasks. In clinux, use crontab -e to open the crontab table to add scheduled tasks. The expression is as follows:
┌─────────────second 范围 (0 - 60) │ ┌───────────── min (0 - 59) │ │ ┌────────────── hour (0 - 23) │ │ │ ┌─────────────── day of month (1 - 31) │ │ │ │ ┌──────────────── month (1 - 12) │ │ │ │ │ ┌───────────────── day of week (0 - 6) (0 to 6 are Sunday to │ │ │ │ │ │ Saturday) │ │ │ │ │ │ │ │ │ │ │ │ * * * * * *
2. Special characters
Asterisk: matches all values
Slash: Increase interval, * / 5 means 5 10 15 .... minutes running
Comma: Enumeration value, 1,13,21 means 1 13 21 minutes running
Hyphen: means range, 10-15 means 10 11 12 13 15 minutes running
Question mark: only for day and week, it means not specified, * can be substituted
3. Cron design ideas in go
// cron entity type Cron struct { entries [] * Entry stop chan struct {} // Control Cron instance to suspend add chan * Entry // When Cron is already running, add a new Entity, snapshot chan is realized by adding the channel [] * Entry // Get a snapshot of all current entities running bool // Whether it is running ... } // Scheduling entity type Entry struct { // Responsible for scheduling the Job in the current Entity Schedule Schedule // Job Next time the implementation of the Next time.time //Job last execution time Prev time.Time // Job Job Job to be executed } // Each test question contains a Job type Job interface { Run () } // Implement the Job interface type FuncJob func () // By simple Run () to implement job func (f FuncJob) Run () {f ()} // each entity contains a scheduler type Schedule interface { // return the next execution time of the job in the same Entity Next (time. Time) time.Time } type SpecSchedule struct { Second, Minute, Hour, Dom, Month, Dow uint64 } type ConstantDelaySchedule struct{ Delay time.Duration // Cycle time interval, the minimum unit is seconds } // Instantiate Cron func New () * Cron { return & Cron { entries: nil, add: make (chan * Entry), stop: make (chan struct {}), snapshot: make (chan [] * Entry), running: false , } } func Parse (spec string ) (_ Schedule, err error) // Add job to Cron, this method is simply through FuncJob Type cast cmd, then call AddJob method func (c * Cron) AddFunc (spec string, cmd func ()) error // Add job to Cron, parse the cron expression spec to the scheduling instance (Schedule) through Parse function, and then call the c.Schedule method func (c * Cron) AddJob (spec string , cmd Job ) error // Get a snapshot of all Entities in the current Cron func (c * Cron) Entries () [] * Entry // Instantiate an Entity with two parameters, and then join the current Cron, if the current Cron is not running, then Directly add the Entity to the Cron // otherwise add the entity to the running Cron through the add channel func (c * Cron) Schedule (schedule Schedule, cmd Job) // start a new goroutine to run the current Cron func (c * Cron) Start () // Stop the current Cron by sending a struct {} {} to the stop member, and set running to false at the same time, // It can be seen from this that stop is only to notify Cron to stop, so send a channel Value, do n’t care about the value, //So stop in Cron is an empty structure func (c * Cron) Stop ()
4. Examples:
package main import ( " log " " github.com/robfig/cron " ) // crontab in Linux can only be accurate to the minute, and func newWithSecond () in seconds at go is supported in go * cron.Cron { secondParser: = cron.NewParser ( cron.Second | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.DowOptional | cron.Descriptor) return cron.New (cron.WithParser (secondParser), cron.WithChain ()) } type H struct { C * cron.Cron } func (h H) Run () { entries: = hCEntries () if len(entries) > 0 { log.Println("---------cron running entries:", len(entries), ", next time:", entries[1].Next, ", pre time:", entries[1].Prev) } } func main() { i := 0 c := newWithSecond() spec := "*/5 * * * * *" entryId, err := c.AddFunc(spec, func() { i++ log.Println("cron running:", i) if i >= 1 { entries := c.Entries() log.Println("cron running entries:", len(entries), ", next time:", entries[0].Next, ", pre time:", entries[0].Prev) } }) log.Println("entryId:", entryId, " err:", err) s, err := cron.ParseStandard("*/1 * * * * ") if err != nil { log.Println(err) } h := H{c} c.Schedule(s, h) c.Start() select {} }
Reference address: https://www.jianshu.com/p/fd3dda663953 , http://www.luyixian.cn/news_show_232522.aspx