Usage
注冊(cè)任務(wù)到調(diào)度器里,當(dāng)任務(wù)要執(zhí)行的時(shí)候會(huì)使用goroutines調(diào)用,這樣每個(gè)任務(wù)都不會(huì)發(fā)生阻塞翘悉。
Golang不僅僅是兼容了linux標(biāo)準(zhǔn)的crontab格式育瓜,而且擴(kuò)展了秒喝检。也就是說正常的crontab是 分 時(shí) 小時(shí) 月 星期娄帖,而robfig cron是 秒 分 時(shí) 日 月 星期季蚂。
c := cron.New()
c.AddFunc("0 30 * * * *", func() { fmt.Println("Every hour on the half hour") })
c.AddFunc("@hourly", func() { fmt.Println("Every hour") })
c.AddFunc("@every 1h30m", func() { fmt.Println("Every hour thirty") })
c.Start()
//這些任務(wù)都是異步執(zhí)行的.
c.AddFunc("@daily", func() { fmt.Println("Every day") })
//獲取他下次執(zhí)行的時(shí)間.
inspect(c.Entries())
//關(guān)閉著計(jì)劃任務(wù), 但是不能關(guān)閉已經(jīng)在執(zhí)行中的任務(wù).
c.Stop()
CRON Expression Format
A cron expression represents a set of times, using 6 space-separated fields.
Field name | Mandatory? | Allowed values | Allowed special characters |
---|---|---|---|
Seconds | Yes | 0-59 | * / , - |
Minutes | Yes | 0-59 | * / , - |
Hours | Yes | 0-23 | * / , - |
Day of month | Yes | 1-31 | * / , - ? |
Month | Yes | 1-12 or JAN-DEC | * / , - |
Day of week | Yes | 0-6 or SUN-SAT | * / , - ? |
Predefined schedules
You may use one of several pre-defined schedules in place of a cron expression.
Entry | Description | Equivalent To |
---|---|---|
@yearly (or @annually) | Run once a year, midnight, Jan. 1st | 0 0 0 1 1 * |
@monthly | Run once a month, midnight, first of month | 0 0 0 1 * * |
@weekly | Run once a week, midnight on Sunday | 0 0 0 * * 0 |
@daily (or @midnight) | Run once a day, midnight | 0 0 0 * * * |
@hourly | Run once an hour, beginning of hour | 0 0 * * * * |