GitHub - roylee0704/gron: gron, Cron Jobs in Go.
func main() {
//我们使用sync.WaitGroup保证主 goroutine 不退出。因为c.Start()中只是启动了一个 goroutine,如果主 goroutine 退出了,整个程序就停止了
var wg sync.WaitGroup
wg.Add(1)
//调用gron.New()创建一个管理器
c := gron.New()
//然后调用管理器的AddFunc()或Add()方法向它添加任务,
c.AddFunc(gron.Every(5*time.Second), func() {
fmt.Println("runs every 5 seconds.")
})
c.Start()
wg.Wait()
}
func main() {
var wg sync.WaitGroup
wg.Add(1)
c := gron.New()
c.AddFunc(gron.Every(1*time.Second), func() {
fmt.Println("runs every second.")
})
c.AddFunc(gron.Every(1*time.Minute), func() {
fmt.Println("runs every minute.")
})
c.AddFunc(gron.Every(1*time.Hour), func() {
fmt.Println("runs every hour.")
})
c.AddFunc(gron.Every(1*xtime.Day), func() {
fmt.Println("runs every day.")
})
c.AddFunc(gron.Every(1*xtime.Week), func() {
fmt.Println("runs every week.")
})
t, _ := time.ParseDuration("4m10s")
c.AddFunc(gron.Every(t), func() {
fmt.Println("runs every 4 minutes 10 seconds.")
})
c.Start()
wg.Wait()
}
func main() {
var wg sync.WaitGroup
wg.Add(1)
c := gron.New()
c.AddFunc(gron.Every(1*xtime.Day).At("22:00"), func() {
fmt.Println("runs every second.")
})
c.Start()
wg.Wait()
}
type GreetingJob struct {
Namestring}
func (g GreetingJob) Run() {
fmt.Println("Hello ", g.Name)
}
func main() {
var wg sync.WaitGroup
wg.Add(1)
g1 := GreetingJob{Name: "dj"}
g2 := GreetingJob{Name: "dajun"}
c := gron.New()
c.Add(gron.Every(5*time.Second), g1)
c.Add(gron.Every(10*time.Second), g2)
c.Start()
wg.Wait()
}