overflow_probe_container_ne.../service/ConfigService.go

58 lines
1.2 KiB
Go
Raw Normal View History

2017-12-04 11:59:11 +00:00
package service
2017-12-13 14:10:52 +00:00
import (
2018-03-26 02:58:39 +00:00
"reflect"
cda "git.loafle.net/commons_go/di/annotation"
cdr "git.loafle.net/commons_go/di/registry"
2017-12-13 14:10:52 +00:00
configM "git.loafle.net/overflow/overflow_commons_go/modules/config/model"
2017-12-14 06:17:58 +00:00
"git.loafle.net/overflow/overflow_probe_container_network/crawler/impl"
2017-12-13 14:10:52 +00:00
)
2018-03-26 02:58:39 +00:00
func init() {
cdr.RegisterType(reflect.TypeOf((*ConfigService)(nil)))
}
2017-12-04 11:59:11 +00:00
type ConfigService struct {
2018-03-26 02:58:39 +00:00
cda.TypeAnnotation `annotation:"@overFlow:Service()"`
2017-12-04 11:59:11 +00:00
}
2017-12-14 06:17:58 +00:00
func (s *ConfigService) Add(crawlerName string, config *configM.Config) error {
c := impl.GetCrawler(crawlerName)
return c.Add(config)
2017-12-04 11:59:11 +00:00
}
2017-12-14 06:17:58 +00:00
func (s *ConfigService) Remove(crawlerName string, id string) error {
c := impl.GetCrawler(crawlerName)
return c.Remove(id)
2017-12-04 11:59:11 +00:00
}
2017-12-14 06:17:58 +00:00
func (s *ConfigService) Init(configs []map[string]*configM.Config) error {
var err error
sensorIDs := make([]map[string]*configM.Config, 0)
ok := true
Loop:
2017-12-13 14:10:52 +00:00
for _, item := range configs {
for k, v := range item {
2017-12-14 06:17:58 +00:00
c := impl.GetCrawler(k)
if err = c.Init(v); nil != err {
ok = false
break Loop
}
sensorIDs = append(sensorIDs, item)
}
}
if !ok {
for _, item := range sensorIDs {
for k, v := range item {
s.Remove(k, v.ID.String())
}
2017-12-13 14:10:52 +00:00
}
2017-12-14 06:17:58 +00:00
return err
2017-12-13 14:10:52 +00:00
}
2017-12-04 11:59:11 +00:00
2017-12-14 06:17:58 +00:00
return nil
2017-12-04 11:59:11 +00:00
}