probe/service/ContainerService.go

354 lines
8.9 KiB
Go
Raw Normal View History

2018-04-13 11:59:46 +00:00
package service
import (
2018-04-18 14:56:13 +00:00
"context"
"fmt"
"io/ioutil"
"os"
"os/exec"
2018-04-13 11:59:46 +00:00
"reflect"
2018-04-18 14:56:13 +00:00
"strconv"
"time"
2018-04-13 11:59:46 +00:00
cda "git.loafle.net/commons/di-go/annotation"
cdr "git.loafle.net/commons/di-go/registry"
2018-04-18 14:56:13 +00:00
"git.loafle.net/commons/logging-go"
crc "git.loafle.net/commons/rpc-go/client"
csc "git.loafle.net/commons/server-go/client"
2018-04-26 08:39:32 +00:00
occp "git.loafle.net/overflow/commons-go/config/probe"
2018-04-26 08:43:40 +00:00
ocsp "git.loafle.net/overflow/commons-go/service/probe"
2018-04-18 14:56:13 +00:00
"git.loafle.net/overflow/probe/client/container"
"git.loafle.net/overflow/probe/config"
// For annotation
2018-04-13 11:59:46 +00:00
_ "git.loafle.net/overflow/commons-go/core/annotation"
)
var ContainerServiceType = reflect.TypeOf((*ContainerService)(nil))
func init() {
cdr.RegisterType(ContainerServiceType)
}
type ContainerService struct {
2018-04-26 08:43:40 +00:00
ocsp.ContainerService
2018-04-13 11:59:46 +00:00
cda.TypeAnnotation `annotation:"@overflow:RPCService()"`
2018-04-18 14:56:13 +00:00
2018-04-19 15:03:58 +00:00
DiscoveryService *DiscoveryService `annotation:"@Inject()"`
2018-04-18 14:56:13 +00:00
2018-04-26 08:39:32 +00:00
rpcServiceMap map[occp.ContainerType][]interface{}
containerStates map[occp.ContainerType]*containerState
2018-04-18 14:56:13 +00:00
connectorMap map[csc.Connector]*containerState
2018-04-13 11:59:46 +00:00
}
2018-04-17 14:11:13 +00:00
func (s *ContainerService) InitService() error {
2018-04-26 08:39:32 +00:00
s.containerStates = make(map[occp.ContainerType]*containerState)
s.rpcServiceMap = make(map[occp.ContainerType][]interface{})
2018-04-18 14:56:13 +00:00
s.connectorMap = make(map[csc.Connector]*containerState)
2018-04-17 14:11:13 +00:00
return nil
}
func (s *ContainerService) StartService() error {
2018-04-26 08:39:32 +00:00
s.rpcServiceMap[occp.ContainerDiscovery] = []interface{}{
2018-04-19 15:03:58 +00:00
s.DiscoveryService,
2018-04-18 14:56:13 +00:00
}
2018-04-26 08:39:32 +00:00
s.rpcServiceMap[occp.ContainerNetwork] = []interface{}{}
s.rpcServiceMap[occp.ContainerGenernal] = []interface{}{}
2018-04-17 14:11:13 +00:00
return nil
}
func (s *ContainerService) StopService() {
2018-04-18 14:56:13 +00:00
for containerType := range s.containerStates {
s.removeContainerState(containerType)
}
2018-04-17 14:11:13 +00:00
}
func (s *ContainerService) DestroyService() {
}
2018-04-26 08:39:32 +00:00
func (s *ContainerService) Call(containerType occp.ContainerType, result interface{}, method string, params ...interface{}) error {
2018-04-18 14:56:13 +00:00
client, err := s.getClient(containerType)
if nil != err {
return err
}
return client.Call(result, method, params...)
}
2018-04-26 08:39:32 +00:00
func (s *ContainerService) Send(containerType occp.ContainerType, method string, params ...interface{}) error {
2018-04-18 14:56:13 +00:00
client, err := s.getClient(containerType)
if nil != err {
return err
}
return client.Send(method, params...)
}
2018-04-26 08:39:32 +00:00
func (s *ContainerService) getClient(containerType occp.ContainerType) (*crc.Client, error) {
2018-04-18 14:56:13 +00:00
cs := s.checkContainer(containerType)
if nil == cs {
2018-04-20 11:19:14 +00:00
_cs, err := s.runContainer(containerType)
//_cs, err := s.debugContainer(containerType)
2018-04-18 14:56:13 +00:00
if nil != err {
return nil, err
}
cs = _cs
s.containerStates[containerType] = cs
}
if nil == cs.client {
client, err := container.NewClient(containerType, cs.port, s.rpcServiceMap[containerType])
if nil != err {
s.removeContainerState(containerType)
return nil, err
}
2018-04-19 15:03:58 +00:00
if err := client.Start(); nil != err {
s.removeContainerState(containerType)
return nil, err
}
2018-04-18 14:56:13 +00:00
cs.client = client
cs.client.Connector.SetOnDisconnected(s.onDisconnected)
s.connectorMap[cs.client.Connector] = cs
}
return cs.client, nil
}
func (s *ContainerService) onDisconnected(connector csc.Connector) {
cs, ok := s.connectorMap[connector]
if !ok || nil == cs {
return
}
2018-04-20 11:19:14 +00:00
s.refreshContainer(cs.containerType)
2018-04-18 14:56:13 +00:00
}
2018-04-26 08:39:32 +00:00
func (s *ContainerService) runContainer(containerType occp.ContainerType) (*containerState, error) {
2018-04-18 14:56:13 +00:00
if cs := s.checkContainer(containerType); nil != cs {
return cs, nil
}
cmd, pidFilePath := cotainerCommand(containerType)
removePidFile(pidFilePath)
if err := cmd.Start(); nil != err {
logging.Logger().Errorf("to run Container[%s] failed err %v", containerType.String(), err)
return nil, err
}
port, err := watchPidFileCreate(pidFilePath, time.Duration(time.Second*2))
if nil != err {
return nil, err
}
2018-04-27 07:42:59 +00:00
// go func(containerType occp.ContainerType, cmd *exec.Cmd) {
// if err := cmd.Wait(); nil != err {
// logging.Logger().Error(err)
// }
// logging.Logger().Infof("Container[%s] has been stopped", containerType.String())
// s.refreshContainer(containerType)
// }(containerType, cmd)
2018-04-18 14:56:13 +00:00
cs := &containerState{
containerType: containerType,
cmd: cmd,
port: port,
}
return cs, nil
}
2018-04-13 11:59:46 +00:00
2018-04-26 08:39:32 +00:00
func (s *ContainerService) refreshContainer(containerType occp.ContainerType) {
2018-04-20 11:19:14 +00:00
cs := s.checkContainer(containerType)
if nil == cs {
if _, err := s.getClient(containerType); nil != err {
logging.Logger().Error(err)
}
return
}
2018-04-29 09:08:38 +00:00
cs.cmd.Process.Kill()
cs.cmd = nil
2018-04-20 11:19:14 +00:00
delete(s.connectorMap, cs.client.Connector)
err := cs.client.Stop(context.Background())
if nil != err {
logging.Logger().Error(err)
}
cs.client = nil
2018-04-27 07:42:59 +00:00
delete(s.containerStates, containerType)
2018-04-20 11:19:14 +00:00
if _, err := s.getClient(containerType); nil != err {
logging.Logger().Error(err)
}
//
//if cs, ok := s.checkContainer(containerType); nil == cs {
// cs, ok := s.containerStates[containerType]
// if !ok {
// return
// }
// delete(s.connectorMap, cs.client.Connector)
//
// logging.Logger().Debugf("Client[%s]11 has been disconnected", cs.containerType.String())
// err := cs.client.Stop(context.Background())
// if nil != err {
// logging.Logger().Error(err)
// }
//
// client, err := container.NewClient(cs.containerType, cs.port, s.rpcServiceMap[cs.containerType])
// if nil != err {
// s.removeContainerState(cs.containerType)
// logging.Logger().Error(err)
// return
// }
// if err := client.Start(); nil != err {
// s.removeContainerState(cs.containerType)
// logging.Logger().Error(err)
// return
// }
// cs.client = client
// cs.client.Connector.SetOnDisconnected(s.onDisconnected)
// s.connectorMap[cs.client.Connector] = cs
//} else {
// cs, ok := s.containerStates[containerType]
// if !ok {
// return
// }
// delete(s.connectorMap, cs.client.Connector)
// err := cs.client.Stop(context.Background())
// if nil != err {
// logging.Logger().Error(err)
// }
// delete(s.containerStates, containerType)
// _, err = s.getClient(containerType)
// if nil != err {
// logging.Logger().Error(err)
// }
//}
}
2018-04-26 08:39:32 +00:00
func (s *ContainerService) debugContainer(containerType occp.ContainerType) (*containerState, error) {
2018-04-19 15:03:58 +00:00
cs := &containerState{
containerType: containerType,
cmd: nil,
port: 60000,
}
return cs, nil
}
2018-04-26 08:39:32 +00:00
func (s *ContainerService) checkContainer(containerType occp.ContainerType) *containerState {
2018-04-18 14:56:13 +00:00
cs, ok := s.containerStates[containerType]
if !ok || nil == cs {
return nil
}
2018-04-27 07:42:59 +00:00
// p, err := os.FindProcess(cs.cmd.Process.Pid)
// if nil != err {
// s.removeContainerState(containerType)
// return nil
// }
// if nil == p {
// s.removeContainerState(containerType)
// return nil
// }
// if nil != cs.cmd.ProcessState && cs.cmd.ProcessState.Exited() {
// s.removeContainerState(containerType)
// return nil
// }
2018-04-18 14:56:13 +00:00
return cs
}
2018-04-26 08:39:32 +00:00
func (s *ContainerService) killContainer(containerType occp.ContainerType) error {
2018-04-18 14:56:13 +00:00
cs, ok := s.containerStates[containerType]
if !ok || nil == cs {
return nil
}
if nil == cs.cmd.ProcessState || !cs.cmd.ProcessState.Exited() {
if err := cs.cmd.Process.Kill(); nil != err {
return err
}
}
2018-04-13 11:59:46 +00:00
return nil
}
2018-04-18 14:56:13 +00:00
2018-04-26 08:39:32 +00:00
func (s *ContainerService) removeContainerState(containerType occp.ContainerType) {
2018-04-18 14:56:13 +00:00
cs, ok := s.containerStates[containerType]
if !ok || nil == cs {
return
}
2018-04-27 07:42:59 +00:00
if nil != cs.client {
delete(s.connectorMap, cs.client.Connector)
cs.client.Stop(context.Background())
}
2018-04-18 14:56:13 +00:00
s.killContainer(containerType)
delete(s.containerStates, containerType)
}
2018-04-26 08:39:32 +00:00
func cotainerCommand(containerType occp.ContainerType) (cmd *exec.Cmd, pidFilePath string) {
2018-04-18 14:56:13 +00:00
pidFilePath = config.ContainerPIDFilePath(containerType)
loggingConfigFilePath := config.ContainerLoggingConfigFilePath(containerType)
binFilePath := config.ContainerBinFilePath(containerType)
switch containerType {
2018-04-26 08:39:32 +00:00
case occp.ContainerDiscovery, occp.ContainerNetwork:
2018-04-18 14:56:13 +00:00
args := []string{
2018-04-26 08:39:32 +00:00
fmt.Sprintf("-%s=%s", occp.FlagPidFilePathName, pidFilePath),
fmt.Sprintf("-%s=%s", occp.FlagLoggingConfigFilePathName, loggingConfigFilePath),
2018-04-18 14:56:13 +00:00
}
cmd = exec.Command(binFilePath, args...)
2018-04-26 08:39:32 +00:00
case occp.ContainerGenernal:
2018-04-18 14:56:13 +00:00
args := []string{
"-jar",
binFilePath,
pidFilePath,
loggingConfigFilePath,
}
cmd = exec.Command(config.JavaBinPath(), args...)
}
return
}
func removePidFile(pidFilePath string) {
if _, err := os.Stat(pidFilePath); err == nil {
if err := os.Remove(pidFilePath); nil != err {
logging.Logger().Errorf("removing pid file has been failed [%v]", err)
}
}
}
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("pid file not exist")
}
time.Sleep(time.Duration(time.Millisecond * 100))
}
}
type containerState struct {
2018-04-26 08:39:32 +00:00
containerType occp.ContainerType
2018-04-18 14:56:13 +00:00
cmd *exec.Cmd
port int
client *crc.Client
}