container-go/service/CrawlerService.go

88 lines
2.2 KiB
Go
Raw Permalink Normal View History

2018-04-19 06:24:45 +00:00
package service
import (
"fmt"
"reflect"
cda "git.loafle.net/commons/di-go/annotation"
cdr "git.loafle.net/commons/di-go/registry"
logging "git.loafle.net/commons/logging-go"
"git.loafle.net/overflow/crawler-go"
// For annotation
_ "git.loafle.net/overflow/commons-go/core/annotation"
)
var CrawlerServiceType = reflect.TypeOf((*CrawlerService)(nil))
func init() {
cdr.RegisterType(CrawlerServiceType)
}
type CrawlerService struct {
cda.TypeAnnotation `annotation:"@overflow:RPCService()"`
SensorConfigService *SensorConfigService `annotation:"@Inject()"`
2018-04-19 07:12:56 +00:00
Crawlers map[string]crawler.Crawler `annotation:"@Resource(name='CONTAINER_CRAWLERS')"`
2018-04-19 06:24:45 +00:00
}
func (s *CrawlerService) InitService() error {
if nil == s.Crawlers {
return fmt.Errorf("Crawlers is not set")
}
return nil
}
func (s *CrawlerService) StartService() error {
return nil
}
func (s *CrawlerService) StopService() {
}
func (s *CrawlerService) DestroyService() {
}
2018-04-23 10:21:10 +00:00
func (s *CrawlerService) Auth(crawlerName string, auth map[string]interface{}) error {
2018-04-19 06:24:45 +00:00
_crawler, ok := s.Crawlers[crawlerName]
if !ok {
return fmt.Errorf("There is no crawler[%s]", crawlerName)
}
logging.Logger().Debugf("Auth invoked with [%v]", auth)
err := _crawler.Auth(auth)
if nil != err {
return fmt.Errorf("Failed to authentication from crawler[%s] %v", crawlerName, err)
}
logging.Logger().Debugf("Auth success with[%v]", auth)
return nil
}
func (s *CrawlerService) Get(sensorConfigID string) (map[string]string, error) {
sensorConfig, ok := s.SensorConfigService.sensorConfigs[sensorConfigID]
if !ok {
return nil, fmt.Errorf("There is no sensor config for id[%s]", sensorConfigID)
}
2018-07-04 02:35:52 +00:00
_crawlerName := sensorConfig.Crawler.MetaCrawlerKey
2018-04-19 06:24:45 +00:00
_crawler, ok := s.Crawlers[_crawlerName]
if !ok {
return nil, fmt.Errorf("There is no crawler[%s] for id[%s]", _crawlerName, sensorConfigID)
}
logging.Logger().Debugf("Get 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("Get success config[%v]", result)
return result, nil
}