镜像自地址
https://github.com/tuna/tunasync.git
已同步 2025-12-06 14:36:47 +00:00
feature(worker): use cgroup track job process, so that they can be all-killed
这个提交包含在:
83
worker/cgroup.go
普通文件
83
worker/cgroup.go
普通文件
@@ -0,0 +1,83 @@
|
||||
package worker
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"syscall"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
|
||||
"github.com/codeskyblue/go-sh"
|
||||
)
|
||||
|
||||
type cgroupHook struct {
|
||||
emptyHook
|
||||
provider mirrorProvider
|
||||
basePath string
|
||||
baseGroup string
|
||||
created bool
|
||||
}
|
||||
|
||||
func newCgroupHook(p mirrorProvider, basePath, baseGroup string) *cgroupHook {
|
||||
if basePath == "" {
|
||||
basePath = "/sys/fs/cgroup"
|
||||
}
|
||||
if baseGroup == "" {
|
||||
baseGroup = "tunasync"
|
||||
}
|
||||
return &cgroupHook{
|
||||
provider: p,
|
||||
basePath: basePath,
|
||||
baseGroup: baseGroup,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *cgroupHook) preExec() error {
|
||||
c.created = true
|
||||
return sh.Command("cgcreate", "-g", c.Cgroup()).Run()
|
||||
}
|
||||
|
||||
func (c *cgroupHook) postExec() error {
|
||||
err := c.killAll()
|
||||
if err != nil {
|
||||
logger.Error("Error killing tasks: %s", err.Error())
|
||||
}
|
||||
|
||||
c.created = false
|
||||
return sh.Command("cgdelete", c.Cgroup()).Run()
|
||||
}
|
||||
|
||||
func (c *cgroupHook) Cgroup() string {
|
||||
name := c.provider.Name()
|
||||
return fmt.Sprintf("cpu:%s/%s", c.baseGroup, name)
|
||||
}
|
||||
|
||||
func (c *cgroupHook) killAll() error {
|
||||
if !c.created {
|
||||
return nil
|
||||
}
|
||||
name := c.provider.Name()
|
||||
taskFile, err := os.Open(filepath.Join(c.basePath, "cpu", c.baseGroup, name, "tasks"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer taskFile.Close()
|
||||
taskList := []int{}
|
||||
scanner := bufio.NewScanner(taskFile)
|
||||
for scanner.Scan() {
|
||||
pid, err := strconv.Atoi(scanner.Text())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
taskList = append(taskList, pid)
|
||||
}
|
||||
for _, pid := range taskList {
|
||||
logger.Debug("Killing process: %d", pid)
|
||||
unix.Kill(pid, syscall.SIGKILL)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
在新工单中引用
屏蔽一个用户