overflow_probe/crawler_manager/crawler_communicator.go

201 lines
3.9 KiB
Go
Raw Normal View History

2017-08-03 10:08:34 +00:00
package crawler_manager
import (
"context"
2017-08-04 02:32:31 +00:00
g "git.loafle.net/overflow/overflow_probe/crawler/grpc"
2017-08-04 03:00:26 +00:00
log "github.com/cihub/seelog"
2017-08-04 03:08:32 +00:00
"google.golang.org/grpc"
2017-08-03 10:08:34 +00:00
"encoding/json"
2017-08-04 02:32:31 +00:00
"git.loafle.net/overflow/overflow_probe/agent_api/config_manager"
2017-08-03 10:08:34 +00:00
)
func callAdd(container *string, conf *config_manager.Config) bool {
port := GetInstance().portMap[*container]
conn, err := grpc.Dial(address+port, grpc.WithInsecure())
if err != nil {
2017-08-04 03:00:26 +00:00
//log.Fatalf("did not connect: %v", err)
2017-08-04 03:08:32 +00:00
log.Error(err)
2017-08-03 10:08:34 +00:00
return false
}
defer conn.Close()
cc := g.NewConfigClient(conn)
in := &g.InputAdd{}
b, err := json.Marshal(conf)
if err != nil {
return false
}
in.Data = b
in.Name = g.Crawlers(g.Crawlers_value[conf.Crawler.Name])
out, err := cc.Add(context.Background(), in)
if err != nil {
2017-08-04 03:00:26 +00:00
log.Error(err)
2017-08-03 10:08:34 +00:00
}
2017-08-04 03:00:26 +00:00
//log.Println("callAdd:", out)
log.Debug("callAdd:", out)
2017-08-03 10:08:34 +00:00
return true
}
func callInitConfigOne(container *string, conf *config_manager.Config) bool {
var cl []*config_manager.Config
cl = append(cl, conf)
return callInitConfig(container, cl)
}
func callInitConfig(container *string, cl []*config_manager.Config) bool {
port := GetInstance().portMap[*container]
conn, err := grpc.Dial(address+port, grpc.WithInsecure())
if err != nil {
2017-08-04 03:00:26 +00:00
//log.Fatalf("did not connect: %v", err)
2017-08-04 03:08:32 +00:00
log.Error(err)
2017-08-03 10:08:34 +00:00
return false
}
defer conn.Close()
cc := g.NewConfigClient(conn)
inArr := &g.InputArray{}
for _, conf := range cl {
in := &g.Init{}
in.Name = g.Crawlers(g.Crawlers_value[conf.Crawler.Name])
b, err := json.Marshal(conf)
if err != nil {
continue
}
in.Data = b
inArr.In = append(inArr.In, in)
}
outInit, errInit := cc.Init(context.Background(), inArr)
if errInit != nil {
2017-08-04 03:00:26 +00:00
log.Debug(errInit)
2017-08-03 10:08:34 +00:00
return false
}
2017-08-04 03:00:26 +00:00
log.Debug("callInit:", outInit)
2017-08-03 10:08:34 +00:00
return true
}
//func callInit(container *string, paths *[]string) bool {
//
// port := GetInstance().portMap[*container]
//
// return callInitAddress(address + port, paths)
//}
//
//
//func callInitAddress(address string, paths *[]string) bool {
// conn, err := grpc.Dial(address, grpc.WithInsecure())
// if err != nil {
// log.Fatalf("did not connect: %v", err)
// return false
// }
// defer conn.Close()
//
// cc := g.NewConfigClient(conn)
//
// inArr := &g.InputArray{}
// base := "HEALTH_"
// for _, path := range *paths {
//
// in := &g.Init{}
2017-08-04 02:32:31 +00:00
// //in.Path = "/home/snoop/develop/path/go/src/git.loafle.net/overflow/overflow_probe/crawler/config/"
2017-08-03 10:08:34 +00:00
// in.Path = path + "/"
// bcn := filepath.Base(path)
// bcn = strings.ToUpper(bcn)
//
// in.Name = g.Crawlers(g.Crawlers_value[base+bcn])
// //in.Name = g.Crawlers(g.Crawlers_value[g.Crawlers_HEALTH_REDIS.String()]) //test
// inArr.In = append(inArr.In, in)
// }
//
// outInit, errInit := cc.Init(context.Background(), inArr)
// if errInit != nil {
// log.Println(errInit)
// return false
// }
// log.Println("callInit:",outInit)
//
// return true
//}
func callRemove(container *string, conf *config_manager.Config) {
port := GetInstance().portMap[*container]
conn, err := grpc.Dial(address+port, grpc.WithInsecure())
if err != nil {
2017-08-04 03:00:26 +00:00
//log.Fatalf("did not connect: %v", err)
2017-08-04 03:08:32 +00:00
log.Error(err)
2017-08-03 10:08:34 +00:00
}
defer conn.Close()
cc := g.NewConfigClient(conn)
inR := &g.Input{}
//inR.Id = *id //FIXME
inR.Name = g.Crawlers(g.Crawlers_value[conf.Crawler.Name])
outRem, errRem := cc.Remove(context.Background(), inR)
if errRem != nil {
2017-08-04 03:00:26 +00:00
log.Debug(errRem)
2017-08-03 10:08:34 +00:00
}
2017-08-04 03:00:26 +00:00
log.Debug("callRemove:", outRem)
2017-08-03 10:08:34 +00:00
}
func callStatus(container *string) bool {
port := GetInstance().portMap[*container]
if port == "" {
return false
}
return callStatusAddress(address + port)
}
func callStatusAddress(addr string) bool {
conn, err := grpc.Dial(addr, grpc.WithInsecure())
if err != nil {
2017-08-04 03:00:26 +00:00
log.Error("did not connect: ", err)
2017-08-03 10:08:34 +00:00
}
defer conn.Close()
c := g.NewStatusClient(conn)
e := &g.Empty{}
out, err := c.Status(context.Background(), e)
if err != nil {
2017-08-04 03:00:26 +00:00
log.Error("could not greet: ", err)
2017-08-03 10:08:34 +00:00
}
return out.Check
}