提交 cc961ba3 编写于 作者: 雨爱无痕

Add kill timeout process

上级 7bdf3e73
......@@ -21,6 +21,7 @@ require (
github.com/go-git/go-git/v5 v5.4.2 // indirect
github.com/go-playground/validator/v10 v10.9.0
github.com/go-redis/redis/v8 v8.11.3
github.com/gofrs/uuid v4.2.0+incompatible // indirect
github.com/gorilla/websocket v1.4.2
github.com/iris-contrib/middleware/cors v0.0.0-20210110101738-6d0a4d799b5d
github.com/jameskeane/bcrypt v0.0.0-20120420032655-c3cd44c1e20f
......
package execHelper
import (
"fmt"
"os/exec"
"strings"
"syscall"
commConsts "github.com/easysoft/zentaoatf/internal/pkg/consts"
serverConfig "github.com/easysoft/zentaoatf/internal/server/config"
serverDomain "github.com/easysoft/zentaoatf/internal/server/modules/v1/domain"
commonUtils "github.com/easysoft/zentaoatf/pkg/lib/common"
"github.com/kataras/iris/v12/websocket"
)
......@@ -91,3 +95,40 @@ func setBuildTool(testSet *serverDomain.TestSet, req serverDomain.WsReq) {
testSet.BuildTool = commConsts.UnitBuildToolMap[strings.TrimSpace(arr[0])]
}
}
func killWinProcessByUUID(uuid string) {
cmd1 := exec.Command("cmd")
cmd1.SysProcAttr = &syscall.SysProcAttr{CmdLine: fmt.Sprintf(`/c WMIC path win32_process where "CommandLine like '%%%s%%'" get Processid,Caption`, uuid), HideWindow: true}
out, _ := cmd1.Output()
lines := strings.Split(string(out), "\n")
for index, line := range lines {
if index == 0 {
continue
}
line = strings.TrimSpace(line)
cols := strings.Split(line, " ")
if len(cols) > 3 {
fmt.Println(fmt.Sprintf(`taskkill /F /pid %s`, cols[3]))
cmd2 := exec.Command("cmd")
cmd2.SysProcAttr = &syscall.SysProcAttr{CmdLine: fmt.Sprintf(`/c taskkill /F /pid %s`, cols[3]), HideWindow: true}
cmd2.Start()
}
}
}
func killLinuxProcessByUUID(uuid string) {
command := fmt.Sprintf(`-c ps -ef | grep %s | grep -v "grep" | awk '{print $2}' | xargs kill -9`, uuid)
cmd := exec.Command("/bin/bash")
cmd.SysProcAttr = &syscall.SysProcAttr{CmdLine: command, HideWindow: true}
cmd.Start()
}
func KillProcessByUUID(uuid string) {
if commonUtils.IsWin() {
killWinProcessByUUID(uuid)
} else {
killLinuxProcessByUUID(uuid)
}
}
......@@ -21,6 +21,7 @@ import (
logUtils "github.com/easysoft/zentaoatf/pkg/lib/log"
stringUtils "github.com/easysoft/zentaoatf/pkg/lib/string"
"github.com/fatih/color"
"github.com/gofrs/uuid"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/websocket"
)
......@@ -40,8 +41,9 @@ func RunFile(filePath, workspacePath string, conf commDomain.WorkspaceConf,
}
ctxt, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(timeout))
defer cancel()
uuidString := ""
if commonUtils.IsWin() {
uuidString = uuid.Must(uuid.NewV4()).String()
scriptInterpreter := ""
if strings.ToLower(lang) != "bat" {
scriptInterpreter = configHelper.GetFieldVal(conf, stringUtils.UcFirst(lang))
......@@ -51,13 +53,13 @@ func RunFile(filePath, workspacePath string, conf commDomain.WorkspaceConf,
cmd = exec.CommandContext(ctxt, "cmd", "/C", scriptInterpreter, filePath, "|", "more")
} else {
if command, ok := commConsts.LangMap[lang]["CompiledCommand"]; ok && command != "" {
cmd = exec.CommandContext(ctxt, "cmd", "/C", scriptInterpreter, command, filePath)
cmd = exec.CommandContext(ctxt, "cmd", "/C", scriptInterpreter, command, filePath, "-uuid", uuidString)
} else {
cmd = exec.CommandContext(ctxt, "cmd", "/C", scriptInterpreter, filePath)
cmd = exec.CommandContext(ctxt, "cmd", "/C", scriptInterpreter, filePath, "-uuid", uuidString)
}
}
} else if strings.ToLower(lang) == "bat" {
cmd = exec.CommandContext(ctxt, "cmd", "/C", filePath)
cmd = exec.CommandContext(ctxt, "cmd", "/C", filePath, "-uuid", uuidString)
} else {
msg := i118Utils.I118Prt.Sprintf("no_interpreter_for_run", lang, filePath)
if commConsts.ExecFrom != commConsts.FromCmd {
......@@ -140,12 +142,15 @@ func RunFile(filePath, workspacePath string, conf commDomain.WorkspaceConf,
}
cmd.Start()
go func() {
time.AfterFunc(time.Second*time.Duration(timeout), func() {
stdout.Close()
stderr.Close()
})
}()
if commonUtils.IsWin() {
go func() {
time.AfterFunc(time.Second*time.Duration(timeout), func() {
KillProcessByUUID(uuidString)
stdout.Close()
stderr.Close()
})
}()
}
isTerminal := false
reader1 := bufio.NewReader(stdout)
stdOutputArr := make([]string, 0)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册