This commit is contained in:
snoop 2017-04-27 20:24:35 +09:00
parent b4d709693c
commit 5343329213
3 changed files with 115 additions and 54 deletions

View File

@ -1,20 +1,20 @@
package crawler_manager package crawler_manager
import ( import (
"path/filepath"
"google.golang.org/grpc" "google.golang.org/grpc"
"log" "log"
g "loafle.com/overflow/crawler_go/grpc" g "loafle.com/overflow/crawler_go/grpc"
"context" "context"
"strings"
"loafle.com/overflow/agent_api/config_manager" "loafle.com/overflow/agent_api/config_manager"
"encoding/json" "encoding/json"
) )
func callAdd(container *string, crawlerName *string, id *string) bool { func callAdd(container *string, conf *config_manager.Config) bool {
port := GetInstance().portMap[*container] port := GetInstance().portMap[*container]
@ -27,10 +27,15 @@ func callAdd(container *string, crawlerName *string, id *string) bool {
cc := g.NewConfigClient(conn) cc := g.NewConfigClient(conn)
in := &g.Input{} in := &g.InputAdd{}
in.Id = *id b, err := json.Marshal(conf)
in.Name = g.Crawlers(g.Crawlers_value[*crawlerName]) 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) out, err := cc.Add(context.Background(), in)
if err != nil { if err != nil {
@ -68,8 +73,7 @@ func callInitConfig(container *string, cl []*config_manager.Config) bool {
continue continue
} }
in.Path = string(b) in.Data = b
inArr.In = append(inArr.In, in) inArr.In = append(inArr.In, in)
} }
@ -86,53 +90,53 @@ func callInitConfig(container *string, cl []*config_manager.Config) bool {
} }
func callInit(container *string, paths *[]string) bool { //func callInit(container *string, paths *[]string) bool {
//
port := GetInstance().portMap[*container] // port := GetInstance().portMap[*container]
//
return callInitAddress(address + port, paths) // 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 callInitAddress(address string, paths *[]string) bool { func callRemove(container *string, conf *config_manager.Config) {
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, crawlerName *string, id *string) {
port := GetInstance().portMap[*container] port := GetInstance().portMap[*container]
@ -146,8 +150,8 @@ func callRemove(container *string, crawlerName *string, id *string) {
cc := g.NewConfigClient(conn) cc := g.NewConfigClient(conn)
inR := &g.Input{} inR := &g.Input{}
inR.Id = *id //inR.Id = *id //FIXME
inR.Name = g.Crawlers(g.Crawlers_value[*crawlerName]) inR.Name = g.Crawlers(g.Crawlers_value[conf.Crawler.Name])
outRem, errRem := cc.Remove(context.Background(), inR) outRem, errRem := cc.Remove(context.Background(), inR)
if errRem != nil { if errRem != nil {

View File

@ -94,6 +94,20 @@ func (c *CrawlerManager)checkContainer(container *string) bool {
return callStatus(container) return callStatus(container)
} }
func (c *CrawlerManager)checkAndRunContainer(container *string) bool {
b := callStatus(container)
if b == false {
err := c.runAndInitContainerOne(container)
if err != nil {
return false
}
}
return true
}
func (c *CrawlerManager)checkPid() { func (c *CrawlerManager)checkPid() {
@ -216,6 +230,33 @@ func (c *CrawlerManager)runContainer(container *string) error {
return nil return nil
} }
func (c *CrawlerManager)addSensor(id string) {
conf := c.ConfigMgr.GetCrawlerById(id)
b := c.checkAndRunContainer(&conf.Crawler.Name)
if b == false {
return
}
b = callAdd(&conf.Crawler.Container, conf)
if b {
//FIXME :: Noti ADD_SENSOR_2_END
}
}
func (c *CrawlerManager)removeSensor(id string) {
conf := c.ConfigMgr.GetCrawlerById(id)
callRemove(&conf.Crawler.Name, conf)
//remove and stop
}
func (c *CrawlerManager)stopContainerAll() { func (c *CrawlerManager)stopContainerAll() {

View File

@ -13,6 +13,7 @@ func listenEvent() {
go listenConfigLoaded(); go listenConfigLoaded();
go listenAddSensor(); go listenAddSensor();
go listenRemoveSensor();
} }
@ -40,6 +41,21 @@ func listenAddSensor() {
str := o.(string) str := o.(string)
fmt.Println(str) fmt.Println(str)
//GetInstance().Addconfig(str)
GetInstance().addSensor(str)
}
func listenRemoveSensor() {
ch := make(chan interface{}, 0)
//observer.Add(messages.REMOVE_SENSOR_2_END, ch);
o := <-ch
str := o.(string)
GetInstance().removeSensor(str)
} }