57 lines
1.0 KiB
Go
57 lines
1.0 KiB
Go
|
package scheduler
|
||
|
|
||
|
import (
|
||
|
"loafle.com/overflow/collector_go/scheduler/cron"
|
||
|
c "loafle.com/overflow/crawler_go"
|
||
|
"log"
|
||
|
"strconv"
|
||
|
"sync"
|
||
|
)
|
||
|
|
||
|
const DEFAULT_INTERVAL = 5
|
||
|
|
||
|
type Scheduler struct {
|
||
|
crawler *c.CrawlerImpl
|
||
|
once sync.Once
|
||
|
}
|
||
|
|
||
|
func (s *Scheduler) Init() {
|
||
|
s.once.Do(func() {
|
||
|
cron.Start()
|
||
|
s.crawler = &c.CrawlerImpl{}
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func (s *Scheduler) NewSchedule(id string, interval string, targetFunc interface{}) {
|
||
|
i, err := strconv.Atoi(interval)
|
||
|
if err != nil {
|
||
|
i = DEFAULT_INTERVAL
|
||
|
}
|
||
|
cron.AddTask(id, uint64(i-1)).Invoke(targetFunc, id)
|
||
|
}
|
||
|
|
||
|
func (s *Scheduler) RemoveSchedule(id string) {
|
||
|
cron.Remove(id)
|
||
|
}
|
||
|
|
||
|
func (s *Scheduler) RemoveAllSchedule() {
|
||
|
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-1))
|
||
|
}
|
||
|
|
||
|
func (s *Scheduler) requestGet(id string) {
|
||
|
data, err := s.crawler.Get(id)
|
||
|
if err != nil {
|
||
|
log.Printf("[ID: %s] An error has occurred. %s", id, err.Error())
|
||
|
return
|
||
|
}
|
||
|
log.Println(data)
|
||
|
}
|