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((*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) } 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 }