fix(tasks): fix task cancellation process tree leak, emit cancelled events, and refine status badges styling

This commit is contained in:
duorameng
2026-06-29 22:34:08 +08:00
parent e5fc716b8f
commit b52af6c4fd
10 changed files with 132 additions and 73 deletions
+6 -5
View File
@@ -81,11 +81,12 @@ const (
EventPasswordChanged = "password_changed"
// 任务事件类型
EventTaskSuccess = "task_success"
EventTaskFailed = "task_failed"
EventTaskTimeout = "task_timeout"
EventTaskRunning = "task_running"
EventTaskQueued = "task_queued"
EventTaskSuccess = "task_success"
EventTaskFailed = "task_failed"
EventTaskTimeout = "task_timeout"
EventTaskRunning = "task_running"
EventTaskQueued = "task_queued"
EventTaskCancelled = "task_cancelled"
// 其他事件类型
EventSystemNotice = "system_notice"
+3
View File
@@ -161,6 +161,9 @@ func ExecuteWithHooks(ctx context.Context, req Request, stdout, stderr io.Writer
shell, args := utils.GetShellCommand(req.Command)
cmd := exec.CommandContext(execCtx, shell, args...)
usePty := runtime.GOOS != "windows" && stdout != nil && (stdout == stderr || stdout == io.Discard)
SetProcessGroupAndCancel(cmd, usePty)
// 设置工作目录
// 设置工作目录
workDir := strings.TrimSpace(req.WorkDir)
+21
View File
@@ -0,0 +1,21 @@
//go:build !windows
package executor
import (
"os/exec"
"syscall"
)
func SetProcessGroupAndCancel(cmd *exec.Cmd, usePty bool) {
if !usePty {
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
}
cmd.Cancel = func() error {
if cmd.Process != nil {
// Kill the entire process group by sending SIGKILL to negative PID
return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
}
return nil
}
}
+18
View File
@@ -0,0 +1,18 @@
//go:build windows
package executor
import (
"fmt"
"os/exec"
)
func SetProcessGroupAndCancel(cmd *exec.Cmd, usePty bool) {
cmd.Cancel = func() error {
if cmd.Process != nil {
killCmd := exec.Command("taskkill", "/F", "/T", "/PID", fmt.Sprintf("%d", cmd.Process.Pid))
return killCmd.Run()
}
return nil
}
}
+1
View File
@@ -100,6 +100,7 @@ func (m *SystemWSManager) SubscribeEvents(bus *eventbus.EventBus) {
constant.EventTaskTimeout,
constant.EventTaskRunning,
constant.EventTaskQueued,
constant.EventTaskCancelled,
}
for _, evt := range taskEvents {
@@ -304,6 +304,8 @@ func (h *ServerSchedulerHandler) OnTaskCompleted(req *executor.ExecutionRequest,
eventType = constant.EventTaskFailed
case constant.TaskStatusTimeout:
eventType = constant.EventTaskTimeout
case constant.TaskStatusCancelled:
eventType = constant.EventTaskCancelled
}
if eventType != "" {
eventbus.DefaultBus.Publish(eventbus.Event{