Golang | 定时任务
source link: https://ijayer.github.io/post/tech/code/golang/20180307-lib-cron-learn-note/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Golang | 定时任务
go get github.com/robfig/cron
cron(定时任务): 按照约定的时间,定时的执行特定的任务(job),cron 表达式表达了这种约定。
cron表达式格式
cron 表达式是一个字符串,字符串分为6个域,中间以空格隔开,每个域代表一种含义。
Seconds Minutes Hours DayofMonth Month DayofWeek |
字段名 | 是否必须 | 允许的值 | 允许的特定字符 |
---|---|---|---|
秒(Seconds) | 是 | 0-59 | * / , – |
分(Minutes) | 是 | 0-59 | * / , – |
时(Hours) | 是 | 0-23 | * / , – |
日(Day of month) | 是 | 1-31 | * / , – ? |
月(Month) | 是 | 1-12 or JAN-DEC | * / , – |
星期(Day of week) | 否 | 0-6 or SUM-SAT | * / , – ? |
- 月(月)和星期(Day of week)字段的值不区分大小写, 如:SUN、Sun、sun是一样的
- 星期(星期几)的字段没提供, 相当于是 *
- 每年执行:@yearly @annually
- 每月执行:@monthlay
- 每周执行:@weekly
- 每天执行:@dail @midnight(深夜)
- 没小时执行:@hourly
- @every + 空格 + * h * m * s (*表示具体的数字, h小时,m分钟,s秒)
- @every 2h 10m: 表示没两小时10分钟执行一次
cron特殊字符说明
星号(*)
表示 cron表达式能匹配该字段的所有值。如在第5个字段使用星号(month),表示每个月
斜线(/)
表示增长间隔,如第1个字段(minutes)值是 3-59/15,表示每小时的第3分钟开始执行一次,之后每隔 15 分钟执行一次(即 3、18、33、48这些时间点执行),这里也可以表示为:3/15
例如:spec := “*/5 * * * * *” //每隔5s执行一次
逗号(,)
用于枚举值,如第6个字段值是 MON,WED,FRI,表示星期一、三、五执行
例如: spec := “* 52,54 9 * * *” //每天9:52分和9:54分的每秒都执行一次
连字号(-)
表示一个范围,如第3个字段的值为 9-17 表示 9am到 5pm直接每个小时(包括9和17)
例如:spec := “15-30 * * * * *” //每分钟的15-30s执行定时任务
问号(?)
只用于日(Day of month)和星期(Day of week),表示不指定值,可以用于代替 *
cron主要类型或接口说明
Cron 包含一系列要执行的实体: 支持暂停[stop], 添加实体等
|
Entry
|
每一个实体包含一个需要运行的 Job: Job 是一个接口, 有一个方法: run
|
由于 Entity中需要 Job类型,因此,我们希望定期运行的任务,就需要实现 Job接口。同时,由于 Job接口只有一个无参数无返回值的方法,为了使用方便,作者提供了一个类型:
type FuncJob func()
它通过简单的实现 Run()方法来实现 Job接口:
func (f FuncJob) Run() { f() }
这样,任何无参数无返回值的函数,通过强制类型转换为 FuncJob,就可以当作 Job来使用了,AddFunc方法 就是这么做的
Schedule
每一个实体包含一个调度器(Schedule): 负责调度Job的执行, 也是一个接口
|
Schedule 的具体实现通过解析Cron表达式得到。
库中提供了 Schedule的两个具体实现,分别是 SpecSchedule和 ConstantDelaySchedule。
- SpecSchedule
|
从开始介绍的 Cron表达式可以容易得知各个字段的意思,同时,对各种表达式的解析也会最终得到一个 SpecSchedule的实例。库中的 Parse返回的其实就是 SpecSchedule的实例(当然也就实现了 Schedule接口)
- ConstantDelaySchedule
|
这是一个简单的循环调度器,如:每 5分钟。注意,最小单位是秒,不能比秒还小,比如毫秒。
通过 Every函数可以获取该类型的实例,如:
constDelaySchedule := Every(5e9)
得到的是一个每 5秒执行一次的调度器。
cron主要实例化方法
实例化 Cron
|
可见实例化时,成员使用的基本是默认值;
解析 Cron表达式
|
|
cron使用实例
|
See Also
Thanks to the authors 🙂
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK