当前位置:网站首页>Go scheduled task cron package usage

Go scheduled task cron package usage

2022-06-13 04:33:00 gohxc

cron What is it?

  cron It means : Planning tasks , To put it bluntly, it's a timed task . I have an appointment with the system , You run a task at what time, minutes, seconds or every few minutes (job), It's that simple .

Download and install

go get github.com/robfig/cron

cron expression   

  cron Expressions are a good thing , This thing not only Java Of quartZ Can use ,Go Language can also be used . I haven't used Linux Of cron, But online says Linux It can also be used crontab -e Command to configure scheduled tasks .Go Language and Java Both are accurate to seconds , however Linux Not in the middle .

cron The expression represents a collection of times , Use 6 Space separated fields represent :

Field name Whether must Allowed value   Allowed specific characters

 second (Seconds)   yes    0-59    * / , -
 branch (Minute)    yes    0-59    * / , -
 when (Hours)     yes    0-23    * / , -
 Japan (Day of month)  yes    1-31    * / , - ?
 month (Month)     yes    1-12  or  JAN-DEC  * / , -
 week (Day of week)  no    0-6  or  SUM-SAT   * / , - ?

notes :
- 1. month (Month) And week (Day of week) Field values are not case sensitive , Such as :SUN、Sun and sun It's the same .
- 2. week (Day of week) If the field is not provided , Equivalent to *
cron Specific character description
  1) asterisk (*)
     Express cron The expression can match all the values of the field . As in the 5 Use an asterisk for each field (month), It means every month
  2) Oblique line (/)
     Represents the growth interval , As the first 1 A field (minutes) The value is 3-59/15, Means the... Of every hour 3 Minutes to start , After every 15 Once per minute ( namely 3、18、33、48 At these points in time ), It can also be expressed here as :3/15
  3) comma (,)
     Used to enumerate values , As the first 6 The first field value is MON,WED,FRI, Express Monday 、 3、 ... and 、 5、 ... and perform
  4) hyphen (-)
     Represents a range , As the first 3 The value of the first field is 9-17 Express 9am To 5pm Directly every hour ( Include 9 and 17)
  5) question mark (?)
     Used only for Japan (Day of month) and week (Day of week), Indicates that no value is specified , Can be used instead of *
    
cron Illustrate with examples

 every other 5 Once per second :*/5 * * * * ?
 every other 1 Once per minute :0 */1 * * * ?
 Every day 23 Click to execute once :0 0 23 * * ?
 Every morning 1 Click to execute once :0 0 1 * * ?
 monthly 1 No. In the morning 1 Click to execute once :0 0 1 1 * ?
 stay 26 branch 、29 branch 、33 Execute once :0 26,29,33 * * * ?
 Daily 0 spot 、13 spot 、18 spot 、21 Do it all once :0 0 0,13,18,21 * * ?

Project use

package main

import (
    "github.com/robfig/cron"
    "log"
)

func main() {
    i := 0
    c := cron.New()
    spec := "*/5 * * * * ?"
    c.AddFunc(spec, func() {
        i++
        log.Println("cron running:", i)
    })
    c.Start()
}
原网站

版权声明
本文为[gohxc]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280522159367.html