probe/service/CollectorService.go

100 lines
3.0 KiB
Go
Raw Normal View History

2018-04-17 14:11:13 +00:00
package service
import (
2018-04-29 09:41:14 +00:00
"fmt"
2018-04-17 14:11:13 +00:00
"reflect"
2018-04-29 09:41:14 +00:00
"strconv"
"time"
2018-04-17 14:11:13 +00:00
cda "git.loafle.net/commons/di-go/annotation"
cdr "git.loafle.net/commons/di-go/registry"
2018-04-29 09:41:14 +00:00
logging "git.loafle.net/commons/logging-go"
cuts "git.loafle.net/commons/util-go/time/scheduler"
cutss "git.loafle.net/commons/util-go/time/scheduler/storage"
occp "git.loafle.net/overflow/commons-go/config/probe"
ocmsc "git.loafle.net/overflow/commons-go/model/sensorconfig"
2018-04-26 08:43:40 +00:00
ocsp "git.loafle.net/overflow/commons-go/service/probe"
// For annotation
2018-04-17 14:11:13 +00:00
_ "git.loafle.net/overflow/commons-go/core/annotation"
)
var CollectorServiceType = reflect.TypeOf((*CollectorService)(nil))
func init() {
cdr.RegisterType(CollectorServiceType)
}
type CollectorService struct {
2018-04-26 08:43:40 +00:00
ocsp.CollectorService
2018-04-17 14:11:13 +00:00
cda.TypeAnnotation `annotation:"@overflow:RPCService()"`
2018-04-29 09:41:14 +00:00
DataClientService *DataClientService `annotation:"@Inject()"`
ContainerService *ContainerService `annotation:"@Inject()"`
SensorConfigService *SensorConfigService `annotation:"@Inject()"`
scheduler *cuts.Scheduler
2018-04-17 14:11:13 +00:00
}
func (s *CollectorService) InitService() error {
2018-04-29 09:41:14 +00:00
_storage := cutss.NewMemoryStorage()
s.scheduler = cuts.New(_storage)
2018-04-17 14:11:13 +00:00
return nil
}
func (s *CollectorService) StartService() error {
2018-04-29 09:41:14 +00:00
if err := s.scheduler.Start(); nil != err {
return err
}
2018-04-17 14:11:13 +00:00
2018-04-29 09:41:14 +00:00
if err := s.addScheduleAll(); nil != err {
return err
}
2018-04-17 14:11:13 +00:00
return nil
}
func (s *CollectorService) StopService() {
2018-04-29 09:41:14 +00:00
s.scheduler.Stop()
2018-04-17 14:11:13 +00:00
}
func (s *CollectorService) DestroyService() {
}
2018-04-29 09:41:14 +00:00
func (s *CollectorService) addScheduleAll() error {
sensorConfigs := s.SensorConfigService.sensorConfigs
if nil == sensorConfigs || 0 == len(sensorConfigs) {
return nil
}
for _, sensorConfig := range sensorConfigs {
interval, err := strconv.ParseInt(sensorConfig.Schedule.Interval, 10, 64)
if nil != err {
return fmt.Errorf("Cannot convert interval[%s] %v", sensorConfig.Schedule.Interval, err)
}
s.addSchedule(interval, sensorConfig)
}
return nil
}
func (s *CollectorService) addSchedule(interval int64, sensorConfig *ocmsc.SensorConfig) {
s.scheduler.RunEvery(time.Duration(interval)*time.Second, s.collectTask, sensorConfig)
}
func (s *CollectorService) collectTask(sensorConfig *ocmsc.SensorConfig) {
go func(_sensorConfig *ocmsc.SensorConfig) {
logging.Logger().Debugf("CollectorService.collectTask for sensor config id[%s] container[%s] crawler[%s]", _sensorConfig.ConfigID, _sensorConfig.Crawler.Container, _sensorConfig.Crawler.Name)
var result map[string]string
err := s.ContainerService.Call(occp.ToContainerType(_sensorConfig.Crawler.Container), &result, "CrawlerService.Get", _sensorConfig.ConfigID)
if nil != err {
logging.Logger().Errorf("Cannot get data from crawler[%s] of container[%s] %v", _sensorConfig.Crawler.Name, _sensorConfig.Crawler.Container, err)
return
}
logging.Logger().Debugf("Data[%v] received from crawler[%s] of container[%s]", result, _sensorConfig.Crawler.Name, _sensorConfig.Crawler.Container)
//cs.CentralService.Send(oocmp.HTTPEntry_Data, "CrawlerService.Data", result)
}(sensorConfig)
}