crawler_manager_go/crawler_communicator.go
2017-04-27 20:24:35 +09:00

196 lines
3.6 KiB
Go

package crawler_manager
import (
"google.golang.org/grpc"
"log"
g "loafle.com/overflow/crawler_go/grpc"
"context"
"loafle.com/overflow/agent_api/config_manager"
"encoding/json"
)
func callAdd(container *string, conf *config_manager.Config) bool {
port := GetInstance().portMap[*container]
conn, err := grpc.Dial(address+port, grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %v", err)
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 {
log.Println(err)
}
log.Println("callAdd:", out)
return true
}
func callInitConfig(container *string, cl []*config_manager.Config) bool {
port := GetInstance().portMap[*container]
conn, err := grpc.Dial(address+port, grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %v", err)
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 {
log.Println(errInit)
return false
}
log.Println("callInit:",outInit)
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{}
// //in.Path = "/home/snoop/develop/path/go/src/loafle.com/overflow/crawler_go/config/"
// 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 {
log.Fatalf("did not connect: %v", err)
}
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 {
log.Println(errRem)
}
log.Println("callRemove:",outRem)
}
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 {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
c := g.NewStatusClient(conn)
e := &g.Empty{}
out, err := c.Status(context.Background(), e)
if err != nil {
log.Fatalf("could not greet: %v", err)
}
return out.Check
}