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.

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

问题描述

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

package main

import (
	"fmt"

	"github.com/robfig/cron/v3"
)

func main() {
	// 每隔3秒执行一次:*/3 * * * * *
	spec := "*/3 * * * * *"
	c := cron.New()
	c.AddFunc(spec, func() {
		fmt.Println("每隔3秒执行一次")
	})
	go c.Start()
	defer c.Stop()
	select {}
}

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

运行上述代码,发现定时任务并没有每隔3s打印输出一次
crond 表达式是参考:
https://www.cnblogs.com/linjiqin/p/3178452.html
https://www.jianshu.com/p/2fafcc32561f
在这里插入图片描述

解决方案

这是因为"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包

package main

import (
	"fmt"

	"github.com/robfig/cron"
)

func main() {
	// 每隔3秒执行一次:*/3 * * * * *
	spec := "*/3 * * * * *"
	c := cron.New()
	c.AddFunc(spec, func() {
		fmt.Println("每隔3秒执行一次")
	})
	go c.Start()
	defer c.Stop()
	select {}
}

在这里插入图片描述

解决方案二:

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

package main

import (
	"fmt"

	"github.com/robfig/cron/v3"
)

func main() {
	// 每天凌晨0点执行一次:0 0 0 * * ?
	// 每隔3秒执行一次:*/3 * * * * *
	// spec := "*/3 * * * * *"

	spec := "* * * * *"
	c := cron.New(cron.WithSeconds())
	c.AddFunc(spec, func() {
		fmt.Println("execute")
	})
	go c.Start()
	defer c.Stop()
	select {}
}
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值