一、cron定时任务简述
表达式说明如下:
# 文件格式說明 # ┌──分鐘(0 - 59) # │ ┌──小時(0 - 23) # │ │ ┌──日(1 - 31) # │ │ │ ┌─月(1 - 12) # │ │ │ │ ┌─星期(0 - 6,表示从周日到周六) # │ │ │ │ │ # * * * * * 被執行的命令
https://crontab.guru/#0_4_8-14_*_*
二、golang 操作cron发布定时任务
1. 安装cron最新第三方库,和老版本的最大区别在于如果需要秒级定义,需要额外传入参数,并且可以自由配置参数
go get github.com/robfig/cron/v3
2. 示例:
package main import ( "fmt" "github.com/robfig/cron/v3" "time" ) func TestCron() { c := cron.New() i := 1 EntryID, err := c.AddFunc("*/1 * * * *", func() { fmt.Println(time.Now(), "每分钟执行一次", i) i++ }) fmt.Println(time.Now(), EntryID, err) c.Start() time.Sleep(time.Minute * 5) } func main() { TestCron() }
得到结果->
2021-09-06 10:26:32.7499139 +0800 CST m=+0.003000001 1 <nil> 2021-09-06 10:27:00.0009139 +0800 CST m=+27.254000001 每分钟执行一次 1 2021-09-06 10:28:00.0009139 +0800 CST m=+87.254000001 每分钟执行一次 2 2021-09-06 10:29:00.0009139 +0800 CST m=+147.254000001 每分钟执行一次 3 2021-09-06 10:30:00.0009139 +0800 CST m=+207.254000001 每分钟执行一次 4 2021-09-06 10:31:00.0009139 +0800 CST m=+267.254000001 每分钟执行一次 5
3. 相对旧版的改动(常用部分,详细可参考 Go 每日一库之 cron)
3.1. 秒级操作
cron.WithSeconds()
3.2. 函数没执行完就跳过本次函数
cron.WithChain(cron.SkipIfStillRunning(cron.DefaultLogger))
3.3. 打印任务日志
cron.WithLogger(cron.VerbosePrintfLogger(log.New(os.Stdout, "cron: ", log.LstdFlags)))
3.4. 综合上述示例:
package main import ( "fmt" "github.com/robfig/cron/v3" "log" "os" "time" ) func TestCron() { c := cron.New(cron.WithSeconds(), cron.WithChain(cron.SkipIfStillRunning(cron.DefaultLogger)), cron.WithLogger( cron.VerbosePrintfLogger(log.New(os.Stdout, "cron: ", log.LstdFlags)))) i := 1 EntryID, err := c.AddFunc("*/5 * * * * *", func() { fmt.Println(time.Now(), "每5s一次----------------", i) time.Sleep(time.Second *6) i++ }) fmt.Println(time.Now(), EntryID, err) c.Start() time.Sleep(time.Second * 30) } func main() { TestCron() }
time.sleep
2021-09-06 10:55:44.5709139 +0800 CST m=+0.003000001 1 <nil> cron: 2021/09/06 10:55:44 start cron: 2021/09/06 10:55:44 schedule, now=2021-09-06T10:55:44+08:00, entry=1, next=2021-09-06T10:55:45+08:00 cron: 2021/09/06 10:55:45 wake, now=2021-09-06T10:55:45+08:00 cron: 2021/09/06 10:55:45 run, now=2021-09-06T10:55:45+08:00, entry=1, next=2021-09-06T10:55:50+08:00 2021-09-06 10:55:45.0009139 +0800 CST m=+0.433000001 每5s一次---------------- 1 cron: 2021/09/06 10:55:50 wake, now=2021-09-06T10:55:50+08:00 cron: 2021/09/06 10:55:50 run, now=2021-09-06T10:55:50+08:00, entry=1, next=2021-09-06T10:55:55+08:00 cron: 2021/09/06 10:55:55 wake, now=2021-09-06T10:55:55+08:00 cron: 2021/09/06 10:55:55 run, now=2021-09-06T10:55:55+08:00, entry=1, next=2021-09-06T10:56:00+08:00 2021-09-06 10:55:55.0009139 +0800 CST m=+10.433000001 每5s一次---------------- 2 cron: 2021/09/06 10:56:00 wake, now=2021-09-06T10:56:00+08:00 cron: 2021/09/06 10:56:00 run, now=2021-09-06T10:56:00+08:00, entry=1, next=2021-09-06T10:56:05+08:00 cron: 2021/09/06 10:56:05 wake, now=2021-09-06T10:56:05+08:00 cron: 2021/09/06 10:56:05 run, now=2021-09-06T10:56:05+08:00, entry=1, next=2021-09-06T10:56:10+08:00 2021-09-06 10:56:05.0009139 +0800 CST m=+20.433000001 每5s一次---------------- 3 cron: 2021/09/06 10:56:10 wake, now=2021-09-06T10:56:10+08:00 cron: 2021/09/06 10:56:10 run, now=2021-09-06T10:56:10+08:00, entry=1, next=2021-09-06T10:56:15+08:00
AddJob
package main import ( "fmt" "github.com/robfig/cron/v3" "time" ) type Job1 struct { } func (t Job1) Run() { fmt.Println(time.Now(), "I'm Job1") } type Job2 struct { } func (t Job2) Run() { fmt.Println(time.Now(), "I'm Job2") } func TestCron() { c := cron.New(cron.WithSeconds()) EntryID, err := c.AddJob("*/5 * * * * *", Job1{}) fmt.Println(time.Now(), EntryID, err) EntryID, err = c.AddJob("*/10 * * * * *", Job2{}) fmt.Println(time.Now(), EntryID, err) c.Start() time.Sleep(time.Second * 30) } func main() { TestCron() }
得到结果->
go get github.com/robfig/cron/v30