package cron import ( "DataShare/dao" "DataShare/global" "DataShare/model" "DataShare/service" "fmt" "github.com/jasonlvhit/gocron" "github.com/sirupsen/logrus" "strconv" "sync" ) type CacheTimer struct{ sc *gocron.Scheduler q chan bool Id []int } var once sync.Once var singleInstance *CacheTimer func GetInstance() *CacheTimer { if singleInstance == nil { once.Do( func() { singleInstance = &CacheTimer{} }) } return singleInstance } func (this *CacheTimer)Start(){ //每次开机运行都执行一下daytask this.q = make(chan bool) this.Id = make([]int,0) go this.jobs(this.q) } func (this *CacheTimer)Stop(){ this.q <- true } func (this *CacheTimer)jobs(quit <-chan bool) { for { //cron jobs this.sc = gocron.NewScheduler() //每天凌晨00:00 同步配置文件 //this.sc.Every(1).Day().At("00:00:00").Do(this.CacheTask) fmt.Println("---------------------------timer jobs------------------------") this.sc.Every(5).Seconds().Do(this.CacheTask) this.sc.Every(10).Seconds().Do(this.SubscribeTask) select { case <-quit: // here we receive from quit and exit // if `g` has started, we may want to `g.Clear()` // or the scheduled jobs will continue, we will just not be waiting for them to finish. return case <-this.sc.Start(): // here we know all the jobs are done, and go round the loop again } } } func (this *CacheTimer)CacheTask(){ //this.sc.Clear() data_cache_list := dao.LoadDataCacheList() if len(this.Id) != len(data_cache_list) { this.sc.Clear() this.Id = make([]int,0) this.sc.Every(5).Seconds().Do(this.CacheTask) } for i:=0;i