package scheduler import ( c "loafle.com/overflow/crawler_go" "loafle.com/overflow/cron_go" "strconv" "sync" ) const DEFAULT_INTERVAL = 5 type Scheduler struct { crawler *c.CrawlerImpl once sync.Once cronChan chan bool } func (s *Scheduler) Init() { s.once.Do(func() { s.cronChan = cron.Start() s.crawler = &c.CrawlerImpl{} }) } func (s *Scheduler) Stop() { s.cronChan <- false } func (s *Scheduler) NewSchedule(id, interval string, fn interface{}) error { return s.newSchedule(id, interval, fn) } func (s *Scheduler) RemoveSchedule(id string) error { return cron.Remove(id) } func (s *Scheduler) RemoveAllSchedule() error { return cron.RemoveAll() } func (s *Scheduler) UpdateSchedule(id string, interval string) { i, err := strconv.Atoi(interval) if err != nil { i = DEFAULT_INTERVAL } cron.UpdateTask(id, uint64(i)) } func (s *Scheduler) newSchedule(id string, interval string, fn interface{}) error { i, err := strconv.Atoi(interval) if err != nil { i = DEFAULT_INTERVAL } return cron.AddTask(id, uint64(i)).Invoke(fn, id) }