crawler_manager_go/crawler_communicator.go
2017-04-14 17:19:02 +09:00

147 lines
2.7 KiB
Go

package crawler_manager
import (
"path/filepath"
"google.golang.org/grpc"
"log"
g "loafle.com/overflow/crawler_go/grpc"
"context"
"encoding/json"
"strings"
)
func CallAdd() {
conn, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
cc := g.NewConfigClient(conn)
in := &g.Input{}
in.Id = "test_redis_sid"
in.Name = g.Crawlers_HEALTH_REDIS
out, err := cc.Add(context.Background(), in)
if err != nil {
log.Println(err)
}
log.Println(out)
}
func CallInit2(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)
log.Println("cn:" + base+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(outInit)
return true
}
func CallInit(crawlerName string, path string) {
port := portMap[crawlerName]
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)
in := &g.Init{}
in.Path = filepath.Dir(path)+ "/"
in.Name = g.Crawlers(g.Crawlers_value[crawlerName])
inArr := &g.InputArray{}
inArr.In = append(inArr.In, in)
outInit, errInit := cc.Init(context.Background(), inArr)
if errInit != nil {
log.Println(errInit)
}
log.Println(outInit)
}
func CallRemove() {
conn, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
cc := g.NewConfigClient(conn)
inR := &g.Input{}
inR.Id = "123123"
inR.Name = g.Crawlers_HEALTH_DNS
outRem, errRem := cc.Remove(context.Background(), inR)
if errRem != nil {
log.Println(errRem)
}
log.Println(outRem)
}
func CallGet() {
conn, err := grpc.Dial("localhost:50000", grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
c := g.NewDataClient(conn)
in := &g.Input{
Name: g.Crawlers_HEALTH_REDIS,
Id: "test_redis_sid",
}
out, err := c.Get(context.Background(), in)
if err != nil {
log.Fatalf("could not greet: %v", err)
}
var check bool
json.Unmarshal(out.Data, &check)
log.Println(check)
}