<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                [TOC] # 簡單例子 安裝 ~~~ go get -u github.com/gorhill/cronexpr ~~~ ~~~ import ( "fmt" "github.com/gorhill/cronexpr" "time" ) func main() { var ( expr *cronexpr.Expression err error //當前時間 now time.Time nextTime time.Time ) //crontab 分鐘(0-59) ,小時(0-23), 天, 月, 星期幾只有5個 //每隔5秒執行一次 //他其實支持到秒的,秒 和 年 7個 if expr, err = cronexpr.Parse("*/5 * * * * * *"); err != nil { fmt.Println(err) return } //當前時間 now = time.Now() //下次調度時間 nextTime = expr.Next(now) fmt.Println(now) fmt.Println(nextTime) //睡眠一會,等待定時器超時 time.AfterFunc(nextTime.Sub(now), func() { fmt.Println("被調度了: ", nextTime) }) for { ; } } ~~~ # 子協程檢查任務過期 ~~~ import ( "fmt" "github.com/gorhill/cronexpr" "time" ) //代表一個任務 type CronJob struct { expr *cronexpr.Expression nextTime time.Time //expr.Next(now) 下次調度時間,可以看這個任務是不是到期了 } func main() { //需要有個調度協程,他定時檢查所有的cron任務,誰過期了就執行誰 var ( cronJob *CronJob //結構體指針,任務 expr *cronexpr.Expression now time.Time //當前時間 scheduleTable map[string]*CronJob //調度表 key: 任務名字 ) //創建任務表 scheduleTable = make(map[string]*CronJob) //當前時間 now = time.Now() //1. 定義個cronjob, MustParse和Parse相比就是里面必須寫正確 expr = cronexpr.MustParse("*/5 * * * * * *") cronJob = &CronJob{ expr: expr, nextTime: expr.Next(now), } //任務1注冊到調度表 scheduleTable["job1"] = cronJob //2. 定義個cronjob expr = cronexpr.MustParse("*/5 * * * * * *") cronJob = &CronJob{ expr: expr, nextTime: expr.Next(now), } //任務2注冊到調度表 scheduleTable["job2"] = cronJob //啟動調度協程 go func() { var ( jobName string cronJob *CronJob now time.Time ) //定時檢查下任務調度表 for { now = time.Now() for jobName, cronJob = range scheduleTable { //判斷是否過期,下次調度時間早于當前,下次調度時間等于當前時間 if cronJob.nextTime.Before(now) || cronJob.nextTime.Equal(now) { //啟動一個協程執行這個任務 go func(jobName string) { fmt.Println("協程---執行任務: ", jobName) }(jobName) //計算下次調度時間 cronJob.nextTime = cronJob.expr.Next(now) fmt.Println(jobName, "下次執行時間: ", cronJob.nextTime) } } select { //定時器睡眠100毫秒,在100毫秒可讀,返回 case <- time.NewTimer(100 * time.Millisecond).C: } } }() for { ; } } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看