1、cron 表达式的基本格式
用过 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 | 月(Month) | 1-12 or JAN-DEC | 星期(Day of week) | 否 | 0-6 or SUM-SAT | 注: 1)月(Month)和星期(Day of week)字段的值不区分大小写,如:SUN、Sun 和 sun 是一样的。 2)星期 (Day of week)字段如果没提供,相当于是 * 2、特殊字符说明1)星号(*)表示 cron 表达式能匹配该字段的所有值。如在第5个字段使用星号(month),表示每个月 2)斜线(/)表示增长间隔,如第1个字段(minutes) 值是 3-59/15,表示每小时的第3分钟开始执行一次,之后每隔 15 分钟执行一次(即 3、18、33、48 这些时间点执行),这里也可以表示为:3/15 3)逗号(,)用于枚举值,如第6个字段值是 MON,WED,FRI,表示 星期一、三、五 执行 4)连字号(-)表示一个范围,如第3个字段的值为 9-17 表示 9am 到 5pm 直接每个小时(包括9和17) 5)问号(?)只用于 日(Day of month) 和 星期(Day of week),表示不指定值,可以用于代替 * 3、主要类型或接口说明1)Cron:包含一系列要执行的实体;支持暂停【stop】;添加实体等 |
add chan *Entry // 当 Cron 已经运行了,增加新的 Entity 是通过 add 这个 channel 实现的 |
5 | snapshot chan []*Entry // 获取当前所有 entity 的快照 |
bool
// 当已经运行时为true;否则为false
7 | } |