This commit is contained in:
crusader 2018-03-23 01:10:38 +09:00
parent 94a4f3136c
commit 45f79e6b42
4 changed files with 29 additions and 10 deletions

View File

@ -2,7 +2,6 @@ package container
import ( import (
"fmt" "fmt"
"log"
"os/exec" "os/exec"
"path" "path"
@ -36,17 +35,19 @@ func GetContainerCommand(name string) (cmd *exec.Cmd, pidPath string) {
case ooccp.ContainerNetworkName: case ooccp.ContainerNetworkName:
logConfigPath := path.Join(config.Config.Paths["root"], ooccp.PathBin, ooccp.ContainerNetworkLogConfigFileName) logConfigPath := path.Join(config.Config.Paths["root"], ooccp.PathBin, ooccp.ContainerNetworkLogConfigFileName)
exePath := path.Join(config.Config.Paths["root"], ooccp.PathBin, ooccp.ContainerNetworkFileName) exePath := path.Join(config.Config.Paths["root"], ooccp.PathBin, ooccp.ContainerNetworkFileName)
arg := fmt.Sprintf("-%s=%s -%s=%s", oocc.FlagPidFilePathName, pidPath, oocc.FlagLogConfigFilePathName, logConfigPath)
cmd = exec.Command(exePath, arg) arg1 := fmt.Sprintf("-%s=%s", oocc.FlagPidFilePathName, pidPath)
arg2 := fmt.Sprintf("-%s=%s", oocc.FlagLogConfigFilePathName, logConfigPath)
cmd = exec.Command(exePath, arg1, arg2)
case ooccp.ContainerDiscoveryName: case ooccp.ContainerDiscoveryName:
logConfigPath := path.Join(config.Config.Paths["root"], ooccp.PathBin, ooccp.ContainerDiscoveryLogConfigFileName) logConfigPath := path.Join(config.Config.Paths["root"], ooccp.PathBin, ooccp.ContainerDiscoveryLogConfigFileName)
exePath := path.Join(config.Config.Paths["root"], ooccp.PathBin, ooccp.ContainerDiscoveryFileName) exePath := path.Join(config.Config.Paths["root"], ooccp.PathBin, ooccp.ContainerDiscoveryFileName)
arg := fmt.Sprintf("-%s=%s -%s=%s", oocc.FlagPidFilePathName, pidPath, oocc.FlagLogConfigFilePathName, logConfigPath)
log.Printf("arg:%s", arg) arg1 := fmt.Sprintf("-%s=%s", oocc.FlagPidFilePathName, pidPath)
arg2 := fmt.Sprintf("-%s=%s", oocc.FlagLogConfigFilePathName, logConfigPath)
cmd = exec.Command(exePath, arg) cmd = exec.Command(exePath, arg1, arg2)
default: default:
} }
return return

View File

@ -46,13 +46,15 @@ func (pm *probeManagers) Start() error {
oocmp.HTTPEntry_Data: centralDataClient, oocmp.HTTPEntry_Data: centralDataClient,
} }
cdr.RegisterResource("CentralClients", centralClients) cdr.RegisterResource("CentralClients", centralClients)
cdr.RegisterResource("ProbeRPCInvoker", probeRPCRegistry)
var ( var (
services []interface{} services []interface{}
err error err error
) )
if services, err = cdr.GetInstancesByAnnotationName(oopca.ServiceTag); nil != err { services, err = cdr.GetInstancesByAnnotationName(oopca.ServiceTag)
if nil != err {
logging.Logger().Panicf("Probe: Cannot create instances of service %v", err) logging.Logger().Panicf("Probe: Cannot create instances of service %v", err)
} }

View File

