fix(worker): cgroup hook now ensure child progresses are killed

This commit is contained in:
bigeagle 2016-08-02 21:55:41 +08:00
parent c8af09f129
commit 437acd3f01
No known key found for this signature in database
GPG Key ID: 9171A4571C27920A

View File

@ -2,18 +2,20 @@ package worker
import ( import (
"bufio" "bufio"
"errors"
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
"syscall" "syscall"
"time"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
"github.com/codeskyblue/go-sh" "github.com/codeskyblue/go-sh"
) )
var cgSubsystem string = "cpu" var cgSubsystem = "cpu"
type cgroupHook struct { type cgroupHook struct {
emptyHook emptyHook
@ -82,24 +84,44 @@ func (c *cgroupHook) killAll() error {
return nil return nil
} }
name := c.provider.Name() name := c.provider.Name()
readTaskList := func() ([]int, error) {
taskList := []int{}
taskFile, err := os.Open(filepath.Join(c.basePath, cgSubsystem, c.baseGroup, name, "tasks")) taskFile, err := os.Open(filepath.Join(c.basePath, cgSubsystem, c.baseGroup, name, "tasks"))
if err != nil { if err != nil {
return err return taskList, err
} }
defer taskFile.Close() defer taskFile.Close()
taskList := []int{}
scanner := bufio.NewScanner(taskFile) scanner := bufio.NewScanner(taskFile)
for scanner.Scan() { for scanner.Scan() {
pid, err := strconv.Atoi(scanner.Text()) pid, err := strconv.Atoi(scanner.Text())
if err != nil { if err != nil {
return err return taskList, err
} }
taskList = append(taskList, pid) taskList = append(taskList, pid)
} }
return taskList, nil
}
for i := 0; i < 4; i++ {
if i == 3 {
return errors.New("Unable to kill all child tasks")
}
taskList, err := readTaskList()
if err != nil {
return err
}
if len(taskList) == 0 {
return nil
}
for _, pid := range taskList { for _, pid := range taskList {
logger.Debugf("Killing process: %d", pid) logger.Debugf("Killing process: %d", pid)
unix.Kill(pid, syscall.SIGKILL) unix.Kill(pid, syscall.SIGKILL)
} }
// sleep 10ms for the first round, and 1.01s, 2.01s, 3.01s for the rest
time.Sleep(time.Duration(i)*time.Second + 10*time.Millisecond)
}
return nil return nil
} }