GO定时任务CRON执行不成功?看一下这篇文章就明白了

背景描述

碰到一个需求,需要起一个定时任务,由于最近在熟悉go语言,所以想用go来实现这个需求。

搜索go定时任务框架,官方推荐的框架是cron,文档地址是https://godoc.org/github.com/robfig/cron

官方示例如下

Usage Callers may register Funcs to be invoked on a given schedule. Cron will run them in their own goroutines.

1c := cron.New() 2c.AddFunc("30 * * * *", func() { fmt.Println("Every hour on the half hour") }) 3c.AddFunc("30 3-6,20-23 * * *", func() { fmt.Println(".. in the range 3-6am, 8-11pm") }) 4c.AddFunc("CRON_TZ=Asia/Tokyo 30 04 * * *", func() { fmt.Println("Runs at 04:30 Tokyo time every day") }) 5c.AddFunc("@hourly", func() { fmt.Println("Every hour, starting an hour from now") }) 6c.AddFunc("@every 1h30m", func() { fmt.Println("Every hour thirty, starting an hour thirty from now") }) 7c.Start() 8.. 9// Funcs are invoked in their own goroutine, asynchronously. 10... 11// Funcs may also be added to a running Cron 12c.AddFunc("@daily", func() { fmt.Println("Every day") }) 13.. 14// Inspect the cron job entries' next and previous run times. 15inspect(c.Entries()) 16.. 17c.Stop() // Stop the scheduler (does not stop any jobs already running). 18

问题描述

按照官方示例,写了以下demo,测试定时任务是否可以运行成功

1package main 2 3import ( 4 "fmt" 5 6 "github.com/robfig/cron/v3" 7) 8 9func main() { 10 // 每隔3秒执行一次:*/3 * * * * * 11 spec := "*/3 * * * * *" 12 c := cron.New() 13 c.AddFunc(spec, func() { 14 fmt.Println("每隔3秒执行一次") 15 }) 16 go c.Start() 17 defer c.Stop() 18 select {} 19} 20

释: select 是 Go 中的一个控制结构,类似于用于通信的 switch 语句。每个 case 必须是一个通信操作,要么是发送要么是接收。 select 随机执行一个可运行的 case。如果没有 case 可运行,它将阻塞,直到有 case 可运行。一个默认的子句应该总是可运行的。

运行上述代码,发现定时任务并没有每隔3s打印输出一次

crond 表达式解释,如下图

image

解决方案

这是因为"github.com/robfig/cron/v3"与"github.com/robfig/cron"包的差异造成的,实际上在v3的文档中有以下描述

Since adding Seconds is the most common modification to the standard cron spec, cron provides a builtin function to do that, which is equivalent to the custom parser you saw earlier, except that its seconds field is REQUIRED:

cron.New(cron.WithSeconds())

说明cron v3版本默认已经不再是支持到秒级别的定时了。

解决方案一:

使用老版本的cron包

1package main 2 3import ( 4 "fmt" 5 6 "github.com/robfig/cron" 7) 8 9func main() { 10 // 每隔3秒执行一次:*/3 * * * * * 11 spec := "*/3 * * * * *" 12 c := cron.New() 13 c.AddFunc(spec, func() { 14 fmt.Println("每隔3秒执行一次") 15 }) 16 go c.Start() 17 defer c.Stop() 18 select {} 19} 20 21

解决方案二:

使用新版本的cron包,但是要cron.New(cron.WithSeconds())方法

1package main 2 3import ( 4 "fmt" 5 6 "github.com/robfig/cron/v3" 7) 8 9func main() { 10 // 每天凌晨0点执行一次:0 0 0 * * ? 11 // 每隔3秒执行一次:*/3 * * * * * 12 // spec := "*/3 * * * * *" 13 14 spec := "* * * * *" 15 c := cron.New(cron.WithSeconds()) 16 c.AddFunc(spec, func() { 17 fmt.Println("execute") 18 }) 19 go c.Start() 20 defer c.Stop() 21 select {} 22} 23
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

Jenkins定时构建cron语法

文章目录Jenkins定时构建cron语法Jenkins定时构建cron语法Jenkins定时构建采用cron语法。常见例子:\每1分钟\/1\\\\\每5分钟H/5\\\\\每30分钟H/30\\\\\每1小时H\/1

Linux服务器时间同步命令

时间同步1.首先需了解linux内一任务计划工具crontabcrontab可以定时去执行你要做的动作直接用crontab命令编辑crontabu//设定某个用户的cron服务,一般root用户在执行这个命令的时候需要此参数crontabl//列出某个用户cron服务的详细内容crontabr//删除某个用户的cron服务cr