This commit is contained in:
geek
2018-04-20 20:19:14 +09:00
parent 264c1fc72b
commit 3d1a0e1686
4 changed files with 90 additions and 6 deletions

View File

@@ -86,8 +86,8 @@ func (s *ContainerService) Send(containerType ocpcc.ContainerType, method string
func (s *ContainerService) getClient(containerType ocpcc.ContainerType) (*crc.Client, error) {
cs := s.checkContainer(containerType)
if nil == cs {
// _cs, err := s.runContainer(containerType)
_cs, err := s.debugContainer(containerType)
_cs, err := s.runContainer(containerType)
//_cs, err := s.debugContainer(containerType)
if nil != err {
return nil, err
}
@@ -118,7 +118,7 @@ func (s *ContainerService) onDisconnected(connector csc.Connector) {
if !ok || nil == cs {
return
}
logging.Logger().Debugf("Client[%s] has been disconnected", cs.containerType.String())
s.refreshContainer(cs.containerType)
}
func (s *ContainerService) runContainer(containerType ocpcc.ContainerType) (*containerState, error) {
@@ -144,6 +144,7 @@ func (s *ContainerService) runContainer(containerType ocpcc.ContainerType) (*con
logging.Logger().Error(err)
}
logging.Logger().Infof("Container[%s] has been stopped", containerType.String())
s.refreshContainer(containerType)
}(containerType, cmd)
cs := &containerState{
@@ -155,6 +156,71 @@ func (s *ContainerService) runContainer(containerType ocpcc.ContainerType) (*con
return cs, nil
}
func (s *ContainerService) refreshContainer(containerType ocpcc.ContainerType) {
cs := s.checkContainer(containerType)
if nil == cs {
if _, err := s.getClient(containerType); nil != err {
logging.Logger().Error(err)
}
return
}
delete(s.connectorMap, cs.client.Connector)
err := cs.client.Stop(context.Background())
if nil != err {
logging.Logger().Error(err)
}
cs.client = nil
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)
// }
//}
}
func (s *ContainerService) debugContainer(containerType ocpcc.ContainerType) (*containerState, error) {
cs := &containerState{
containerType: containerType,
@@ -171,10 +237,20 @@ func (s *ContainerService) checkContainer(containerType ocpcc.ContainerType) *co
return nil
}
if nil != cs.cmd.ProcessState && cs.cmd.ProcessState.Exited() {
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
//}
return cs
}