feat(Go-StudyExample):2020/12/17: 新增cronTab与go cron的定时任务使用示例
This commit is contained in:
@@ -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...")
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user