This commit is contained in:
crusader 2018-03-27 16:37:39 +09:00
parent bf761f7a63
commit c281424878
7 changed files with 199 additions and 0 deletions

View File

@ -1 +1,5 @@
package overflow_probe_container package overflow_probe_container
const (
PROBE_CONTAINER_CRAWLERS = "PROBE_CONTAINER_CRAWLERS"
)

10
crawler/Crawler.go Normal file
View File

@ -0,0 +1,10 @@
package crawler
import (
sensorConfigM "git.loafle.net/overflow/overflow_commons_go/modules/sensor_config/model"
)
type Crawler interface {
Name() string
Get(sensorConfig *sensorConfigM.SensorConfig) (map[string]string, error)
}

View File

@ -80,6 +80,11 @@ func (sh *ServerHandlers) OnStop(serverCTX server.ServerContext) {
sh.ServerHandlers.OnStop(serverCTX) sh.ServerHandlers.OnStop(serverCTX)
} }
func (sh *ServerHandlers) Destroy(serverCTX server.ServerContext) {
sh.ServerHandlers.Destroy(serverCTX)
}
func (sh *ServerHandlers) Validate() { func (sh *ServerHandlers) Validate() {
sh.ServerHandlers.Validate() sh.ServerHandlers.Validate()

56
service/CrawlerService.go Normal file
View File

@ -0,0 +1,56 @@
package service
import (
"context"
"fmt"
"reflect"
cda "git.loafle.net/commons_go/di/annotation"
cdr "git.loafle.net/commons_go/di/registry"
oocmci "git.loafle.net/overflow/overflow_commons_go/modules/commons/interfaces"
"git.loafle.net/overflow/overflow_probe_container/crawler"
)
func init() {
cdr.RegisterType(reflect.TypeOf((*ProbeService)(nil)))
}
type CrawlerService struct {
cda.TypeAnnotation `annotation:"@overFlow:Service()"`
oocmci.Service
SensorConfigService *SensorConfigService `annotation:"@Inject()"`
Crawlers map[string]crawler.Crawler `annotation:"@Resource(name=PROBE_CONTAINER_CRAWLERS)"`
}
func (cs *CrawlerService) Start() error {
if nil == cs.Crawlers {
return fmt.Errorf("Crawlers is not set")
}
return nil
}
func (cs *CrawlerService) Stop(ctx context.Context) error {
return nil
}
func (cs *CrawlerService) GetData(sensorConfigID string) (map[string]string, error) {
sensorConfig, ok := cs.SensorConfigService.sensorConfigs[sensorConfigID]
if !ok {
return nil, fmt.Errorf("There is no sensor config for id[%s]", sensorConfigID)
}
_crawlerName := sensorConfig.Crawler.Name
_crawler, ok := cs.Crawlers[_crawlerName]
if !ok {
return nil, fmt.Errorf("There is no crawler[%s] for id[%s]", _crawlerName, sensorConfigID)
}
result, err := _crawler.Get(sensorConfig)
if nil != err {
return nil, fmt.Errorf("Failed to get data from crawler[%s] for id[%s] %v", _crawlerName, sensorConfigID, err)
}
return result, nil
}

View File

@ -1,11 +1,13 @@
package service package service
import ( import (
"context"
"reflect" "reflect"
cda "git.loafle.net/commons_go/di/annotation" cda "git.loafle.net/commons_go/di/annotation"
cdr "git.loafle.net/commons_go/di/registry" cdr "git.loafle.net/commons_go/di/registry"
cr "git.loafle.net/commons_go/rpc" cr "git.loafle.net/commons_go/rpc"
oocmci "git.loafle.net/overflow/overflow_commons_go/modules/commons/interfaces"
) )
func init() { func init() {
@ -14,10 +16,21 @@ func init() {
type ProbeService struct { type ProbeService struct {
cda.TypeAnnotation `annotation:"@overFlow:Service()"` cda.TypeAnnotation `annotation:"@overFlow:Service()"`
oocmci.Service
RPCServlet cr.Servlet RPCServlet cr.Servlet
} }
func (ps *ProbeService) Start() error {
return nil
}
func (ps *ProbeService) Stop(ctx context.Context) error {
return nil
}
func (ps *ProbeService) Send(method string, params ...interface{}) error { func (ps *ProbeService) Send(method string, params ...interface{}) error {
return ps.RPCServlet.Send(method, params...) return ps.RPCServlet.Send(method, params...)
} }

View File

@ -0,0 +1,80 @@
package service
import (
"context"
"fmt"
"reflect"
cda "git.loafle.net/commons_go/di/annotation"
cdr "git.loafle.net/commons_go/di/registry"
oocmci "git.loafle.net/overflow/overflow_commons_go/modules/commons/interfaces"
sensorConfigM "git.loafle.net/overflow/overflow_commons_go/modules/sensor_config/model"
)
func init() {
cdr.RegisterType(reflect.TypeOf((*SensorConfigService)(nil)))
}
type SensorConfigService struct {
cda.TypeAnnotation `annotation:"@overFlow:Service()"`
oocmci.Service
sensorConfigs map[string]*sensorConfigM.SensorConfig
}
func (scs *SensorConfigService) Start() error {
scs.sensorConfigs = make(map[string]*sensorConfigM.SensorConfig, 0)
return nil
}
func (scs *SensorConfigService) Stop(ctx context.Context) error {
return nil
}
func (scs *SensorConfigService) Init(sensorConfigs []*sensorConfigM.SensorConfig) error {
if nil == sensorConfigs || 0 == len(sensorConfigs) {
return nil
}
for _, sensorConfig := range sensorConfigs {
scs.sensorConfigs[sensorConfig.ID.String()] = sensorConfig
}
return nil
}
func (scs *SensorConfigService) AddConfig(sensorConfig *sensorConfigM.SensorConfig) error {
if nil == sensorConfig {
return fmt.Errorf("Sensor config is not valid")
}
sensorConfigID := sensorConfig.ID.String()
if _, ok := scs.sensorConfigs[sensorConfigID]; ok {
return fmt.Errorf("Sensor config[%s] is exist already", sensorConfigID)
}
scs.sensorConfigs[sensorConfigID] = sensorConfig
return nil
}
func (scs *SensorConfigService) UpdateConfig(sensorConfig *sensorConfigM.SensorConfig) error {
if nil == sensorConfig {
return fmt.Errorf("Sensor config is not valid")
}
sensorConfigID := sensorConfig.ID.String()
if _, ok := scs.sensorConfigs[sensorConfigID]; !ok {
return fmt.Errorf("Sensor config[%s] is not exist", sensorConfigID)
}
delete(scs.sensorConfigs, sensorConfigID)
scs.sensorConfigs[sensorConfigID] = sensorConfig
return nil
}
func (scs *SensorConfigService) RemoveConfig(sensorConfigID string) error {
if _, ok := scs.sensorConfigs[sensorConfigID]; !ok {
return fmt.Errorf("Sensor config[%s] is not exist", sensorConfigID)
}
delete(scs.sensorConfigs, sensorConfigID)
return nil
}

31
service/StateService.go Normal file
View File

@ -0,0 +1,31 @@
package service
import (
"context"
"reflect"
cda "git.loafle.net/commons_go/di/annotation"
cdr "git.loafle.net/commons_go/di/registry"
oocmci "git.loafle.net/overflow/overflow_commons_go/modules/commons/interfaces"
)
func init() {
cdr.RegisterType(reflect.TypeOf((*StateService)(nil)))
}
type StateService struct {
cda.TypeAnnotation `annotation:"@overFlow:Service()"`
oocmci.Service
}
func (s *StateService) Start() error {
return nil
}
func (s *StateService) Stop(ctx context.Context) error {
return nil
}
func (s *StateService) State() (bool, error) {
return true, nil
}