overflow_probe_container/service/CrawlerService.go
crusader e7a2c3714e ing
2018-03-27 18:46:46 +09:00

61 lines
1.6 KiB
Go

package service
import (
"context"
"fmt"
"reflect"
cda "git.loafle.net/commons_go/di/annotation"
cdr "git.loafle.net/commons_go/di/registry"
"git.loafle.net/commons_go/logging"
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((*CrawlerService)(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)
}
logging.Logger().Debugf("GetData invoked with sensor config[%s]", 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)
}
logging.Logger().Debugf("GetData success config[%v]", result)
return result, nil
}