probe/container/container-session.go

189 lines
4.5 KiB
Go
Raw Normal View History

2018-05-03 11:24:07 +00:00
package container
import (
"fmt"
"os/exec"
"strconv"
"sync"
"time"
logging "git.loafle.net/commons/logging-go"
2018-07-01 04:09:50 +00:00
css "git.loafle.net/commons/server-go/socket"
2018-05-03 11:24:07 +00:00
occp "git.loafle.net/overflow/commons-go/config/probe"
"git.loafle.net/overflow/probe/config"
)
type OnConnectInfo struct {
ContainerType occp.ContainerType
2018-07-01 04:09:50 +00:00
WriteChan chan<- css.SocketMessage
2018-05-03 11:24:07 +00:00
}
func NewContainerSession(containerType occp.ContainerType) *ContainerSession {
cs := &ContainerSession{
containerType: containerType,
2018-07-01 04:09:50 +00:00
writeChan: make(chan css.SocketMessage, 256),
2018-05-03 11:24:07 +00:00
}
return cs
}
type ContainerSession struct {
containerType occp.ContainerType
cmd *exec.Cmd
2018-07-01 04:09:50 +00:00
writeChan chan css.SocketMessage
containerWriteChan chan<- css.SocketMessage
2018-05-03 11:24:07 +00:00
writeStopChan chan struct{}
stopWg sync.WaitGroup
}
func (cs *ContainerSession) Start() error {
if err := cs.runContainer(); nil != err {
return err
}
cs.writeStopChan = make(chan struct{})
cs.stopWg.Add(1)
go cs.handleWrite()
return nil
}
func (cs *ContainerSession) Stop() {
close(cs.writeStopChan)
if err := cs.killContainer(); nil != err {
logging.Logger().Error(err)
}
cs.stopWg.Wait()
}
2018-07-01 04:09:50 +00:00
func (cs *ContainerSession) Connected(writeChan chan<- css.SocketMessage) {
2018-05-03 11:24:07 +00:00
logging.Logger().Debugf("Container[%s] has been connected", cs.containerType.String())
cs.containerWriteChan = writeChan
}
func (cs *ContainerSession) Disconnected() {
logging.Logger().Debugf("Container[%s] has been disconnected", cs.containerType.String())
cs.refreshContainer()
}
2018-07-01 04:09:50 +00:00
func (cs *ContainerSession) Send(messageType int, message []byte) error {
2018-05-03 11:24:07 +00:00
select {
2018-07-01 04:09:50 +00:00
case cs.writeChan <- css.MakeSocketMessage(messageType, message):
logging.Logger().Debugf("send buffer of Container[%s] has been queued %s", cs.containerType.String(), string(message))
2018-05-03 11:24:07 +00:00
return nil
default:
return fmt.Errorf("Buffer of Container[%s] is full", cs.containerType.String())
}
}
func (cs *ContainerSession) handleWrite() {
defer func() {
cs.stopWg.Done()
}()
var (
2018-07-01 04:09:50 +00:00
socketMessage css.SocketMessage
messageType int
message []byte
ok bool
2018-05-03 11:24:07 +00:00
)
for {
select {
2018-07-01 04:09:50 +00:00
case socketMessage, ok = <-cs.writeChan:
2018-05-03 11:24:07 +00:00
if !ok {
logging.Logger().Debugf("WriteChan of Container[%s] has been closed", cs.containerType.String())
return
}
2018-07-01 04:09:50 +00:00
messageType, message = socketMessage()
2018-05-03 11:24:07 +00:00
LOOP_WRITE:
for {
select {
2018-07-01 04:09:50 +00:00
case cs.containerWriteChan <- css.MakeSocketMessage(messageType, message):
2018-05-03 11:24:07 +00:00
logging.Logger().Debugf("message of Container[%s] has been sended %s", cs.containerType.String(), string(message))
break LOOP_WRITE
case <-cs.writeStopChan:
return
default:
time.Sleep(100 * time.Millisecond)
continue LOOP_WRITE
}
}
case <-cs.writeStopChan:
return
}
}
}
func (cs *ContainerSession) runContainer() error {
cmd := cotainerCommand(cs.containerType)
if err := cmd.Start(); nil != err {
logging.Logger().Errorf("to run Container[%s] failed err %v", cs.containerType.String(), err)
return err
}
cs.cmd = cmd
logging.Logger().Debugf("Process of Container[%s] has been started", cs.containerType.String())
return nil
}
func (cs *ContainerSession) killContainer() error {
defer func() {
cs.cmd = nil
logging.Logger().Debugf("Process of Container[%s] has been stopped", cs.containerType.String())
}()
if nil == cs.cmd {
return nil
}
2018-05-08 09:33:11 +00:00
// if nil == cs.cmd.ProcessState || !cs.cmd.ProcessState.Exited() {
if err := cs.cmd.Process.Kill(); nil != err {
return err
2018-05-03 11:24:07 +00:00
}
2018-05-08 09:33:11 +00:00
// }
2018-05-03 11:24:07 +00:00
return nil
}
func (cs *ContainerSession) refreshContainer() {
if err := cs.killContainer(); nil != err {
logging.Logger().Error(err)
}
if err := cs.runContainer(); nil != err {
logging.Logger().Error(err)
}
logging.Logger().Debugf("Process of Container[%s] has been refreshed", cs.containerType.String())
}
func cotainerCommand(containerType occp.ContainerType) (cmd *exec.Cmd) {
loggingConfigFilePath := config.ContainerLoggingConfigFilePath(containerType)
binFilePath := config.ContainerBinFilePath(containerType)
switch containerType {
case occp.ContainerDiscovery, occp.ContainerNetwork:
args := []string{
fmt.Sprintf("-%s=%s", occp.FlagProbePortName, strconv.FormatInt(config.ProbePortNumber, 10)),
fmt.Sprintf("-%s=%s", occp.FlagLoggingConfigFilePathName, loggingConfigFilePath),
}
cmd = exec.Command(binFilePath, args...)
2018-05-04 11:45:23 +00:00
case occp.ContainerGeneral:
2018-05-03 11:24:07 +00:00
args := []string{
"-jar",
binFilePath,
strconv.FormatInt(config.ProbePortNumber, 10),
2018-05-04 11:45:23 +00:00
fmt.Sprintf("-Dlogging.config=file:\"%s\"", loggingConfigFilePath),
2018-05-03 11:24:07 +00:00
}
cmd = exec.Command(config.JavaBinPath(), args...)
}
return
}