大佬教程收集整理的这篇文章主要介绍了Go语言版crontab,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
用过 linux 的应该对 cron 有所了解。linux 中可以通过 crontab -e 来配置定时任务。不过,linux 中的 cron 只能精确到分钟。而我们这里要讨论的 Go 实现的 cron 可以精确到秒,除了这点比较大的区别外,cron 表达式的基本语法是类似的。(如果使用过 Java 中的 Quartz,对 cron 表达式应该比较了解,而且它和这里我们将要讨论的 Go 版 cron 很像,也都精确到秒)
cron(计划任务),顾名思义,按照约定的时间,定时的执行特定的任务(job)。cron 表达式 表达了这种约定。
cron 表达式代表了一个时间集合,使用 6 个空格分隔的字段表示。
字段名 | 是否必须 | 允许的值 | 允许的特定字符 | ||
秒(Seconds) | 是 | 0-59 |
* /,-
|
||
分(minutes) | 时(Hours) | 0-23 | 日(Day of month) | 1-31 | :rgb(204,– ? |
月(Month) | 1-12 or JAN-DEC | 星期(Day of week) | 否 | 0-6 or SUM-SAT | :rgb(204,– ? |
注:
1)月(Month)和星期(Day of week)字段的值不区分大小写,如:SUN、Sun 和 sun 是一样的。
2)星期
(Day of week)字段如果没提供,相当于是 *
表示 cron 表达式能匹配该字段的所有值。如在第5个字段使用星号(month),表示每个月
表示增长间隔,如第1个字段(minutes) 值是 3-59/15,表示每小时的第3分钟开始执行一次,之后每隔 15 分钟执行一次(即 3、18、33、48 这些时间点执行),这里也可以表示为:3/15
用于@L_450_1@,如第6个字段值是 MON,WED,FRI,表示 星期一、三、五 执行
表示一个范围,如第3个字段的值为 9-17 表示 9am 到 5pm 直接每个小时(包括9和17)
只用于 日(Day of month) 和 星期(Day of week),表示不指定值,可以用于代替 *
// 当 Cron 已经运行了,增加新的 Entity 是通过 add 这个 chAnnel 实现的
5 | snapshot chan []*Entry // 获取当前所有 entity 的快照 |
bool
// 当已经运行时为true;否则为false
7 | } |
注意,Cron 结构没有导出任何成员。
注意:有一个成员 stop,类型是 struct{},即空结构体。
// The schedule on which this job should be run.
// 负责调度当前 Entity 中的 Job 执行 |
// started or this entry's schedule is unsatisfiable |
9 | Nexttime .Time |
11 | // The last time this job was run. This is the zero time if the job has never |
13 | // 上一次执行时间 |
.Time
15 | 16 | // The Job to run. |
17 | // 要执行的 Job |
19 | } |
} |
由于 Entity 中需要 Job 类型,因此,我们希望定期运行的任务,就需要实现 Job 接口。同时,由于 Job 接口只有一个无参数无返回值的方法,为了使用方便,作者提供了一个类型:
它通过简单的实现 Run() 方法来实现 Job 接口:
这样,任何无参数无返回值的函数,通过强制类型转换为 FuncJob,就可以当作 Job 来使用了,AddFunc 方法 就是这么做的。
负责调度 Job 的执行。它也是一个接口。
// Next is invoked initially,and then each time the job is run. |
Next(.TimE) } |
schedule 的具体实现通过解析 Cron 表达式得到。
库中提供了 schedule 的两个具体实现,分别是 Specschedule 和 ConstantDelayschedule。
① Specschedule
@H_874_434@type SpecscheduleSecond,minute,Hour,Dom,Month,Dow uint64
} |
从开始介绍的 Cron 表达式可以容易得知各个字段的意思,同时,对各种表达式的解析也会最终得到一个 Specschedule 的实例。库中的 Parse 返回的其实就是 Specschedule 的实例(当然也就实现了 schedule 接口)。
② ConstantDelayschedule
} |
这是一个简单的循环调度器,如:每 5 分钟。注意,最小单位是秒,不能比秒还小,比如 毫秒。
得到的是一个每 5 秒执行一次的调度器。
① 实例化 Cron
return
&Cron{
entries: nil, |
stop: make(chan {}),monospace!important; BACkground:none!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; direction:ltr!important; display:inline!important">snapshot: make(chan []*Entry),monospace!important; BACkground:none!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; direction:ltr!important; display:inline!important">running: false ,monospace!important; BACkground:none!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; direction:ltr!important; display:inline!important">} |
} |
可见实例化时,成员使用的基本是默认值;
② 解析 Cron 表达式
spec 可以是:
② 成员方法
// 将 job 加入 Cron 中
func (c *Cron) AddFunc(spec String,cmd func()) error |
func (c *Cron) AddJob(spec String,cmd Job) error |
// 通过两个参数实例化一个 Entity,然后加入当前 Cron 中 |
// 注意:如果当前 Cron 未运行,则直接将该 entity 加入 Cron 中; |
23 | func (c *Cron) Stop() |
这是一个简单示例。
输出类似这样:
@H_383_801@ 2014/02/22 21:23:40 cron running: 1
2014/02/22 21:23:45 cron running: 2
2014/02/22 21:23:50 cron running: 3
2014/02/22 21:23:55 cron running: 4
2014/02/22 21:24:00 cron running: 5
可见 cron 的使用还是挺简单的。
以上是大佬教程为你收集整理的Go语言版crontab全部内容,希望文章能够帮你解决Go语言版crontab所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。