ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] > [github](https://github.com/rfyiamcool/cronlib) ## 概述 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 | * / , - ? ## 安装 `go get github.com/rfyiamcool/cronlib` ## 实例 注意不要在 init中初始化 `cron = cronlib.New() ` ## 循环实例(注意避免注意指针影响) <details> <summary>main.go</summary> ``` package main import ( "fmt" "github.com/rfyiamcool/cronlib" ) var ( cron *cronlib.CronSchduler ) func main() { cron = cronlib.New() a := []struct { cron string name string url string }{ { name: "hello", cron: "*/1 * * * * *", url: "hello", }, { name: "word", cron: "*/2 * * * * *", url: "word", }, } for _, s := range a { b :=s model, e := cronlib.NewJobModel( b.cron, func() { fmt.Println(b.url) }) if e != nil { panic(e) } err := cron.Register(b.name, model) if err != nil { panic(err) } } cron.Start() select {} } ``` </details> <br/> ## 更新/停止操作 <details> <summary>描述</summary> ``` cron := cronlib.New() //添加定时器 model, _ := cronlib.NewJobModel("*/1 * * * * *", func() { fmt.Println("1") }) cron.Register("1",model) model2, _ := cronlib.NewJobModel("*/2 * * * * *", func() { fmt.Println("2") }) cron.Register("2",model2) // 停止定时 go func() { time.Sleep(4*time.Second) cron.StopService("1") }() // 取消注册 go func() { time.Sleep(5*time.Second) cron.UnRegister("1") }() // 更新定时器 go func() { time.Sleep(5*time.Second) model2, _ := cronlib.NewJobModel("0 13 11 * * *", func() { fmt.Println("3") }) cron.UpdateJobModel("2",model2) }() //批量停止 prefix 的定时器 // cron.StopServicePrefix("web_") cron.Start() ``` </details> <br/>