From f073163bf7720272e08616dd91159c54c5721eb9 Mon Sep 17 00:00:00 2001 From: crusader Date: Thu, 15 Mar 2018 22:52:23 +0900 Subject: [PATCH] ing --- client/container/client.go | 40 ++++++++++++++--- client/container/client_handlers.go | 30 ------------- client/container/socket_handlers.go | 26 ----------- commons/annotation/service.go | 22 +++++++++ config.json | 3 -- glide.yaml | 1 + probe/probe.go | 22 +++------ service/CentralService.go | 4 +- service/ConfigService.go | 4 +- service/ContainerService.go | 47 ++++++++++---------- service/ContainerService_test.go | 69 +++++++++++++++++++++++++++++ service/CrawlerService.go | 3 +- service/DiscoveryService.go | 4 +- service/LogService.go | 3 +- service/ProbeService.go | 3 +- service/SensorService.go | 3 +- service/service.go | 68 +++++++++++++--------------- 17 files changed, 204 insertions(+), 148 deletions(-) delete mode 100644 client/container/client_handlers.go delete mode 100644 client/container/socket_handlers.go create mode 100644 commons/annotation/service.go create mode 100644 service/ContainerService_test.go diff --git a/client/container/client.go b/client/container/client.go index c165670..89e9bd3 100644 --- a/client/container/client.go +++ b/client/container/client.go @@ -1,16 +1,46 @@ -package client +package container import ( + "fmt" + "os/exec" + "path" + crc "git.loafle.net/commons_go/rpc/client" crr "git.loafle.net/commons_go/rpc/registry" + ooccp "git.loafle.net/overflow/overflow_commons_go/config/probe" oopcc "git.loafle.net/overflow/overflow_probe_container/client" + "git.loafle.net/overflow/overflow_probes/config" ) -func New(addr string, rpcInvoker crr.RPCInvoker) crc.Client { - ch := oopcc.NewClientHandler(rpcInvoker) - socketHandler := NewSocketHandler() +func New(port int, rpcInvoker crr.RPCInvoker) crc.Client { + addr := fmt.Sprintf("localhost:%d", port) - c := oopcc.New(addr, ch, socketHandler) + c := oopcc.New(addr, rpcInvoker) return c } + +func GetContainerCommand(name string) (cmd *exec.Cmd, pidPath string) { + pidPath = path.Join(config.Config.Paths["root"], ooccp.PathPID, name+".pid") + + switch name { + case ooccp.ContainerGeneralName: + javaPath := path.Join(config.Config.Paths["root"], ooccp.PathJRE, "bin", "java") + jarPath := path.Join(config.Config.Paths["root"], ooccp.PathBin, ooccp.ContainerGeneralFileName) + // arg := fmt.Sprintf("-jar %s %s", jarPath, pidPath) + + cmd = exec.Command(javaPath, "-jar", jarPath, pidPath) + case ooccp.ContainerNetworkName: + exePath := path.Join(config.Config.Paths["root"], ooccp.PathBin, ooccp.ContainerNetworkFileName) + arg := fmt.Sprintf("-pid-path=%s", pidPath) + + cmd = exec.Command(exePath, arg) + case ooccp.ContainerDiscoveryName: + exePath := path.Join(config.Config.Paths["root"], ooccp.PathBin, ooccp.ContainerDiscoveryFileName) + arg := fmt.Sprintf("-pid-path=%s", pidPath) + + cmd = exec.Command(exePath, arg) + default: + } + return +} diff --git a/client/container/client_handlers.go b/client/container/client_handlers.go deleted file mode 100644 index d7a804e..0000000 --- a/client/container/client_handlers.go +++ /dev/null @@ -1,30 +0,0 @@ -package client - -import ( - crc "git.loafle.net/commons_go/rpc/client" - crr "git.loafle.net/commons_go/rpc/registry" - oopcc "git.loafle.net/overflow/overflow_probe_container/client" -) - -type ClientHandlers struct { - oopcc.ClientHandler -} - -func (ch *ClientHandlers) Init(clientCTX crc.ClientContext) error { - - return ch.ClientHandler.Init(clientCTX) -} - -func (ch *ClientHandlers) Destroy(clientCTX crc.ClientContext) { - ch.ClientHandler.Destroy(clientCTX) -} - -func (ch *ClientHandlers) Validate() { - ch.ClientHandler.Validate() -} - -func NewClientHandler(rpcInvoker crr.RPCInvoker) oopcc.ClientHandler { - ch := &ClientHandlers{} - ch.ClientHandler = oopcc.NewClientHandler(rpcInvoker) - return ch -} diff --git a/client/container/socket_handlers.go b/client/container/socket_handlers.go deleted file mode 100644 index 0f8a267..0000000 --- a/client/container/socket_handlers.go +++ /dev/null @@ -1,26 +0,0 @@ -package client - -import ( - "net" - - csc "git.loafle.net/commons_go/server/client" -) - -type SocketHandlers struct { - csc.SocketHandler -} - -func (sh *SocketHandlers) OnConnect(socketContext csc.SocketContext, conn net.Conn) { - // no op -} - -func (sh *SocketHandlers) OnDisconnect(soc csc.Socket) { - // no op -} - -func (sh *SocketHandlers) Validate() { -} - -func NewSocketHandler() csc.SocketHandler { - return &SocketHandlers{} -} diff --git a/commons/annotation/service.go b/commons/annotation/service.go new file mode 100644 index 0000000..f473335 --- /dev/null +++ b/commons/annotation/service.go @@ -0,0 +1,22 @@ +package annotation + +// @Service() +// inherit @Component +import ( + "reflect" + + cda "git.loafle.net/commons_go/di/annotation" + cdia "git.loafle.net/commons_go/di/injection/annotation" +) + +const ( + ServiceTag = "@overFlow:Service" +) + +func init() { + cda.RegisterAnnotation(ServiceTag, reflect.TypeOf((*Service)(nil))) +} + +type Service struct { + cdia.Component +} diff --git a/config.json b/config.json index 18bac49..3148205 100644 --- a/config.json +++ b/config.json @@ -9,9 +9,6 @@ "key": "95d8bcdc739741dca74c4a0e489e0774" }, "paths": { - "bin": "/bin", - "config": "/config", - "pid": "/pid", "root": "/project/overFlow/probe" } } \ No newline at end of file diff --git a/glide.yaml b/glide.yaml index 781049c..94c9f3a 100644 --- a/glide.yaml +++ b/glide.yaml @@ -7,3 +7,4 @@ import: version: v2.17.08 - package: github.com/dgrijalva/jwt-go version: v3.1.0 +- package: git.loafle.net/commons_go/di diff --git a/probe/probe.go b/probe/probe.go index a3eaddd..90f92d9 100644 --- a/probe/probe.go +++ b/probe/probe.go @@ -11,7 +11,7 @@ import ( oogwc "git.loafle.net/overflow/overflow_gateway_websocket/client" oopccd "git.loafle.net/overflow/overflow_probes/client/central/data" oopccp "git.loafle.net/overflow/overflow_probes/client/central/probe" - "git.loafle.net/overflow/overflow_probes/service" + oopca "git.loafle.net/overflow/overflow_probes/commons/annotation" ) func New() ProbeManager { @@ -45,23 +45,13 @@ func (pm *probeManagers) Start() error { } cdr.RegisterResource("CentralClients", centralClients) - centralService := service.GetService("CentralService").(*service.CentralService) - configService := service.GetService("ConfigService").(*service.ConfigService) - crawlerService := service.GetService("CrawlerService").(*service.CrawlerService) - discoveryService := service.GetService("DiscoveryService").(*service.DiscoveryService) - logService := service.GetService("LogService").(*service.LogService) - probeService := service.GetService("ProbeService").(*service.ProbeService) - sensorService := service.GetService("SensorService").(*service.SensorService) + services := cdr.GetInstancesByAnnotationName(oopca.ServiceTag) - probeRPCRegistry.RegisterService(centralService, "") - probeRPCRegistry.RegisterService(configService, "") - probeRPCRegistry.RegisterService(crawlerService, "") - probeRPCRegistry.RegisterService(discoveryService, "") - probeRPCRegistry.RegisterService(logService, "") - probeRPCRegistry.RegisterService(probeService, "") - probeRPCRegistry.RegisterService(sensorService, "") + for _, s := range services { + probeRPCRegistry.RegisterService(s, "") + } - logging.Logger().Debug(fmt.Sprintf("%v", centralService.CentralClients)) + // logging.Logger().Debug(fmt.Sprintf("%v", centralService.CentralClients)) if err := centralProbeClient.Connect(); nil != err { return err diff --git a/service/CentralService.go b/service/CentralService.go index 38bc8cd..97969e8 100644 --- a/service/CentralService.go +++ b/service/CentralService.go @@ -9,10 +9,12 @@ import ( ) func init() { - cdr.RegisterType(reflect.TypeOf((*CentralService)(nil)), &cda.ComponentAnnotation{}) + cdr.RegisterType(reflect.TypeOf((*CentralService)(nil))) } type CentralService struct { + cda.TypeAnnotation `annotation:"@overFlow:Service()"` + CentralClients map[string]oogwc.Client `annotation:"@Resource()"` } diff --git a/service/ConfigService.go b/service/ConfigService.go index 0d8f065..d2fa8a6 100644 --- a/service/ConfigService.go +++ b/service/ConfigService.go @@ -8,8 +8,10 @@ import ( ) func init() { - cdr.RegisterType(reflect.TypeOf((*ConfigService)(nil)), &cda.ComponentAnnotation{}) + cdr.RegisterType(reflect.TypeOf((*ConfigService)(nil))) + } type ConfigService struct { + cda.TypeAnnotation `annotation:"@overFlow:Service()"` } diff --git a/service/ContainerService.go b/service/ContainerService.go index e6e1842..f80a4e1 100644 --- a/service/ContainerService.go +++ b/service/ContainerService.go @@ -4,25 +4,23 @@ import ( "fmt" "io/ioutil" "os" - "os/exec" - "path" "reflect" "strconv" "time" cda "git.loafle.net/commons_go/di/annotation" cdr "git.loafle.net/commons_go/di/registry" - "git.loafle.net/commons_go/logging" crc "git.loafle.net/commons_go/rpc/client" - oopcc "git.loafle.net/overflow/overflow_probe_container/client" - "git.loafle.net/overflow/overflow_probes/config" + "git.loafle.net/overflow/overflow_probes/client/container" ) func init() { - cdr.RegisterType(reflect.TypeOf((*ContainerService)(nil)), &cda.ComponentAnnotation{}) + cdr.RegisterType(reflect.TypeOf((*ContainerService)(nil))) } type ContainerService struct { + cda.TypeAnnotation `annotation:"@overFlow:Service()"` + clients map[string]*containerState } @@ -71,33 +69,33 @@ func (cs *ContainerService) runProcess(name string) error { return nil } - cmdPath := path.Join(config.Config.Paths["root"], config.Config.Paths["bin"], name+".sh") - pidPath := path.Join(config.Config.Paths["root"], config.Config.Paths["pid"], name+".pid") + // cmd, pidPath := container.GetContainerCommand(name) + // if err := cmd.Start(); nil != err { + // logging.Logger().Error(fmt.Sprintf("Probe: To run container(%s) failed err %v", name, err)) + // return err + // } - cmd := exec.Command(cmdPath, pidPath) - if err := cmd.Start(); nil != err { - logging.Logger().Error(fmt.Sprintf("Probe: To run container(%s) failed err %v", name, err)) - return err - } + // port, err := watchPidFileCreate(pidPath, time.Duration(time.Second*2)) + // if nil != err { + // return err + // } + port := 60000 - port, err := watchPidFileCreate(pidPath, time.Duration(time.Second*2)) - if nil != err { - return err - } - - clientAddr := fmt.Sprintf("localhost:%d", port) - client := oopcc.New(clientAddr, nil) + client := container.New(port, nil) if err := client.Connect(); nil != err { - cmd.Process.Kill() + // cmd.Process.Kill() return err } cState := &containerState{ - pid: cmd.Process.Pid, + pid: 29694, port: port, client: client, } + if nil == cs.clients { + cs.clients = make(map[string]*containerState, 0) + } cs.clients[name] = cState return nil @@ -128,9 +126,12 @@ func (cs *ContainerService) killProcess(name string) error { return err } - if err = p.Kill(); nil != err { + if err = p.Signal(os.Interrupt); nil != err { return err } + // if err = p.Kill(); nil != err { + // return err + // } return nil } diff --git a/service/ContainerService_test.go b/service/ContainerService_test.go new file mode 100644 index 0000000..538ef47 --- /dev/null +++ b/service/ContainerService_test.go @@ -0,0 +1,69 @@ +package service + +import ( + "fmt" + "path" + "testing" + + lfcc "git.loafle.net/commons_go/config" + crc "git.loafle.net/commons_go/rpc/client" + ooccp "git.loafle.net/overflow/overflow_commons_go/config/probe" + "git.loafle.net/overflow/overflow_probes/config" + "github.com/stretchr/testify/assert" +) + +func initConfig() { + dir, err := lfcc.ABSPathify("../") + if nil != err { + panic("Config path is not valid") + } + + config.ConfigDir = &dir + + cfp := path.Join(*config.ConfigDir, ooccp.ConfigFileName) + config.ConfigFilePath = &cfp + + conf := lfcc.New() + config.Config = &ooccp.Config{} + if err := conf.Load(config.Config, *config.ConfigFilePath); nil != err { + panic(fmt.Sprintf("Config is not valid: %v", err)) + } + +} + +func TestGetClient(t *testing.T) { + initConfig() + + cs := GetService("ContainerService").(*ContainerService) + + var c crc.Client + var err error + + // // General + // c, err = cs.GetClient(ooccp.ContainerGeneralName) + + // assert.Nil(t, err) + // assert.NotNil(t, c) + + // err = cs.killProcess(ooccp.ContainerGeneralName) + // assert.Nil(t, err) + + // Network + c, err = cs.GetClient(ooccp.ContainerNetworkName) + + assert.Nil(t, err) + assert.NotNil(t, c) + + cs.killProcess(ooccp.ContainerNetworkName) + assert.Nil(t, err) + + // // Discovery + // c, err = cs.GetClient(ooccp.ContainerDiscoveryName) + + // assert.Nil(t, err) + // assert.NotNil(t, c) + + // cs.killProcess(ooccp.ContainerDiscoveryName) + // assert.Nil(t, err) + +} diff --git a/service/CrawlerService.go b/service/CrawlerService.go index 12e0c17..888717d 100644 --- a/service/CrawlerService.go +++ b/service/CrawlerService.go @@ -9,10 +9,11 @@ import ( ) func init() { - cdr.RegisterType(reflect.TypeOf((*CrawlerService)(nil)), &cda.ComponentAnnotation{}) + cdr.RegisterType(reflect.TypeOf((*CrawlerService)(nil))) } type CrawlerService struct { + cda.TypeAnnotation `annotation:"@overFlow:Service()"` } func (cs *CrawlerService) Install() error { diff --git a/service/DiscoveryService.go b/service/DiscoveryService.go index 5fedb87..7d8cde5 100644 --- a/service/DiscoveryService.go +++ b/service/DiscoveryService.go @@ -11,10 +11,12 @@ import ( ) func init() { - cdr.RegisterType(reflect.TypeOf((*DiscoveryService)(nil)), &cda.ComponentAnnotation{}) + cdr.RegisterType(reflect.TypeOf((*DiscoveryService)(nil))) } type DiscoveryService struct { + cda.TypeAnnotation `annotation:"@overFlow:Service()"` + ContainerService *ContainerService `annotation:"@Inject()"` CentralService *CentralService `annotation:"@Inject()"` } diff --git a/service/LogService.go b/service/LogService.go index be20a38..46de9d5 100644 --- a/service/LogService.go +++ b/service/LogService.go @@ -8,10 +8,11 @@ import ( ) func init() { - cdr.RegisterType(reflect.TypeOf((*LogService)(nil)), &cda.ComponentAnnotation{}) + cdr.RegisterType(reflect.TypeOf((*LogService)(nil))) } type LogService struct { + cda.TypeAnnotation `annotation:"@overFlow:Service()"` } func (ls *LogService) Send() error { diff --git a/service/ProbeService.go b/service/ProbeService.go index 29a6777..59e5edb 100644 --- a/service/ProbeService.go +++ b/service/ProbeService.go @@ -8,10 +8,11 @@ import ( ) func init() { - cdr.RegisterType(reflect.TypeOf((*ProbeService)(nil)), &cda.ComponentAnnotation{}) + cdr.RegisterType(reflect.TypeOf((*ProbeService)(nil))) } type ProbeService struct { + cda.TypeAnnotation `annotation:"@overFlow:Service()"` } func (ps *ProbeService) Start() error { diff --git a/service/SensorService.go b/service/SensorService.go index 23dfceb..ee79a6f 100644 --- a/service/SensorService.go +++ b/service/SensorService.go @@ -9,10 +9,11 @@ import ( ) func init() { - cdr.RegisterType(reflect.TypeOf((*SensorService)(nil)), &cda.ComponentAnnotation{}) + cdr.RegisterType(reflect.TypeOf((*SensorService)(nil))) } type SensorService struct { + cda.TypeAnnotation `annotation:"@overFlow:Service()"` } func (ss *SensorService) Start(id int64) error { diff --git a/service/service.go b/service/service.go index 479178a..4b2ee7e 100644 --- a/service/service.go +++ b/service/service.go @@ -1,13 +1,5 @@ package service -import ( - "fmt" - "reflect" - - cdr "git.loafle.net/commons_go/di/registry" - "git.loafle.net/commons_go/logging" -) - func InitService() { } @@ -15,34 +7,34 @@ func DestroyService() { } -func GetService(name string) interface{} { - var t reflect.Type - switch name { - case "CentralService": - t = reflect.TypeOf((*CentralService)(nil)) - case "ConfigService": - t = reflect.TypeOf((*ConfigService)(nil)) - case "ContainerService": - t = reflect.TypeOf((*ContainerService)(nil)) - case "CrawlerService": - t = reflect.TypeOf((*CrawlerService)(nil)) - case "DiscoveryService": - t = reflect.TypeOf((*DiscoveryService)(nil)) - case "LogService": - t = reflect.TypeOf((*LogService)(nil)) - case "ProbeService": - t = reflect.TypeOf((*ProbeService)(nil)) - case "SensorService": - t = reflect.TypeOf((*SensorService)(nil)) - default: - logging.Logger().Panic(fmt.Sprintf("Probe: Service[%s] is not exist", name)) - return nil - } +// func GetService(name string) interface{} { +// var t reflect.Type +// switch name { +// case "CentralService": +// t = reflect.TypeOf((*CentralService)(nil)) +// case "ConfigService": +// t = reflect.TypeOf((*ConfigService)(nil)) +// case "ContainerService": +// t = reflect.TypeOf((*ContainerService)(nil)) +// case "CrawlerService": +// t = reflect.TypeOf((*CrawlerService)(nil)) +// case "DiscoveryService": +// t = reflect.TypeOf((*DiscoveryService)(nil)) +// case "LogService": +// t = reflect.TypeOf((*LogService)(nil)) +// case "ProbeService": +// t = reflect.TypeOf((*ProbeService)(nil)) +// case "SensorService": +// t = reflect.TypeOf((*SensorService)(nil)) +// default: +// logging.Logger().Panic(fmt.Sprintf("Probe: Service[%s] is not exist", name)) +// return nil +// } - i, err := cdr.GetInstance(t) - if nil != err { - logging.Logger().Panic(fmt.Sprintf("Probe: Getting Service[%s] is failed %v", name, err)) - return nil - } - return i -} +// i, err := cdr.GetInstance(t) +// if nil != err { +// logging.Logger().Panic(fmt.Sprintf("Probe: Getting Service[%s] is failed %v", name, err)) +// return nil +// } +// return i +// }