This commit is contained in:
crusader 2018-07-04 11:35:52 +09:00
parent f3e1641301
commit 519e96beea
4 changed files with 33 additions and 32 deletions

View File

@ -27,12 +27,13 @@ func NewConnector(containerType occp.ContainerType, portNumber int) (*csswc.Conn
connector.Name = containerType.String()
connector.ReconnectInterval = 5
connector.ReconnectTryTime = 2
connector.MaxMessageSize = 4096
connector.ReadBufferSize = 4096
connector.WriteBufferSize = 4096
connector.MaxMessageSize = 8192
connector.ReadBufferSize = 8192
connector.WriteBufferSize = 8192
connector.PongTimeout = 60
connector.PingTimeout = 10
connector.PingPeriod = 9
connector.CompressionThreshold = 4096
connector.RequestHeader = func() http.Header {
header := make(map[string][]string)

View File

@ -3,13 +3,13 @@ package service
import (
"fmt"
"reflect"
"strconv"
"sync"
"time"
cda "git.loafle.net/commons/di-go/annotation"
cdr "git.loafle.net/commons/di-go/registry"
logging "git.loafle.net/commons/logging-go"
cuej "git.loafle.net/commons/util-go/encoding/json"
cuts "git.loafle.net/commons/util-go/time/scheduler"
cutss "git.loafle.net/commons/util-go/time/scheduler/storage"
occu "git.loafle.net/overflow/commons-go/core/util"
@ -67,12 +67,12 @@ func (s *CollectorService) AddSensorConfigs(sensorConfigs []*ocmsc.SensorConfig)
}
for _, sensorConfig := range sensorConfigs {
interval, err := strconv.ParseInt(sensorConfig.Schedule.Interval, 10, 64)
interval, err := cuej.NumberToInt(sensorConfig.Schedule.Interval)
if nil != err {
return fmt.Errorf("Cannot convert interval[%s] %v", sensorConfig.Schedule.Interval, err)
}
s.addSchedule(interval, sensorConfig)
logging.Logger().Debugf("scheduler of config[%s] has been added", sensorConfig.ConfigID)
logging.Logger().Debugf("scheduler of config[%s] has been added", sensorConfig.SensorID)
}
return nil
@ -84,47 +84,47 @@ func (s *CollectorService) RemoveSensorConfigs(sensorConfigs []*ocmsc.SensorConf
}
for _, sensorConfig := range sensorConfigs {
s.removeSchedule(sensorConfig.ConfigID)
s.removeSchedule(sensorConfig.SensorID.String())
}
return nil
}
func (s *CollectorService) addSchedule(interval int64, sensorConfig *ocmsc.SensorConfig) {
s.removeSchedule(sensorConfig.ConfigID)
func (s *CollectorService) addSchedule(interval int, sensorConfig *ocmsc.SensorConfig) {
s.removeSchedule(sensorConfig.SensorID.String())
scheduleID, err := s.scheduler.RunEvery(time.Duration(interval)*time.Second, s.collectTask, sensorConfig)
if nil != err {
logging.Logger().Errorf("Cannot add schedule for config[%s] %v", sensorConfig.ConfigID, err)
logging.Logger().Errorf("Cannot add schedule for config[%s] %v", sensorConfig.SensorID, err)
return
}
s.scheduleMap.Store(sensorConfig.ConfigID, scheduleID)
s.scheduleMap.Store(sensorConfig.SensorID, scheduleID)
}
func (s *CollectorService) removeSchedule(configID string) {
scheduleID, ok := s.scheduleMap.Load(configID)
func (s *CollectorService) removeSchedule(sensorID string) {
scheduleID, ok := s.scheduleMap.Load(sensorID)
if !ok {
return
}
s.scheduleMap.Delete(configID)
s.scheduleMap.Delete(sensorID)
s.scheduler.Cancel(scheduleID.(string))
}
func (s *CollectorService) collectTask(sensorConfig *ocmsc.SensorConfig) {
logging.Logger().Debugf("CollectorService.collectTask for sensor config id[%s] of crawler[%s]", sensorConfig.ConfigID, sensorConfig.Crawler.Name)
logging.Logger().Debugf("CollectorService.collectTask for sensor config id[%s] of crawler[%s]", sensorConfig.SensorID, sensorConfig.Crawler.MetaCrawlerKey)
result, err := s.CrawlerService.Get(sensorConfig.ConfigID)
result, err := s.CrawlerService.Get(sensorConfig.SensorID.String())
if nil != err {
logging.Logger().Errorf("Cannot get data from crawler[%s] %v", sensorConfig.Crawler.Name, err)
logging.Logger().Errorf("Cannot get data from crawler[%s] %v", sensorConfig.Crawler.MetaCrawlerKey, err)
return
}
m := &ocmd.Metric{
SensorConfigID: sensorConfig.ConfigID,
SensorConfigID: sensorConfig.SensorID.String(),
Data: result,
CollectDate: occu.NowPtr(),
}
if err := s.ProbeService.Send("MetricService.Send", m); nil != err {
logging.Logger().Errorf("Cannot send data from config id[%s] of crawler[%s] %v", sensorConfig.ConfigID, sensorConfig.Crawler.Name, err)
logging.Logger().Errorf("Cannot send data from config id[%s] of crawler[%s] %v", sensorConfig.SensorID, sensorConfig.Crawler.MetaCrawlerKey, err)
}
}

View File

@ -69,7 +69,7 @@ func (s *CrawlerService) Get(sensorConfigID string) (map[string]string, error) {
if !ok {
return nil, fmt.Errorf("There is no sensor config for id[%s]", sensorConfigID)
}
_crawlerName := sensorConfig.Crawler.Name
_crawlerName := sensorConfig.Crawler.MetaCrawlerKey
_crawler, ok := s.Crawlers[_crawlerName]
if !ok {
return nil, fmt.Errorf("There is no crawler[%s] for id[%s]", _crawlerName, sensorConfigID)

View File

@ -52,7 +52,7 @@ func (s *SensorConfigService) InitConfig(sensorConfigs []*ocmsc.SensorConfig) er
}
for _, sensorConfig := range sensorConfigs {
s.sensorConfigs[sensorConfig.ConfigID] = sensorConfig
s.sensorConfigs[sensorConfig.SensorID.String()] = sensorConfig
}
if err := s.CollectorService.AddSensorConfigs(sensorConfigs); nil != err {
@ -69,17 +69,17 @@ func (s *SensorConfigService) AddConfig(sensorConfig *ocmsc.SensorConfig) error
return fmt.Errorf("Sensor config is not valid")
}
configID := sensorConfig.ConfigID
if _, ok := s.sensorConfigs[configID]; ok {
return fmt.Errorf("Sensor config[%s] is exist already", configID)
sensorID := sensorConfig.SensorID.String()
if _, ok := s.sensorConfigs[sensorID]; ok {
return fmt.Errorf("Sensor config[%s] is exist already", sensorID)
}
s.sensorConfigs[configID] = sensorConfig
s.sensorConfigs[sensorID] = sensorConfig
if err := s.CollectorService.AddSensorConfigs([]*ocmsc.SensorConfig{sensorConfig}); nil != err {
return err
}
logging.Logger().Debugf("Sensor config[%s] was added", configID)
logging.Logger().Debugf("Sensor config[%s] was added", sensorID)
return nil
}
@ -89,12 +89,12 @@ func (s *SensorConfigService) UpdateConfig(sensorConfig *ocmsc.SensorConfig) err
return fmt.Errorf("Sensor config is not valid")
}
configID := sensorConfig.ConfigID
if _, ok := s.sensorConfigs[configID]; !ok {
return fmt.Errorf("Sensor config[%s] is not exist", configID)
sensorID := sensorConfig.SensorID.String()
if _, ok := s.sensorConfigs[sensorID]; !ok {
return fmt.Errorf("Sensor config[%s] is not exist", sensorID)
}
delete(s.sensorConfigs, configID)
s.sensorConfigs[configID] = sensorConfig
delete(s.sensorConfigs, sensorID)
s.sensorConfigs[sensorID] = sensorConfig
if err := s.CollectorService.RemoveSensorConfigs([]*ocmsc.SensorConfig{sensorConfig}); nil != err {
return err
@ -104,7 +104,7 @@ func (s *SensorConfigService) UpdateConfig(sensorConfig *ocmsc.SensorConfig) err
return err
}
logging.Logger().Debugf("Sensor config[%s] was updated", configID)
logging.Logger().Debugf("Sensor config[%s] was updated", sensorID)
return nil
}