Go語言定時任務cron的設計與使用
一、Cron表達式
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 | * / , - ?
Cron表達式的格式是通過六個字符表示:"1 * * * * *"
。這六位數分別表示秒,分,小時,每月第幾天,月,每個星期第幾天;
在這里重點解釋一下特殊字符:
*
:代表任意值;*
在分鐘字段,表示每分鐘;/
:用來指定時間間隔,*/15
在分鐘字段,表示每隔15分鐘;,
:列出多個離散值,1,15
在天字段,表示每月1號和15號;-
:定義某個范圍,9-17
在小時字段,表示上午9點到下午5點,兩邊都是閉區(qū)間;?
:表示無特定值。在Cron中,如果天數與星期的指定會互斥??聪旅鎯蓚€例子:
0 0 12 ? * WED - 表示每周三中午12點。關心星期,忽略天數;
0 0 12 15 * ? - 表示每個月的第15天中午12點。關心天數,忽略星期;
同時在"github.com/robfig/cron/v3"
包中預定義的Schedule,如下所示:
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 between Sat/Sun | 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 * * * *
二、如何使用Cron包
func TestCron(t *testing.T) { c := cron.New(cron.WithSeconds()) // 每分鐘第一秒執(zhí)行該任務 c.AddFunc("1 * * * * *", func() { fmt.Println("Hello world!") }) // 每10s執(zhí)行一次任務 sh := cron.Every(10 * time.Second) c.Schedule(sh, cron.FuncJob(func() { fmt.Println("you are ok") })) go func() { ticker := time.NewTicker(time.Second * 4) for { select { case <-ticker.C: fmt.Println("length: ", len(c.Entries())) } } }() // c.Start() c.Start() // Wait for the Cron job to run time.Sleep(5 * time.Minute) // Stop the Cron job scheduler c.Stop() }
上述示例代碼中,使用兩種創(chuàng)建定時任務的方式,分別是:
c.AddFunc()
c.Schedule()
cron包的使用非常簡單,你只需要提供Job
以及其執(zhí)行的規(guī)則即可。
三、如何設計一個Cron
關于Cron,調用者所有的操作與系統(tǒng)執(zhí)行對應的任務之間是異步的。因此,對于調用者來說,系統(tǒng)用例如下:
更進一步,可以查看下Cron提供的API:
type Cron struct { // Has unexported fields. } Cron keeps track of any number of entries, invoking the associated func as specified by the schedule. It may be started, stopped, and the entries may be inspected while running. 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 func (c *Cron) Entry(id EntryID) Entry func (c *Cron) Location() *time.Location func (c *Cron) Remove(id EntryID) func (c *Cron) Run() func (c *Cron) Schedule(schedule Schedule, cmd Job) EntryID func (c *Cron) Start() func (c *Cron) Stop() context.Context
調用者添加完所有任務之后,系統(tǒng)的處理流程如下(從后臺任務的角度看):
上述就是后臺任務的流程,簡化后的代碼如下:
func (c *Cron) run() { // Figure out the next activation times for each entry. now := c.now() for { // Determine the next entry to run. // 將所有任務,按照下一次運行時間排序 sort.Sort(byTime(c.entries)) for { select { case now = <-timer.C: now = now.In(c.location) c.logger.Info("wake", "now", now) // Run every entry whose next time was less than now for _, e := range c.entries { if e.Next.After(now) || e.Next.IsZero() { break } c.startJob(e.WrappedJob) e.Prev = e.Next e.Next = e.Schedule.Next(now) c.logger.Info("run", "now", now, "entry", e.ID, "next", e.Next) } // 新增一個任務 case newEntry := <-c.add: .... // 添加任務到數組容器 // 獲取當前時刻,Cron里面所有的定時任務 case replyChan := <-c.snapshot: replyChan <- c.entrySnapshot() continue // 停止Cron case <-c.stop: ... return // 移除某個定時任務 case id := <-c.remove: .... c.removeEntry(id) } break } } }
四、學習點
1. 通過channel傳輸快照
func (c *Cron) Entries() []Entry { c.runningMu.Lock() defer c.runningMu.Unlock() // 如果Cron,正在運行,那么返回一個通道 if c.running { replyChan := make(chan []Entry, 1) c.snapshot <- replyChan return <-replyChan } // 如果Cron,已經結束了,直接返回所有Entry return c.entrySnapshot() }
這種寫法特別有意思。當調用者想查看當前系統(tǒng)所有的任務時,系統(tǒng)返回的是一個通道,接著在通道中返回所有的數據。具體時序圖如下所示:
下面這個架構圖畫的不是很好,畫都畫了就放這吧。
2. 匹配規(guī)則
讀到cron這個項目,你是否有這樣的疑問?cron后臺任務根據調用給定的規(guī)則,如何執(zhí)行任務的呢?比如"* * * * 1 *"
,系統(tǒng)是如何知道每年的第一個月執(zhí)行相應的任務呢?下面代碼,以月份為例。
程序的大致流程:
- 將月份規(guī)則轉化為二進制數值;
- 通過當前時間不斷+1,直到匹配規(guī)則月份;
這里主要借助下面這個函數:
func getBits(min, max, step uint) uint64 { var bits uint64 // If step is 1, use shifts. if step == 1 { return ^(math.MaxUint64 << (max + 1)) & (math.MaxUint64 << min) } // Else, use a simple loop. for i := min; i <= max; i += step { bits |= 1 << i } return bits } func TestGetBits(t *testing.T) { res := getBits(1, 3, 1) fmt.Printf("%d 的二進制表示是 %b\n", res, res) }
3. 實現接口的函數
// Job is an interface for submitted cron jobs. type Job interface { Run() } type FuncJob func() func (f FuncJob) Run() { f() }
上述代碼定義Job
接口、FuncJob
類型,并且函數類型實現了Job
接口。這種寫法很常見,比如http.HandleFunc
。這樣寫的好處,能夠將一個函數強轉之后直接丟到接口參數中,具體轉化流程如下:
func() 類型函數 -- 強轉:FuncJob(func()) -- FuncJob -- 可以丟進Job接口中;
到此這篇關于Go語言定時任務cron的設計與使用的文章就介紹到這了,更多相關Go定時任務cron內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Go外部依賴包從vendor,$GOPATH和$GOPATH/pkg/mod查找順序
這篇文章主要介紹了Go外部依賴包vendor,$GOPATH和$GOPATH/pkg/mod下查找順序,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12