From 445cf4345a498958bf83b461176df2af7c5b2bce Mon Sep 17 00:00:00 2001 From: Huangzj Date: Thu, 17 Dec 2020 09:22:02 +0800 Subject: [PATCH] =?UTF-8?q?feat(Go-StudyExample):2020/12/17:=20=E6=96=B0?= =?UTF-8?q?=E5=A2=9EcronTab=E4=B8=8Ego=20cron=E7=9A=84=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E4=BD=BF=E7=94=A8=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/cron/Crontab_test.go | 66 +++++++++++++++++++++++ example/cron/Jasonlvhit_test.go | 96 +++++++++++++++++++++++++++++++++ example/cron/readme.md | 10 ++++ readme.md | 2 + 4 files changed, 174 insertions(+) create mode 100644 example/cron/Crontab_test.go create mode 100644 example/cron/Jasonlvhit_test.go create mode 100644 example/cron/readme.md diff --git a/example/cron/Crontab_test.go b/example/cron/Crontab_test.go new file mode 100644 index 0000000..254dedb --- /dev/null +++ b/example/cron/Crontab_test.go @@ -0,0 +1,66 @@ +/* + * @Author : huangzj + * @Time : 2020/12/16 17:10 + * @Description: + */ + +package cron + +import ( + "fmt" + "github.com/robfig/cron" + "log" + "testing" +) + +//测试单个定时任务 +func TestCron(t *testing.T) { + i := 0 + c := cron.New() + spec := "0 */1 * * * ?" //一分钟运行一次 + _ = c.AddFunc(spec, func() { + i++ + fmt.Println("cron running:", i) + }) + c.Start() + + select {} +} + +//多个定时任务 +func TestMoreCron(t *testing.T) { + i := 0 + c := cron.New() + //AddFunc + spec := "*/5 * * * * ?" + _ = c.AddFunc(spec, func() { + i++ + log.Println("cron running:", i) + }) + + //AddJob方法 + _ = c.AddJob(spec, TestJob{}) + _ = c.AddJob(spec, Test2Job{}) + + //启动计划任务 + c.Start() + + //关闭着计划任务, 但是不能关闭已经在执行中的任务. + defer c.Stop() + + select {} +} + +type TestJob struct { +} + +func (t TestJob) Run() { + fmt.Println("testJob1...") +} + +type Test2Job struct { +} + +func (t Test2Job) Run() { + fmt.Println("testJob2...") +} diff --git a/example/cron/Jasonlvhit_test.go b/example/cron/Jasonlvhit_test.go new file mode 100644 index 0000000..be1c273 --- /dev/null +++ b/example/cron/Jasonlvhit_test.go @@ -0,0 +1,96 @@ +/* + * @Author : huangzj + * @Time : 2020/12/16 17:23 + * @Description: + */ + +package cron + +import ( + "fmt" + "github.com/jasonlvhit/gocron" + "testing" + "time" +) + +func TestGoCron(t *testing.T) { + s := gocron.NewScheduler() + _ = s.Every(1).Seconds().Do(task) + _ = s.Every(4).Seconds().Do(superWang) + + sc := s.Start() // keep the channel + go test(s, sc) // wait + <-sc // it will happens if the channel is closed +} + +func task() { + fmt.Println("I am runnning task.", time.Now()) +} +func superWang() { + fmt.Println("I am runnning superWang.", time.Now()) +} + +func test(s *gocron.Scheduler, sc chan bool) { + time.Sleep(8 * time.Second) + s.Remove(task) //remove task + time.Sleep(8 * time.Second) + s.Clear() + fmt.Println("All task removed") + close(sc) // close the channel +} + +func TestFunc(t *testing.T) { + // Do jobs without params + _ = gocron.Every(1).Second().Do(task) + _ = gocron.Every(2).Seconds().Do(task) + _ = gocron.Every(1).Minute().Do(task) + _ = gocron.Every(2).Minutes().Do(task) + _ = gocron.Every(1).Hour().Do(task) + _ = gocron.Every(2).Hours().Do(task) + _ = gocron.Every(1).Day().Do(task) + _ = gocron.Every(2).Days().Do(task) + _ = gocron.Every(1).Week().Do(task) + _ = gocron.Every(2).Weeks().Do(task) + + // Do jobs with params + _ = gocron.Every(1).Second().Do(taskWithParams, 1, "hello") + + // Do jobs on specific weekday + _ = gocron.Every(1).Monday().Do(task) + _ = gocron.Every(1).Thursday().Do(task) + + // Do a job at a specific time - 'hour:min:sec' - seconds optional + _ = gocron.Every(1).Day().At("10:30").Do(task) + _ = gocron.Every(1).Monday().At("18:30").Do(task) + _ = gocron.Every(1).Tuesday().At("18:30:59").Do(task) + + // Begin job immediately upon start + _ = gocron.Every(1).Hour().From(gocron.NextTick()).Do(task) + + // Begin job at a specific date/time + timed := time.Date(2019, time.November, 10, 15, 0, 0, 0, time.Local) + _ = gocron.Every(1).Hour().From(&timed).Do(task) + + // NextRun gets the next running time + _, time := gocron.NextRun() + fmt.Println(time) + + // Remove a specific job + gocron.Remove(task) + + // Clear all scheduled jobs + gocron.Clear() + + // Start all the pending jobs + <-gocron.Start() + + // also, you can create a new scheduler + // to run two schedulers concurrently + s := gocron.NewScheduler() + _ = s.Every(3).Seconds().Do(task) + <-s.Start() +} + +func taskWithParams(a int, b string) { + fmt.Println(a, b) +} diff --git a/example/cron/readme.md b/example/cron/readme.md new file mode 100644 index 0000000..5232738 --- /dev/null +++ b/example/cron/readme.md @@ -0,0 +1,10 @@ +cronTab包获取地址:go get -u github.com/robfig/cron + +cronTab参考地址:http://www.topgoer.com/%E9%A1%B9%E7%9B%AE/%E5%AE%9A%E6%97%B6%E4%BB%BB%E5%8A%A1/cron.html + + + +go cron包获取地址:go get -u github.com/jasonlvhit/gocron + +go cron参考地址:http://www.topgoer.com/%E9%A1%B9%E7%9B%AE/%E5%AE%9A%E6%97%B6%E4%BB%BB%E5%8A%A1/gocron.html + diff --git a/readme.md b/readme.md index bdc5a38..4034919 100644 --- a/readme.md +++ b/readme.md @@ -25,6 +25,8 @@ json与struct之间转换处理工具使用示例 2020/12/17:新增yanyiwu中文分词工具使用示例 +2020/12/17: 新增cronTab与go cron的定时任务使用示例 + # mod vendor模式加载包 通过go mod的方式加载的github上面的包会有报红的问题,但是包本身是可以运行的,这样就是会有一个问题,如果你想要点击去看方法的内容,没办法做到