本篇文章為大家展示了Golang中怎么實(shí)現(xiàn)一個(gè)cron 定時(shí)器,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
創(chuàng)新互聯(lián)是一家專(zhuān)業(yè)提供洛南企業(yè)網(wǎng)站建設(shè),專(zhuān)注與成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)、H5技術(shù)、小程序制作等業(yè)務(wù)。10年已為洛南眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專(zhuān)業(yè)的建站公司優(yōu)惠進(jìn)行中。
crontab.go
package task import ( "sync" "github.com/pkg/errors" cron "github.com/robfig/cron/v3" ) // Crontab crontab manager type Crontab struct { inner *cron.Cron ids map[string]cron.EntryID mutex sync.Mutex } // NewCrontab new crontab func NewCrontab() *Crontab { return &Crontab{ inner: cron.New(), ids: make(map[string]cron.EntryID), } } // IDs ... func (c *Crontab) IDs() []string { c.mutex.Lock() defer c.mutex.Unlock() validIDs := make([]string, 0, len(c.ids)) invalidIDs := make([]string, 0) for sid, eid := range c.ids { if e := c.inner.Entry(eid); e.ID != eid { invalidIDs = append(invalidIDs, sid) continue } validIDs = append(validIDs, sid) } for _, id := range invalidIDs { delete(c.ids, id) } return validIDs } // Start start the crontab engine func (c *Crontab) Start() { c.inner.Start() } // Stop stop the crontab engine func (c *Crontab) Stop() { c.inner.Stop() } // DelByID remove one crontab task func (c *Crontab) DelByID(id string) { c.mutex.Lock() defer c.mutex.Unlock() eid, ok := c.ids[id] if !ok { return } c.inner.Remove(eid) delete(c.ids, id) } // AddByID add one crontab task // id is unique // spec is the crontab expression func (c *Crontab) AddByID(id string, spec string, cmd cron.Job) error { c.mutex.Lock() defer c.mutex.Unlock() if _, ok := c.ids[id]; ok { return errors.Errorf("crontab id exists") } eid, err := c.inner.AddJob(spec, cmd) if err != nil { return err } c.ids[id] = eid return nil } // AddByFunc add function as crontab task func (c *Crontab) AddByFunc(id string, spec string, f func()) error { c.mutex.Lock() defer c.mutex.Unlock() if _, ok := c.ids[id]; ok { return errors.Errorf("crontab id exists") } eid, err := c.inner.AddFunc(spec, f) if err != nil { return err } c.ids[id] = eid return nil } // IsExists check the crontab task whether existed with job id func (c *Crontab) IsExists(jid string) bool { _, exist := c.ids[jid] return exist }
crontab_test.go
package task import ( "fmt" "os" "testing" "time" ) type testTask struct { } func (t *testTask) Run() { fmt.Println("hello world1", time.Now()) } func TestCronTask(t *testing.T) { crontab := NewCrontab() // 實(shí)現(xiàn)接口的方式添加定時(shí)任務(wù) task := &testTask{} //2m 一次 if err := crontab.AddByID("1", "*/2 * * * ?", task); err != nil { fmt.Printf("error to add crontab task:%s", err) os.Exit(-1) } // 添加函數(shù)作為定時(shí)任務(wù) taskFunc := func() { fmt.Println("hello world2", time.Now()) } //而 github.com/robfig/cron 也按照操作系統(tǒng)進(jìn)行判斷,默認(rèn)支持到分鐘級(jí)別 ,1分鐘一次 if err := crontab.AddByFunc("2", "*/1 * * * ?", taskFunc); err != nil { fmt.Printf("error to add crontab task:%s", err) os.Exit(-1) } crontab.Start() select {} }
go cron 只支持
{分} {時(shí)} {日} {月} {周} {年(可選)}
上述內(nèi)容就是Golang中怎么實(shí)現(xiàn)一個(gè)cron 定時(shí)器,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享標(biāo)題:Golang中怎么實(shí)現(xiàn)一個(gè)cron定時(shí)器
本文URL:http://sd-ha.com/article46/iedhhg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站收錄、電子商務(wù)、品牌網(wǎng)站設(shè)計(jì)、服務(wù)器托管、
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)