From 94a4f3136c92c7f107d2dd88b73ba110279db876 Mon Sep 17 00:00:00 2001 From: crusader Date: Wed, 21 Mar 2018 19:22:13 +0900 Subject: [PATCH] ing --- auth/auth.go | 6 ++-- auth/client/client_handlers.go | 7 ++--- auth/client/socket_builders.go | 2 +- auth/client/socket_handlers.go | 5 ++-- auth/service/NoAuthProbeService.go | 6 ++-- client/central/data/client_handlers.go | 6 ++-- client/central/data/socket_handlers.go | 5 ++-- client/container/client.go | 13 ++++++-- commons/central/client/socket_builders.go | 4 +-- config.json | 2 +- main.go | 19 ++++++------ noauthprobe.json | 2 +- probe/probe.go | 16 ++++++---- service/ContainerService.go | 36 +++++++++++++---------- service/DiscoveryService.go | 10 +++---- service/service.go | 4 +-- 16 files changed, 76 insertions(+), 67 deletions(-) diff --git a/auth/auth.go b/auth/auth.go index 131c363..ab9f0c1 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -42,7 +42,7 @@ type authManagers struct { func (am *authManagers) EndableStart(doneChan chan<- error) error { if nil != am.stopChan { - logging.Logger().Panic("Auth: auth is already running. Stop it before starting it again") + logging.Logger().Panicf("Auth: auth is already running. Stop it before starting it again") } am.configPath = path.Join(*config.ConfigDir, ooccn.ConfigFileName) @@ -91,7 +91,7 @@ func (am *authManagers) Stop() { func (am *authManagers) destroy(err error) { if am.stopChan == nil { - logging.Logger().Warn("Auth: auth must be started before stopping it") + logging.Logger().Warnf("Auth: auth must be started before stopping it") } close(am.stopChan) am.stopWg.Wait() @@ -100,7 +100,7 @@ func (am *authManagers) destroy(err error) { am.cClient.Close() close(am.serviceDoneChan) - logging.Logger().Info(fmt.Sprintf("Auth: stopped")) + logging.Logger().Infof("Auth: stopped") am.doneChan <- err } diff --git a/auth/client/client_handlers.go b/auth/client/client_handlers.go index c8aa856..5d86132 100644 --- a/auth/client/client_handlers.go +++ b/auth/client/client_handlers.go @@ -1,8 +1,6 @@ package client import ( - "fmt" - "git.loafle.net/commons_go/logging" crc "git.loafle.net/commons_go/rpc/client" crr "git.loafle.net/commons_go/rpc/registry" @@ -21,12 +19,11 @@ type ClientHandlers struct { } func (ch *ClientHandlers) Init(clientCTX crc.ClientContext) error { - logging.Logger().Info(fmt.Sprintf("Auth: client has been initialized")) + logging.Logger().Infof("Auth: client has been initialized") return nil } func (ch *ClientHandlers) Destroy(clientCTX crc.ClientContext) { - logging.Logger().Info(fmt.Sprintf("Auth: client has been destroyed")) + logging.Logger().Infof("Auth: client has been destroyed") } - diff --git a/auth/client/socket_builders.go b/auth/client/socket_builders.go index 3472f4b..1577786 100644 --- a/auth/client/socket_builders.go +++ b/auth/client/socket_builders.go @@ -43,6 +43,6 @@ func (sb *SocketBuilders) Validate() { sb.SocketBuilders.Validate() if nil == sb.napService { - logging.Logger().Panic("Auth: NoAuthProbeService must be specified") + logging.Logger().Panicf("Auth: NoAuthProbeService must be specified") } } diff --git a/auth/client/socket_handlers.go b/auth/client/socket_handlers.go index 01020f0..2725200 100644 --- a/auth/client/socket_handlers.go +++ b/auth/client/socket_handlers.go @@ -1,7 +1,6 @@ package client import ( - "fmt" "net/http" "git.loafle.net/commons_go/logging" @@ -18,7 +17,7 @@ type SocketHandlers struct { } func (sh *SocketHandlers) OnConnect(socketContext cwfc.SocketContext, res *http.Response) { - logging.Logger().Info(fmt.Sprintf("Auth: client has been connected res[%v]", res)) + logging.Logger().Infof("Auth: client has been connected res[%v]", res) switch sh.napService.Config.State() { case ooccn.NoAuthProbeStateTypeNotRegisterd: @@ -30,7 +29,7 @@ func (sh *SocketHandlers) OnConnect(socketContext cwfc.SocketContext, res *http. } func (sh *SocketHandlers) OnDisconnect(soc cwfc.Socket) { - logging.Logger().Info(fmt.Sprintf("Auth: client has been disconnected soc[%v]", soc)) + logging.Logger().Infof("Auth: client has been disconnected soc[%v]", soc) } diff --git a/auth/service/NoAuthProbeService.go b/auth/service/NoAuthProbeService.go index f54feb7..49587ff 100644 --- a/auth/service/NoAuthProbeService.go +++ b/auth/service/NoAuthProbeService.go @@ -28,7 +28,7 @@ func (s *NoAuthProbeService) Accept(probeKey string) error { return nil } - logging.Logger().Info(fmt.Sprintf("Auth: Probe[%s] has been accepted from central", probeKey)) + logging.Logger().Infof("Auth: Probe[%s] has been accepted from central", probeKey) s.DoneChan <- nil return nil @@ -42,7 +42,7 @@ func (s *NoAuthProbeService) Deny() error { return nil } - logging.Logger().Info(fmt.Sprintf("Auth: Probe has been denyed from central")) + logging.Logger().Infof("Auth: Probe has been denyed from central") s.DoneChan <- nil return nil } @@ -65,7 +65,7 @@ func (s *NoAuthProbeService) GetRequestHeader() map[string][]string { func (s *NoAuthProbeService) GetRegisterRequestHeader() string { i, err := oopai.GetRegistHeader() if nil != err { - logging.Logger().Error(fmt.Sprintf("Auth: Gathering system information has been failed %v", err)) + logging.Logger().Errorf("Auth: Gathering system information has been failed %v", err) } return i } diff --git a/client/central/data/client_handlers.go b/client/central/data/client_handlers.go index a56a2da..d5a030f 100644 --- a/client/central/data/client_handlers.go +++ b/client/central/data/client_handlers.go @@ -1,8 +1,6 @@ package data import ( - "fmt" - "git.loafle.net/commons_go/logging" crc "git.loafle.net/commons_go/rpc/client" oogwc "git.loafle.net/overflow/overflow_gateway_websocket/client" @@ -20,11 +18,11 @@ type ClientHandlers struct { } func (ch *ClientHandlers) Init(clientCTX crc.ClientContext) error { - logging.Logger().Info(fmt.Sprintf("Probe Data: client has been initialized")) + logging.Logger().Infof("Probe Data: client has been initialized") return nil } func (ch *ClientHandlers) Destroy(clientCTX crc.ClientContext) { - logging.Logger().Info(fmt.Sprintf("Probe Data: client has been destroyed")) + logging.Logger().Infof("Probe Data: client has been destroyed") } diff --git a/client/central/data/socket_handlers.go b/client/central/data/socket_handlers.go index 25a1d9d..92a95e5 100644 --- a/client/central/data/socket_handlers.go +++ b/client/central/data/socket_handlers.go @@ -1,7 +1,6 @@ package data import ( - "fmt" "net/http" "git.loafle.net/commons_go/logging" @@ -13,12 +12,12 @@ type SocketHandlers struct { } func (sh *SocketHandlers) OnConnect(socketContext cwfc.SocketContext, res *http.Response) { - logging.Logger().Info(fmt.Sprintf("Probe: client has been connected res[%v]", res)) + logging.Logger().Infof("Probe: client has been connected res[%v]", res) } func (sh *SocketHandlers) OnDisconnect(soc cwfc.Socket) { - logging.Logger().Info(fmt.Sprintf("Probe: client has been disconnected soc[%v]", soc)) + logging.Logger().Infof("Probe: client has been disconnected soc[%v]", soc) } diff --git a/client/container/client.go b/client/container/client.go index 89e9bd3..3faf879 100644 --- a/client/container/client.go +++ b/client/container/client.go @@ -2,11 +2,13 @@ package container import ( "fmt" + "log" "os/exec" "path" crc "git.loafle.net/commons_go/rpc/client" crr "git.loafle.net/commons_go/rpc/registry" + oocc "git.loafle.net/overflow/overflow_commons_go/config" 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" @@ -25,19 +27,24 @@ func GetContainerCommand(name string) (cmd *exec.Cmd, pidPath string) { switch name { case ooccp.ContainerGeneralName: + logConfigPath := path.Join(config.Config.Paths["root"], ooccp.PathBin, ooccp.ContainerGeneralLogConfigFileName) 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) + cmd = exec.Command(javaPath, "-jar", jarPath, pidPath, logConfigPath) case ooccp.ContainerNetworkName: + logConfigPath := path.Join(config.Config.Paths["root"], ooccp.PathBin, ooccp.ContainerNetworkLogConfigFileName) exePath := path.Join(config.Config.Paths["root"], ooccp.PathBin, ooccp.ContainerNetworkFileName) - arg := fmt.Sprintf("-pid-path=%s", pidPath) + arg := fmt.Sprintf("-%s=%s -%s=%s", oocc.FlagPidFilePathName, pidPath, oocc.FlagLogConfigFilePathName, logConfigPath) cmd = exec.Command(exePath, arg) case ooccp.ContainerDiscoveryName: + logConfigPath := path.Join(config.Config.Paths["root"], ooccp.PathBin, ooccp.ContainerDiscoveryLogConfigFileName) exePath := path.Join(config.Config.Paths["root"], ooccp.PathBin, ooccp.ContainerDiscoveryFileName) - arg := fmt.Sprintf("-pid-path=%s", pidPath) + arg := fmt.Sprintf("-%s=%s -%s=%s", oocc.FlagPidFilePathName, pidPath, oocc.FlagLogConfigFilePathName, logConfigPath) + + log.Printf("arg:%s", arg) cmd = exec.Command(exePath, arg) default: diff --git a/commons/central/client/socket_builders.go b/commons/central/client/socket_builders.go index 029f955..cebbb7b 100644 --- a/commons/central/client/socket_builders.go +++ b/commons/central/client/socket_builders.go @@ -1,8 +1,6 @@ package client import ( - "fmt" - "git.loafle.net/commons_go/logging" cunu "git.loafle.net/commons_go/util/net/url" cwfc "git.loafle.net/commons_go/websocket_fasthttp/client" @@ -13,7 +11,7 @@ func NewSocketBuilder(entryPath string) *SocketBuilders { sb := &SocketBuilders{} url, err := cunu.Join(config.Config.Central.URL, entryPath) if nil != err { - logging.Logger().Error(fmt.Sprintf("Probe: Cannot create SocketBuilder %v", err)) + logging.Logger().Errorf("Probe: Cannot create SocketBuilder %v", err) return nil } sb.URL = url diff --git a/config.json b/config.json index 3148205..dbd3cf0 100644 --- a/config.json +++ b/config.json @@ -6,7 +6,7 @@ "writeBufferSize": 8192 }, "probe": { - "key": "95d8bcdc739741dca74c4a0e489e0774" + "key": "52abd6fd57e511e7ac52080027658d13" }, "paths": { "root": "/project/overFlow/probe" diff --git a/main.go b/main.go index 3f61ab0..0ad5a09 100644 --- a/main.go +++ b/main.go @@ -49,8 +49,10 @@ func init() { // flag.PrintDefaults() // } - configDir = oocc.FlagConfigDir() + configDir = oocc.FlagConfigDir("./") flag.Parse() + + logging.InitializeLogger("") } func main() { @@ -70,18 +72,18 @@ func main() { defer close(authDoneChan) if err := instance.(commons.EndableStarter).EndableStart(authDoneChan); err != nil { - logging.Logger().Error(err.Error()) + logging.Logger().Error(err) os.Exit(1) } err = <-authDoneChan if nil != err { - logging.Logger().Error(err.Error()) + logging.Logger().Error(err) os.Exit(1) } } instance = probe.New() if err := instance.(commons.Starter).Start(); err != nil { - logging.Logger().Error(err.Error()) + logging.Logger().Error(err) os.Exit(1) } @@ -103,16 +105,16 @@ func main() { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() if err := instance.(commons.Shutdowner).Shutdown(ctx); err != nil { - logging.Logger().Error(fmt.Sprintf("Probe error: %v", err)) + logging.Logger().Errorf("Probe error: %v", err) } } func loadConfig() { if dir, err := lfcc.ABSPathify(*configDir); nil != err { - logging.Logger().Panic(fmt.Sprintf("Config path[%s] is not valid", *configDir)) + logging.Logger().Panicf("Config path[%s] is not valid", *configDir) } else { - logging.Logger().Debug(fmt.Sprintf("Config path: %s", dir)) + logging.Logger().Debugf("Config path: %s", dir) config.ConfigDir = &dir } cfp := path.Join(*config.ConfigDir, ooccp.ConfigFileName) @@ -121,9 +123,8 @@ func loadConfig() { conf := lfcc.New() config.Config = &ooccp.Config{} if err := conf.Load(config.Config, *config.ConfigFilePath); nil != err { - logging.Logger().Panic(fmt.Sprintf("Config is not valid: %v", err)) + logging.Logger().Panicf("Config is not valid: %v", err) } - } const ( diff --git a/noauthprobe.json b/noauthprobe.json index e049b0f..5002bdf 100644 --- a/noauthprobe.json +++ b/noauthprobe.json @@ -1,3 +1,3 @@ { - "tempKey": "969946e1-4de3-486a-b11c-00d311244c3a" + "tempKey": "84cdd159-2839-439c-97e1-72dc9efadc35" } \ No newline at end of file diff --git a/probe/probe.go b/probe/probe.go index 2699df1..8ef8007 100644 --- a/probe/probe.go +++ b/probe/probe.go @@ -1,7 +1,6 @@ package probe import ( - "fmt" "sync" cdr "git.loafle.net/commons_go/di/registry" @@ -33,7 +32,7 @@ type probeManagers struct { func (pm *probeManagers) Start() error { if nil != pm.stopChan { - logging.Logger().Panic("Probe: already running. Stop it before starting it again") + logging.Logger().Panicf("Probe: already running. Stop it before starting it again") } oops.InitService() @@ -48,7 +47,14 @@ func (pm *probeManagers) Start() error { } cdr.RegisterResource("CentralClients", centralClients) - services := cdr.GetInstancesByAnnotationName(oopca.ServiceTag) + var ( + services []interface{} + err error + ) + + if services, err = cdr.GetInstancesByAnnotationName(oopca.ServiceTag); nil != err { + logging.Logger().Panicf("Probe: Cannot create instances of service %v", err) + } for _, s := range services { probeRPCRegistry.RegisterService(s, "") @@ -72,7 +78,7 @@ func (pm *probeManagers) Start() error { func (pm *probeManagers) Stop() { if pm.stopChan == nil { - logging.Logger().Warn("Probe: probe must be started before stopping it") + logging.Logger().Warnf("Probe: probe must be started before stopping it") } close(pm.stopChan) pm.stopWg.Wait() @@ -80,7 +86,7 @@ func (pm *probeManagers) Stop() { // pm.cClient.Close() - logging.Logger().Info(fmt.Sprintf("Probe: stopped")) + logging.Logger().Infof("Probe: stopped") } diff --git a/service/ContainerService.go b/service/ContainerService.go index f80a4e1..6111e4e 100644 --- a/service/ContainerService.go +++ b/service/ContainerService.go @@ -3,6 +3,7 @@ package service import ( "fmt" "io/ioutil" + "log" "os" "reflect" "strconv" @@ -10,6 +11,7 @@ import ( 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" "git.loafle.net/overflow/overflow_probes/client/container" ) @@ -69,26 +71,28 @@ func (cs *ContainerService) runProcess(name string) error { return nil } - // 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 - // } - - // port, err := watchPidFileCreate(pidPath, time.Duration(time.Second*2)) - // if nil != err { - // return err - // } - port := 60000 - - client := container.New(port, nil) - if err := client.Connect(); nil != err { - // cmd.Process.Kill() + cmd, pidPath := container.GetContainerCommand(name) + if err := cmd.Start(); nil != err { + logging.Logger().Errorf("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 + + client := container.New(port, nil) + if err := client.Connect(); nil != err { + cmd.Process.Kill() + return err + } + + log.Printf("pid: %d", cmd.Process.Pid) + cState := &containerState{ - pid: 29694, + pid: cmd.Process.Pid, port: port, client: client, } diff --git a/service/DiscoveryService.go b/service/DiscoveryService.go index df4a12c..8a9c329 100644 --- a/service/DiscoveryService.go +++ b/service/DiscoveryService.go @@ -5,9 +5,9 @@ import ( cda "git.loafle.net/commons_go/di/annotation" cdr "git.loafle.net/commons_go/di/registry" + ooccp "git.loafle.net/overflow/overflow_commons_go/config/probe" discoveryM "git.loafle.net/overflow/overflow_commons_go/modules/discovery/model" oocmp "git.loafle.net/overflow/overflow_commons_go/modules/probe" - oocmpsp "git.loafle.net/overflow/overflow_commons_go/modules/probe/service/probe" ) func init() { @@ -22,19 +22,19 @@ type DiscoveryService struct { } func (ds *DiscoveryService) DiscoverZone(requesterID string, dz *discoveryM.DiscoveryZone) error { - return ds.ContainerService.Send(oocmpsp.ProbeContainerNameDiscovery, "DiscoveryService.DiscoverZone", requesterID, dz) + return ds.ContainerService.Send(ooccp.ContainerDiscoveryName, "DiscoveryService.DiscoverZone", requesterID, dz) } func (ds *DiscoveryService) DiscoverHost(requesterID string, zone *discoveryM.Zone, dh *discoveryM.DiscoveryHost) error { - return ds.ContainerService.Send(oocmpsp.ProbeContainerNameDiscovery, "DiscoveryService.DiscoverHost", requesterID, zone, dh) + return ds.ContainerService.Send(ooccp.ContainerDiscoveryName, "DiscoveryService.DiscoverHost", requesterID, zone, dh) } func (ds *DiscoveryService) DiscoverPort(requesterID string, host *discoveryM.Host, dp *discoveryM.DiscoveryPort) error { - return ds.ContainerService.Send(oocmpsp.ProbeContainerNameDiscovery, "DiscoveryService.DiscoverPort", requesterID, host, dp) + return ds.ContainerService.Send(ooccp.ContainerDiscoveryName, "DiscoveryService.DiscoverPort", requesterID, host, dp) } func (ds *DiscoveryService) DiscoverService(requesterID string, port *discoveryM.Port, dService *discoveryM.DiscoveryService) error { - return ds.ContainerService.Send(oocmpsp.ProbeContainerNameDiscovery, "DiscoveryService.DiscoverZone", requesterID, port, dService) + return ds.ContainerService.Send(ooccp.ContainerDiscoveryName, "DiscoveryService.DiscoverZone", requesterID, port, dService) } // use by discovery diff --git a/service/service.go b/service/service.go index 4b2ee7e..113dbea 100644 --- a/service/service.go +++ b/service/service.go @@ -27,13 +27,13 @@ func DestroyService() { // case "SensorService": // t = reflect.TypeOf((*SensorService)(nil)) // default: -// logging.Logger().Panic(fmt.Sprintf("Probe: Service[%s] is not exist", name)) +// logging.Logger().Panicf("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)) +// logging.Logger().Panicf("Probe: Getting Service[%s] is failed %v", name, err) // return nil // } // return i