github.com/easysoft/zendata@v0.0.0-20240513203326-705bd5a7fd67/internal/server/utils/cron.go (about) 1 package serverUtils 2 3 import ( 4 "fmt" 5 6 "github.com/robfig/cron/v3" 7 ) 8 9 var cronInstace *cron.Cron 10 11 var taskFunc = make(map[string]func()) 12 13 func GetCrontabInstance() *cron.Cron { 14 if cronInstace != nil { 15 return cronInstace 16 } 17 cronInstace = cron.New() 18 cronInstace.Start() 19 20 return cronInstace 21 } 22 23 func AddTaskFuc(name string, schedule string, f func()) { 24 if _, ok := taskFunc[name]; !ok { 25 fmt.Println("Add a new task:", name) 26 27 cInstance := GetCrontabInstance() 28 cInstance.AddFunc(schedule, f) 29 30 taskFunc[name] = f 31 } else { 32 fmt.Println("Don't add same task `" + name + "` repeatedly!") 33 } 34 } 35 36 func Stop() { 37 cronInstace.Stop() 38 }