Browse Source

fix: 解决僵尸进程的问题 (#4464)

pull/4468/head
ssongliu 8 months ago committed by GitHub
parent
commit
0bf6db1cf8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 18
      backend/app/api/v1/terminal.go
  2. 86
      backend/app/service/container.go
  3. 1
      frontend/src/views/container/container/log/index.vue

18
backend/app/api/v1/terminal.go

@ -102,7 +102,7 @@ func (b *BaseApi) RedisWsSsh(c *gin.Context) {
if wshandleError(wsConn, err) {
return
}
defer killBash(redisConf.ContainerName, commands, pidMap)
defer killBash(redisConf.ContainerName, pidMap)
defer slave.Close()
tty, err := terminal.NewLocalWsSession(cols, rows, wsConn, slave, false)
@ -170,7 +170,7 @@ func (b *BaseApi) ContainerWsSsh(c *gin.Context) {
if wshandleError(wsConn, err) {
return
}
defer killBash(containerID, command, pidMap)
defer killBash(containerID, pidMap)
defer slave.Close()
tty, err := terminal.NewLocalWsSession(cols, rows, wsConn, slave, true)
@ -221,26 +221,26 @@ func loadMapFromDockerTop(containerID string) map[string]string {
lines := strings.Split(stdout, "\n")
for _, line := range lines {
parts := strings.Fields(line)
if len(parts) != 2 {
if len(parts) == 0 {
continue
}
pidMap[parts[0]] = parts[1]
pidMap[parts[0]] = strings.Join(parts, " ")
}
return pidMap
}
func killBash(containerID, comm string, pidMap map[string]string) {
func killBash(containerID string, pidMap map[string]string) {
sudo := cmd.SudoHandleCmd()
newPidMap := loadMapFromDockerTop(containerID)
for pid, command := range newPidMap {
for pid, newCmd := range newPidMap {
isOld := false
for pid2 := range pidMap {
if pid == pid2 {
for pid2, oldCmd := range pidMap {
if pid == pid2 && oldCmd == newCmd {
isOld = true
break
}
}
if !isOld && command == comm {
if !isOld {
_, _ = cmd.Execf("%s kill -9 %s", sudo, pid)
}
}

86
backend/app/service/container.go

@ -626,22 +626,26 @@ func (u *ContainerService) ContainerLogs(wsConn *websocket.Conn, containerType,
if cmd.CheckIllegal(container, since, tail) {
return buserr.New(constant.ErrCmdIllegal)
}
command := fmt.Sprintf("docker logs %s", container)
commandName := "docker"
commandArg := []string{"logs", container}
if containerType == "compose" {
command = fmt.Sprintf("docker-compose -f %s logs", container)
commandName = "docker-compose"
commandArg = []string{"-f", container, "logs"}
}
if tail != "0" {
command += " --tail " + tail
commandArg = append(commandArg, "--tail")
commandArg = append(commandArg, tail)
}
if since != "all" {
command += " --since " + since
commandArg = append(commandArg, "--since")
commandArg = append(commandArg, since)
}
if follow {
command += " -f"
commandArg = append(commandArg, "-f")
}
command += " 2>&1"
cmd := exec.Command("bash", "-c", command)
if !follow {
commandArg = append(commandArg, "2>&1")
cmd := exec.Command(commandName, commandArg...)
stdout, _ := cmd.CombinedOutput()
if !utf8.Valid(stdout) {
return errors.New("invalid utf8")
@ -652,50 +656,52 @@ func (u *ContainerService) ContainerLogs(wsConn *websocket.Conn, containerType,
return nil
}
cmd := exec.Command(commandName, commandArg...)
stdout, err := cmd.StdoutPipe()
if err != nil {
_ = cmd.Process.Signal(syscall.SIGTERM)
return err
}
if err := cmd.Start(); err != nil {
_ = cmd.Process.Signal(syscall.SIGTERM)
return err
}
defer func() {
_ = cmd.Process.Signal(syscall.SIGTERM)
_ = cmd.Wait()
exitCh := make(chan struct{})
go func() {
_, wsData, _ := wsConn.ReadMessage()
if string(wsData) == "close conn" {
_ = cmd.Process.Signal(syscall.SIGTERM)
exitCh <- struct{}{}
}
}()
var exitCh chan struct{}
if follow {
go func() {
_, wsData, _ := wsConn.ReadMessage()
if string(wsData) == "close conn" {
exitCh <- struct{}{}
}
}()
}
buffer := make([]byte, 1024)
for {
select {
case <-exitCh:
return nil
default:
n, err := stdout.Read(buffer)
if err != nil {
if err == io.EOF {
return err
go func() {
buffer := make([]byte, 1024)
for {
select {
case <-exitCh:
return
default:
n, err := stdout.Read(buffer)
if err != nil {
if err == io.EOF {
return
}
global.LOG.Errorf("read bytes from log failed, err: %v", err)
continue
}
if !utf8.Valid(buffer[:n]) {
continue
}
if err = wsConn.WriteMessage(websocket.TextMessage, buffer[:n]); err != nil {
global.LOG.Errorf("send message with log to ws failed, err: %v", err)
return
}
global.LOG.Errorf("read bytes from log failed, err: %v", err)
continue
}
if !utf8.Valid(buffer[:n]) {
continue
}
if err = wsConn.WriteMessage(websocket.TextMessage, buffer[:n]); err != nil {
global.LOG.Errorf("send message with log to ws failed, err: %v", err)
return err
}
}
}
}()
_ = cmd.Wait()
return nil
}
func (u *ContainerService) ContainerStats(id string) (*dto.ContainerStats, error) {

1
frontend/src/views/container/container/log/index.vue

@ -143,6 +143,7 @@ const searchLogs = async () => {
MsgError(i18n.global.t('container.linesHelper'));
return;
}
terminalSocket.value?.send('close conn');
terminalSocket.value?.close();
logInfo.value = '';
const href = window.location.href;

Loading…
Cancel
Save