crawler_manager_go/crawler_communicator.go

205 lines
3.8 KiB
Go
Raw Permalink Normal View History

2017-04-14 02:29:55 +00:00
package crawler_manager
import (
2017-04-27 11:24:35 +00:00
2017-04-14 02:29:55 +00:00
"google.golang.org/grpc"
"log"
2017-05-29 11:06:34 +00:00
g "git.loafle.net/overflow/crawler_go/grpc"
2017-04-14 02:29:55 +00:00
"context"
2017-04-14 07:23:42 +00:00
2017-04-15 11:10:33 +00:00
2017-04-27 11:24:35 +00:00
2017-05-29 11:06:34 +00:00
"git.loafle.net/overflow/agent_api/config_manager"
2017-04-27 09:07:56 +00:00
"encoding/json"
2017-04-14 02:29:55 +00:00
)
2017-04-27 11:24:35 +00:00
func callAdd(container *string, conf *config_manager.Config) bool {
2017-04-14 02:29:55 +00:00
2017-04-15 11:10:33 +00:00
port := GetInstance().portMap[*container]
conn, err := grpc.Dial(address+port, grpc.WithInsecure())
2017-04-14 02:29:55 +00:00
if err != nil {
log.Fatalf("did not connect: %v", err)
2017-04-15 11:10:33 +00:00
return false
2017-04-14 02:29:55 +00:00
}
defer conn.Close()
cc := g.NewConfigClient(conn)
2017-04-27 11:24:35 +00:00
in := &g.InputAdd{}
b, err := json.Marshal(conf)
if err != nil {
return false
}
2017-04-14 02:29:55 +00:00
2017-04-27 11:24:35 +00:00
in.Data = b
in.Name = g.Crawlers(g.Crawlers_value[conf.Crawler.Name])
2017-04-14 02:29:55 +00:00
out, err := cc.Add(context.Background(), in)
if err != nil {
log.Println(err)
}
2017-04-15 11:10:33 +00:00
log.Println("callAdd:", out)
return true
}
2017-05-16 06:11:32 +00:00
func callInitConfigOne(container* string, conf *config_manager.Config) bool {
var cl []*config_manager.Config
cl = append(cl , conf)
return callInitConfig(container, cl)
}
2017-04-15 11:10:33 +00:00
2017-04-27 09:07:56 +00:00
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
}
2017-04-27 11:24:35 +00:00
in.Data = b
2017-04-27 09:07:56 +00:00
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
}
2017-04-27 11:24:35 +00:00
//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) {
2017-04-15 11:10:33 +00:00
port := GetInstance().portMap[*container]
2017-04-14 02:29:55 +00:00
conn, err := grpc.Dial(address+port, grpc.WithInsecure())
2017-04-15 11:10:33 +00:00
2017-04-14 02:29:55 +00:00
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
cc := g.NewConfigClient(conn)
2017-04-15 11:10:33 +00:00
inR := &g.Input{}
2017-04-27 11:24:35 +00:00
//inR.Id = *id //FIXME
inR.Name = g.Crawlers(g.Crawlers_value[conf.Crawler.Name])
2017-04-14 02:29:55 +00:00
2017-04-15 11:10:33 +00:00
outRem, errRem := cc.Remove(context.Background(), inR)
if errRem != nil {
log.Println(errRem)
2017-04-14 02:29:55 +00:00
}
2017-04-15 11:10:33 +00:00
log.Println("callRemove:",outRem)
2017-04-14 02:29:55 +00:00
}
2017-04-15 11:16:14 +00:00
func callStatus(container *string) bool {
2017-04-14 02:29:55 +00:00
2017-04-15 11:16:14 +00:00
port := GetInstance().portMap[*container]
2017-04-14 02:29:55 +00:00
2017-04-15 11:10:33 +00:00
if port == "" {
return false
2017-04-14 02:29:55 +00:00
}
2017-04-15 11:10:33 +00:00
return callStatusAddress(address +port)
2017-04-14 02:29:55 +00:00
}
2017-04-15 11:10:33 +00:00
func callStatusAddress(addr string) bool {
conn, err := grpc.Dial(addr, grpc.WithInsecure())
2017-04-14 02:29:55 +00:00
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
2017-04-15 11:10:33 +00:00
c := g.NewStatusClient(conn)
e := &g.Empty{}
out, err := c.Status(context.Background(), e)
2017-04-14 02:29:55 +00:00
if err != nil {
log.Fatalf("could not greet: %v", err)
}
2017-04-15 11:10:33 +00:00
return out.Check
2017-04-14 02:29:55 +00:00
}