cron格式:
分 時(shí) 日 月 周蓮花網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián),蓮花網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為蓮花數(shù)千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的蓮花做網(wǎng)站的公司定做!
每隔5分鐘執(zhí)行1次: /5 * echo hello > /tmp/x.log
每隔1-5分鐘執(zhí)行1次: 1-5 echo hello > /tmp/x.log
每天10點(diǎn)整、22點(diǎn)整執(zhí)行1次: 0 10,22 * echo hello > /tmp/x.log
每隔1-5分鐘執(zhí)行1次: 1-5 echo hello > /tmp/x.log
Parse():解析與校驗(yàn)Cron表達(dá)式
Next():根據(jù)當(dāng)前時(shí)間,計(jì)算下次調(diào)度時(shí)間
package main
import (
"fmt"
"github.com/gorhill/cronexpr"
"time"
)
func main() {
var (
expr *cronexpr.Expression
err error
now time.Time
nextTime time.Time
)
//每隔5分鐘執(zhí)行一次(這個(gè)庫(kù)支持7位,精確到秒、年)
if expr, err = cronexpr.Parse("*/5 * * * * * *"); err != nil {
fmt.Println(err)
return
}
//當(dāng)前時(shí)間
now = time.Now()
//下次調(diào)度時(shí)間
nextTime = expr.Next(now)
//等待這個(gè)定時(shí)器超時(shí)
time.AfterFunc(nextTime.Sub(now), func() {
fmt.Println("被調(diào)度了:", nextTime)
}) //下次時(shí)間減去當(dāng)前時(shí)間
time.Sleep(10 * time.Second)
}
調(diào)用一個(gè)cron
輸出:被調(diào)度了: 2019-04-19 17:00:30 +0800 CST
package main
import (
"fmt"
"github.com/gorhill/cronexpr"
"time"
)
//代表一個(gè)任務(wù)
type CronJob struct {
expr *cronexpr.Expression
nextTime time.Time //expr.Next(time.Now())就可以得到下一次調(diào)度時(shí)間
}
func main() {
//需要有1個(gè)調(diào)度協(xié)程,它定時(shí)檢查所有Cron任務(wù),誰(shuí)過(guò)期了就執(zhí)行誰(shuí)
var (
cronJob *CronJob
expr *cronexpr.Expression
now time.Time
scheduleTable map[string] *CronJob //key:任務(wù)名字,
)
scheduleTable = make(map[string]*CronJob)
//當(dāng)前時(shí)間
now = time.Now()
//定義第一個(gè)cronjob
expr = cronexpr.MustParse("*/5 * * * * * *")
cronJob = &CronJob{
expr: expr,
nextTime: expr.Next(now),
}
//任務(wù)注冊(cè)到了調(diào)度表
scheduleTable["job1"] = cronJob
//定義第二個(gè)cronjob
expr = cronexpr.MustParse("*/5 * * * * * *")
cronJob = &CronJob{
expr: expr,
nextTime: expr.Next(now),
}
//任務(wù)注冊(cè)到了調(diào)度表
scheduleTable["job2"] = cronJob
//啟動(dòng)調(diào)度協(xié)程
go func() {
var (
jobName string
cronJob *CronJob
now time.Time
)
//定時(shí)檢查任務(wù)調(diào)度表是否有到期的
for {
now = time.Now()
for jobName, cronJob = range scheduleTable {
//判斷是否過(guò)期(如果下次調(diào)度時(shí)間早于等于當(dāng)前時(shí)間,說(shuō)明已經(jīng)過(guò)期了)
if cronJob.nextTime.Before(now) || cronJob.nextTime.Equal(now) {
//啟動(dòng)一個(gè)協(xié)程,執(zhí)行這個(gè)任務(wù)
go func(jobName string) {
fmt.Println("執(zhí)行:", jobName)
}(jobName)
//計(jì)算下一次調(diào)度時(shí)間
cronJob.nextTime = cronJob.expr.Next(now)
fmt.Println(jobName, "下次執(zhí)行時(shí)間:", cronJob.nextTime)
}
}
select {
//睡眠100毫秒(不讓它占用過(guò)多cpu)
case <- time.NewTimer(100 * time.Millisecond).C: //將在100毫秒可讀,返回
}
}
}()
time.Sleep(100 * time.Second)
}
調(diào)用多個(gè)cron,按時(shí)間間隔輸出:
執(zhí)行: job1
job1 下次執(zhí)行時(shí)間: 2019-04-19 17:31:20 +0800 CST
job2 下次執(zhí)行時(shí)間: 2019-04-19 17:31:20 +0800 CST
執(zhí)行: job2
執(zhí)行: job1
job1 下次執(zhí)行時(shí)間: 2019-04-19 17:31:25 +0800 CST
執(zhí)行: job2
job2 下次執(zhí)行時(shí)間: 2019-04-19 17:31:25 +0800 CST
執(zhí)行: job1
job1 下次執(zhí)行時(shí)間: 2019-04-19 17:31:30 +0800 CST
job2 下次執(zhí)行時(shí)間: 2019-04-19 17:31:30 +0800 CST
執(zhí)行: job2
......