@ -3,7 +3,6 @@ package service
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"os" "os"
"reflect" "reflect"
"strconv" "strconv"
@ -13,6 +12,7 @@ import (
cdr "git.loafle.net/commons_go/di/registry" cdr "git.loafle.net/commons_go/di/registry"
"git.loafle.net/commons_go/logging" "git.loafle.net/commons_go/logging"
crc "git.loafle.net/commons_go/rpc/client" crc "git.loafle.net/commons_go/rpc/client"
crr "git.loafle.net/commons_go/rpc/registry"
"git.loafle.net/overflow/overflow_probes/client/container" "git.loafle.net/overflow/overflow_probes/client/container"
) )
@ -23,6 +23,8 @@ func init() {
type ContainerService struct { type ContainerService struct {
cda.TypeAnnotation `annotation:"@overFlow:Service()"` cda.TypeAnnotation `annotation:"@overFlow:Service()"`
ProbeRPCInvoker crr.RPCInvoker `annotation:"@Resource()"`
clients map[string]*containerState clients map[string]*containerState
} }
@ -72,6 +74,8 @@ func (cs *ContainerService) runProcess(name string) error {
} }
cmd, pidPath := container.GetContainerCommand(name) cmd, pidPath := container.GetContainerCommand(name)
removePidFile(pidPath)
if err := cmd.Start(); nil != err { if err := cmd.Start(); nil != err {
logging.Logger().Errorf("Probe: To run container(%s) failed err %v", name, err) logging.Logger().Errorf("Probe: To run container(%s) failed err %v", name, err)
return err return err
@ -83,13 +87,13 @@ func (cs *ContainerService) runProcess(name string) error {
} }
// port := 60000 // port := 60000
client := container.New(port, nil) client := container.New(port, cs.ProbeRPCInvoker)
if err := client.Connect(); nil != err { if err := client.Connect(); nil != err {
cmd.Process.Kill() cmd.Process.Kill()
return err return err
} }
log.Printf("pid: %d", cmd.Process.Pid) logging.Logger().Debugf("Container[%s] pid: %d", name, cmd.Process.Pid)
cState := &containerState{ cState := &containerState{
pid: cmd.Process.Pid, pid: cmd.Process.Pid,
@ -140,6 +144,14 @@ func (cs *ContainerService) killProcess(name string) error {
return nil return nil
} }
func removePidFile(pidFilePath string) {
if _, err := os.Stat(pidFilePath); err == nil {
if err := os.Remove(pidFilePath); nil != err {
logging.Logger().Errorf("Probe: Removing pid file has been failed [%v]", err)
}
}
}
func watchPidFileCreate(pidFilePath string, waitTime time.Duration) (int, error) { func watchPidFileCreate(pidFilePath string, waitTime time.Duration) (int, error) {
startTime := time.Now() startTime := time.Now()

View File

@ -5,6 +5,7 @@ import (
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"
ooccp "git.loafle.net/overflow/overflow_commons_go/config/probe" ooccp "git.loafle.net/overflow/overflow_commons_go/config/probe"
discoveryM "git.loafle.net/overflow/overflow_commons_go/modules/discovery/model" discoveryM "git.loafle.net/overflow/overflow_commons_go/modules/discovery/model"
oocmp "git.loafle.net/overflow/overflow_commons_go/modules/probe" oocmp "git.loafle.net/overflow/overflow_commons_go/modules/probe"
@ -22,6 +23,8 @@ type DiscoveryService struct {
} }
func (ds *DiscoveryService) DiscoverZone(requesterID string, dz *discoveryM.DiscoveryZone) error { func (ds *DiscoveryService) DiscoverZone(requesterID string, dz *discoveryM.DiscoveryZone) error {
logging.Logger().Debugf("DiscoverZone: requesterID(%s)", requesterID)
return ds.ContainerService.Send(ooccp.ContainerDiscoveryName, "DiscoveryService.DiscoverZone", requesterID, dz) return ds.ContainerService.Send(ooccp.ContainerDiscoveryName, "DiscoveryService.DiscoverZone", requesterID, dz)
} }
@ -39,6 +42,7 @@ func (ds *DiscoveryService) DiscoverService(requesterID string, port *discoveryM
// use by discovery // use by discovery
func (ds *DiscoveryService) DiscoveredZone(requesterID string, zone *discoveryM.Zone) error { func (ds *DiscoveryService) DiscoveredZone(requesterID string, zone *discoveryM.Zone) error {
logging.Logger().Debugf("DiscoveredZone: requesterID(%s) network(%s) ip(%s) mac(%s) iface(%s)", requesterID, zone.Network, zone.IP, zone.Mac, zone.Iface)
return ds.CentralService.Send(oocmp.HTTPEntry_Probe, "DiscoveryService.discoveredZone", requesterID, zone) return ds.CentralService.Send(oocmp.HTTPEntry_Probe, "DiscoveryService.discoveredZone", requesterID, zone)
} }