2017-04-13 03:43:39 +00:00
|
|
|
package collector_go
|
|
|
|
|
|
|
|
import (
|
2017-05-11 09:34:47 +00:00
|
|
|
"context"
|
2017-04-28 04:16:08 +00:00
|
|
|
confMng "loafle.com/overflow/agent_api/config_manager"
|
2017-05-11 09:34:47 +00:00
|
|
|
"loafle.com/overflow/crawler_go/grpc"
|
|
|
|
crm "loafle.com/overflow/crawler_manager_go"
|
|
|
|
s "loafle.com/overflow/scheduler_go"
|
2017-04-13 03:43:39 +00:00
|
|
|
"log"
|
2017-05-11 09:34:47 +00:00
|
|
|
"strconv"
|
2017-04-28 04:16:08 +00:00
|
|
|
"sync"
|
2017-04-15 11:32:56 +00:00
|
|
|
"time"
|
2017-04-13 03:43:39 +00:00
|
|
|
)
|
|
|
|
|
2017-04-28 04:16:08 +00:00
|
|
|
var (
|
|
|
|
instance *Collector
|
|
|
|
once sync.Once
|
|
|
|
)
|
2017-04-13 03:43:39 +00:00
|
|
|
|
2017-04-28 04:16:08 +00:00
|
|
|
func init() {
|
2017-05-11 09:34:47 +00:00
|
|
|
handleConfigLoaded()
|
2017-04-14 10:09:51 +00:00
|
|
|
}
|
2017-04-13 03:43:39 +00:00
|
|
|
|
2017-04-28 04:16:08 +00:00
|
|
|
func GetInstance() *Collector {
|
|
|
|
once.Do(func() {
|
|
|
|
instance = &Collector{}
|
|
|
|
})
|
|
|
|
return instance
|
|
|
|
}
|
2017-04-15 11:32:56 +00:00
|
|
|
|
2017-04-28 04:16:08 +00:00
|
|
|
type Collector struct {
|
2017-05-11 09:34:47 +00:00
|
|
|
scheduler *s.Scheduler
|
|
|
|
cm confMng.ConfigManager
|
|
|
|
|
|
|
|
startSensorCh chan interface{}
|
|
|
|
stopSensorCh chan interface{}
|
|
|
|
addSensorCh chan interface{}
|
|
|
|
remSensorCh chan interface{}
|
|
|
|
updateSensorCh chan interface{}
|
|
|
|
crmSensorUpdateCh chan interface{}
|
|
|
|
crmUpdateCh chan interface{}
|
|
|
|
crmUpdateDoneCh chan interface{}
|
2017-04-28 04:16:08 +00:00
|
|
|
}
|
2017-04-15 11:32:56 +00:00
|
|
|
|
2017-04-28 04:16:08 +00:00
|
|
|
func (c *Collector) start(conf confMng.ConfigManager) {
|
|
|
|
go func() {
|
|
|
|
c.cm = conf
|
2017-05-11 09:34:47 +00:00
|
|
|
c.scheduler = &s.Scheduler{}
|
|
|
|
c.scheduler.Start()
|
|
|
|
c.addObservers()
|
|
|
|
c.notifyCollectorReady()
|
2017-04-15 11:32:56 +00:00
|
|
|
|
2017-04-28 04:16:08 +00:00
|
|
|
for _, conf := range c.cm.GetSensors() {
|
|
|
|
if err := c.addSensor(conf.Id); err != nil {
|
2017-04-15 11:32:56 +00:00
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2017-05-11 09:34:47 +00:00
|
|
|
func (c *Collector) addObservers() {
|
|
|
|
|
|
|
|
c.startSensorCh = make(chan interface{})
|
|
|
|
c.stopSensorCh = make(chan interface{})
|
|
|
|
c.addSensorCh = make(chan interface{})
|
|
|
|
c.remSensorCh = make(chan interface{})
|
|
|
|
c.updateSensorCh = make(chan interface{})
|
|
|
|
c.crmSensorUpdateCh = make(chan interface{})
|
|
|
|
c.crmUpdateCh = make(chan interface{})
|
|
|
|
c.crmUpdateDoneCh = make(chan interface{})
|
|
|
|
|
|
|
|
go c.handleAgentWillStop()
|
|
|
|
go c.handleSensorStart()
|
|
|
|
go c.handleSensorStop()
|
|
|
|
go c.handleSensorAdd()
|
|
|
|
go c.handleSensorRemove()
|
|
|
|
go c.handleSensorUpdate()
|
|
|
|
go c.handleCrmSensorUpdateDone()
|
|
|
|
go c.handleCrawlerUpdate()
|
|
|
|
go c.handleCrawlerUpdateDone()
|
|
|
|
}
|
|
|
|
|
2017-05-11 10:05:37 +00:00
|
|
|
func (c *Collector) Stop(d interface{}) {
|
2017-05-11 09:34:47 +00:00
|
|
|
c.cleanObserver(c.addSensorCh, c.remSensorCh)
|
2017-04-14 10:09:51 +00:00
|
|
|
c.scheduler.RemoveAllSchedule()
|
2017-04-15 11:32:56 +00:00
|
|
|
c.scheduler.Stop()
|
2017-05-11 09:34:47 +00:00
|
|
|
c.notifyCollectorStopped()
|
2017-04-14 10:09:51 +00:00
|
|
|
}
|
2017-04-13 03:43:39 +00:00
|
|
|
|
2017-04-28 04:16:08 +00:00
|
|
|
func (c *Collector) collect(id string) {
|
2017-04-13 03:43:39 +00:00
|
|
|
|
2017-04-28 04:16:08 +00:00
|
|
|
conf := c.cm.GetSensorById(id)
|
|
|
|
log.Printf("COLLECT %s - [ID: %s] [Crawler : %s]", time.Now(), conf.Id, conf.Crawler.Name)
|
2017-05-11 09:34:47 +00:00
|
|
|
conn, err := crm.GetInstance().GetClient(conf.Crawler.Container)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
2017-04-15 11:32:56 +00:00
|
|
|
|
2017-05-11 09:34:47 +00:00
|
|
|
dc := grpc.NewDataClient(conn)
|
|
|
|
in := &grpc.Input{}
|
2017-04-15 11:32:56 +00:00
|
|
|
|
2017-05-11 09:34:47 +00:00
|
|
|
in.Id = id
|
|
|
|
in.Name = grpc.Crawlers(grpc.Crawlers_value[conf.Crawler.Name])
|
2017-04-15 11:32:56 +00:00
|
|
|
|
2017-05-11 09:34:47 +00:00
|
|
|
out, err := dc.Get(context.Background(), in)
|
2017-04-15 11:32:56 +00:00
|
|
|
|
2017-05-11 09:34:47 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
log.Println(out)
|
2017-04-15 11:32:56 +00:00
|
|
|
|
2017-05-11 09:34:47 +00:00
|
|
|
c.notifyData(out)
|
2017-04-15 11:32:56 +00:00
|
|
|
}
|
|
|
|
|
2017-04-28 04:16:08 +00:00
|
|
|
func (c *Collector) addSensor(sensorId string) error {
|
|
|
|
sensor := c.cm.GetSensorById(sensorId)
|
2017-05-11 09:34:47 +00:00
|
|
|
interval, err := strconv.Atoi(sensor.Schedule.Interval)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.scheduler.NewSchedule(sensorId, uint64(interval), c.collect)
|
2017-04-14 10:09:51 +00:00
|
|
|
}
|
2017-04-13 03:43:39 +00:00
|
|
|
|
2017-04-28 04:16:08 +00:00
|
|
|
func (c *Collector) removeSensor(id string) {
|
|
|
|
if err := c.scheduler.RemoveSchedule(id); err != nil {
|
|
|
|
log.Println(err)
|
2017-04-13 03:43:39 +00:00
|
|
|
}
|
2017-04-14 10:09:51 +00:00
|
|
|
}
|
2017-05-11 09:34:47 +00:00
|
|
|
|
|
|
|
func (c *Collector) updateSensor(id string) {
|
|
|
|
//update
|
|
|
|
}
|