This commit is contained in:
crusader 2017-12-15 17:18:32 +09:00
parent e9be910057
commit b4a05fc617
4 changed files with 100 additions and 38 deletions

View File

@ -9,9 +9,9 @@
"key": "95d8bcdc739741dca74c4a0e489e0774" "key": "95d8bcdc739741dca74c4a0e489e0774"
}, },
"paths": { "paths": {
"bin": "bin", "bin": "/bin",
"config": "config", "config": "/config",
"pid": "pid", "pid": "/pid",
"root": "" "root": "/project/overFlow/probe"
} }
} }

View File

@ -62,7 +62,7 @@ func main() {
var instance interface{} var instance interface{}
go func() { go func() {
if ooccp.ProbeStateTypeNotAuthorized != config.Config.Probe.State() { if ooccp.ProbeStateTypeNotAuthorized == config.Config.Probe.State() {
var err error var err error
instance = auth.New() instance = auth.New()

View File

@ -36,6 +36,7 @@ func (pm *probeManagers) Start() error {
} }
probeRPCRegistry := crr.NewRPCRegistry() probeRPCRegistry := crr.NewRPCRegistry()
centralProbeClient := oopccp.New(probeRPCRegistry) centralProbeClient := oopccp.New(probeRPCRegistry)
centralDataClient := oopccd.New() centralDataClient := oopccd.New()
centralClients := map[string]oogwc.Client{ centralClients := map[string]oogwc.Client{

View File

@ -1,11 +1,21 @@
package service package service
import ( import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"reflect" "reflect"
"strconv"
"time"
cda "git.loafle.net/commons_go/di/annotation" cda "git.loafle.net/commons_go/di/annotation"
cdr "git.loafle.net/commons_go/di/registry" cdr "git.loafle.net/commons_go/di/registry"
"git.loafle.net/commons_go/logging"
crc "git.loafle.net/commons_go/rpc/client" crc "git.loafle.net/commons_go/rpc/client"
oopcc "git.loafle.net/overflow/overflow_probe_container/client"
"git.loafle.net/overflow/overflow_probes/config"
) )
func init() { func init() {
@ -17,9 +27,9 @@ type ContainerService struct {
} }
type containerState struct { type containerState struct {
socketName string pid int
pid int port int
client crc.Client client crc.Client
} }
func (cs *ContainerService) Call(name string, result interface{}, method string, params ...interface{}) error { func (cs *ContainerService) Call(name string, result interface{}, method string, params ...interface{}) error {
@ -39,7 +49,15 @@ func (cs *ContainerService) Send(name string, method string, params ...interface
} }
func (cs *ContainerService) GetClient(name string) (crc.Client, error) { func (cs *ContainerService) GetClient(name string) (crc.Client, error) {
return nil, nil cState, ok := cs.clients[name]
if !ok {
if err := cs.runProcess(name); nil != err {
return nil, err
}
cState, _ = cs.clients[name]
}
return cState.client, nil
} }
func (cs *ContainerService) Connect(name string) error { func (cs *ContainerService) Connect(name string) error {
@ -53,47 +71,90 @@ func (cs *ContainerService) runProcess(name string) error {
return nil return nil
} }
// runCmd := config.Config.Paths["root"] + "/" + config.Config.Paths["bin"] + "/" + name + "/start" 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")
// sockFile := uuid.NewV4().String() cmd := exec.Command(cmdPath, pidPath)
// sockArg := fmt.Sprintf("-sock=\"%s\"", sockFile) 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(runCmd, sockArg) port, err := watchPidFileCreate(pidPath, time.Duration(time.Second*2))
// if err := cmd.Start(); nil != err { if nil != err {
// logging.Logger().Error(fmt.Sprintf("Probe: To run container(%s) failed err %v", name, err)) return err
// return err }
// }
// time.Sleep(time.Duration(time.Second * 2))
// cs := &containerState{ clientAddr := fmt.Sprintf("localhost:%d", port)
// socketName: sockFile, client := oopcc.New(clientAddr, nil)
// pid: cmd.Process.Pid, if err := client.Connect(); nil != err {
// } cmd.Process.Kill()
// cs.client = oopmcc.New(sockFile, nil) return err
// // write pid file }
// cs.containerClients[name] = cs
cState := &containerState{
pid: cmd.Process.Pid,
port: port,
client: client,
}
cs.clients[name] = cState
return nil return nil
} }
func (cs *ContainerService) checkProcess(name string) bool { func (cs *ContainerService) checkProcess(name string) bool {
return false cState, ok := cs.clients[name]
if !ok || nil == cState || nil == cState.client {
return false
}
_, err := os.FindProcess(cState.pid)
if nil != err {
return false
}
return true
} }
func (cs *ContainerService) killProcess(name string) error { func (cs *ContainerService) killProcess(name string) error {
// cs, ok := cs.containerClients[name] cState, ok := cs.clients[name]
// if !ok || nil == cs || nil == cs.client { if !ok || nil == cState || nil == cState.client {
// return fmt.Errorf("Probe: Container[%s] is not exist", name) return fmt.Errorf("Probe: Container[%s] is not exist", name)
// } }
// p, err := os.FindProcess(cs.pid) p, err := os.FindProcess(cState.pid)
// if nil != err { if nil != err {
// return err return err
// } }
// if err = p.Kill(); nil != err {
// return err if err = p.Kill(); nil != err {
// } return err
// // remove pid file }
return nil return nil
} }
func watchPidFileCreate(pidFilePath string, waitTime time.Duration) (int, error) {
startTime := time.Now()
for {
if _, err := os.Stat(pidFilePath); err == nil {
buf, err := ioutil.ReadFile(pidFilePath)
if nil != err {
return 0, err
}
portNumber, err := strconv.ParseInt(string(buf), 10, 32)
if nil != err {
return 0, err
}
return int(portNumber), nil
}
if time.Since(startTime) > waitTime {
return 0, fmt.Errorf("Probe: pid file not exist")
}
time.Sleep(time.Duration(time.Millisecond * 100))
}
}