- 表达式
- 特殊字符说明
- cron举例说明
- Chain">type Chain
- ConstantDelaySchedule">type ConstantDelaySchedule
- Job">type Job
- Logger">type Logger
- JobWrapper">type JobWrapper
- Entry">type Entry
- ScheduleParser">type ScheduleParser
- Parser">type Parser
- Option">type Option
- Cron">type Cron
- 例子
">
https://github.com/robfig/cron
表达式
| 字段名 | 是否必须 | 允许的值 | 允许的特定字符 |
|---|---|---|---|
| 秒(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 | * / , – ? |
注:
1)月(Month)和星期(Day of week)字段的值不区分大小写,如:SUN、Sun 和 sun 是一样的。
2)星期 (Day of week)字段如果没提供,相当于是 *
特殊字符说明
1)星号(*)表示 cron 表达式能匹配该字段的所有值。如在第5个字段使用星号(month),表示每个月2)斜线(/)表示增长间隔,如第1个字段(minutes) 值是 3-59/15,表示每小时的第3分钟开始执行一次,之后每隔 15 分钟执行一次(即 3、18、33、48 这些时间点执行),这里也可以表示为:3/153)逗号(,)用于枚举值,如第6个字段值是 MON,WED,FRI,表示 星期一、三、五 执行4)连字号(-)表示一个范围,如第3个字段的值为 9-17 表示 9am 到 5pm 直接每个小时(包括9和17)5)问号(?)只用于日(Day of month)和星期(Day of week),\表示不指定值,可以用于代替 *
cron举例说明
每隔5秒执行一次:/5 ?
每隔1分钟执行一次:0 /1 ?
每天23点执行一次:0 0 23 ?
每天凌晨1点执行一次:0 0 1 ?
每月1号凌晨1点执行一次:0 0 1 1 ?
在26分、29分、33分执行一次:0 26,29,33 ?
每天的0点、13点、18点、21点都执行一次:0 0 0,13,18,21 * ?
您可以使用几个预定义的表达式来代替上表的表达式
| 输入 | 描述 | 等式 |
|---|---|---|
| @yearly (or @annually) | 每年1月1日午夜跑步一次 | 0 0 0 1 1 * |
| @monthly | 每个月第一天的午夜跑一次 | 0 0 0 1 |
| @weekly | 每周周六的午夜运行一次 | 0 0 0 0 |
| @daily (or @midnight) | 每天午夜跑一次 | 0 0 0 * |
| @hourly | 每小时运行一次 | 0 0 |
type Chain
type Chain struct {// contains filtered or unexported fields}func NewChain(c ...JobWrapper) Chainfunc (c Chain) Then(j Job) Job// NewChain(m1, m2, m3).Then(job)is equivalent to:// m1(m2(m3(job)))
type ConstantDelaySchedule
Every返回crontab Schedule,在每个持续时间内激活一次。不支持小于1秒的延迟(将四舍五入到1秒)。任何小于Second的字段都会被截断。
type ConstantDelaySchedule struct {Delay time.Duration}func (schedule ConstantDelaySchedule) Next(t time.Time) time.Timefunc Every(duration time.Duration) ConstantDelaySchedule
type Job
type Job interface {Run()}
type Logger
type Logger interface {// Info logs routine messages about cron's operation.Info(msg string, keysAndValues ...interface{})// Error logs an error condition.Error(err error, msg string, keysAndValues ...interface{})}var DefaultLogger Logger = PrintfLogger(log.New(os.Stdout, "cron: ", log.LstdFlags))var DiscardLogger Logger = PrintfLogger(log.New(ioutil.Discard, "", 0))// 只记录errorfunc PrintfLogger(l interface{ Printf(string, ...interface{}) }) Logger// 记录所有日志func VerbosePrintfLogger(l interface{ Printf(string, ...interface{}) }) Logger
type JobWrapper
type JobWrapper func(Job) Jobfunc DelayIfStillRunning(logger Logger) JobWrapper // 运行超一分钟的任务打印logfunc Recover(logger Logger) JobWrapper// 如果之前的调用还在运行,则不调用并打印日志func SkipIfStillRunning(logger Logger) JobWrapper
type Entry
type Entry struct {ID EntryIDSchedule ScheduleNext time.Time //下一次运行时间Prev time.Time //上一次运行时间WrappedJob JobJob Job}
type ScheduleParser
type ScheduleParser interface {Parse(spec string) (Schedule, error)}
type Parser
type Parser struct {// contains filtered or unexported fields}func NewParser(options ParseOption) Parser// Standard parser without descriptorsspecParser := NewParser(Minute | Hour | Dom | Month | Dow)sched, err := specParser.Parse("0 0 15 */3 *")// Same as above, just excludes time fieldssubsParser := NewParser(Dom | Month | Dow)sched, err := specParser.Parse("15 */3 *")// Same as above, just makes Dow optionalsubsParser := NewParser(Dom | Month | DowOptional)sched, err := specParser.Parse("15 */3")func (p Parser) Parse(spec string) (Schedule, error)// ParseStandard返回一个新的crontab计划,表示给定的https://en.wikipedia.org/wiki/Cron。// 它需要5个条目,依次表示:分钟、小时、月中的日、月、星期中的日。// 如果规范无效,它将返回一个描述性错误。func ParseStandard(standardSpec string) (Schedule, error)
type Option
type Option func(*Cron)// 指定作业包装器应用于添加到这个cron的所有作业func WithChain(wrappers ...JobWrapper) Option// 覆盖cron实例的时区func WithLocation(loc *time.Location) Optionfunc WithLogger(logger Logger) Option// 覆盖用于解释作业调度的解析器func WithParser(p ScheduleParser) Option// 覆盖用于解释作业调度的解析器,以将seconds字段作为第一个字段func WithSeconds() Option
type Cron
type Cron struct {// contains filtered or unexported fields}
Cron跟踪任意数量的条目,根据计划调用相关的func。可以在运行时启动、停止和检查条目
func New(opts …Option) Cron
func (c Cron) AddFunc(spec string, cmd func()) (EntryID, error) //增加一个方法
func (c Cron) AddJob(spec string, cmd Job) (EntryID, error) //增加一个任务
func (c Cron) Entries() []Entry //获取cron所有条目
func (c Cron) Entry(id EntryID) Entry //获取指定entryid的条目
func (c Cron) Location() time.Location //获取时区位置
func (c Cron) Remove(id EntryID) //删除一个任务
func (c Cron) Run() //启动+阻塞
func (c Cron) Start() //启动
func (c *Cron) Stop() context.Context //停止(不会停止正在执行中的任务)
例子
package mainimport ("github.com/robfig/cron""log")type Job struct {}func (j *Job) Run(){log.Println("Job running.....")}func main() {i := 0c := cron.New()spec := "*/5 * * * * ?"c.AddFunc(spec, func() {i++log.Println("cron running:", i)})c.AddJob(spec,&Job{})c.Run()}
