fix: Fix cronjob manual stop not working (#11300)

This commit is contained in:
ssongliu 2025-12-11 17:00:22 +08:00 committed by GitHub
parent 8606427e8c
commit a9c9f0272a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -108,12 +108,16 @@ func (c *CommandHelper) run(name string, arg ...string) (string, error) {
cmd = exec.CommandContext(newContext, name, arg...) cmd = exec.CommandContext(newContext, name, arg...)
} else { } else {
if c.context == nil { if c.context == nil {
newContext = context.Background()
cmd = exec.Command(name, arg...) cmd = exec.Command(name, arg...)
} else { } else {
newContext = c.context newContext = c.context
cmd = exec.CommandContext(c.context, name, arg...) cmd = exec.CommandContext(c.context, name, arg...)
} }
} }
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
}
customWriter := &CustomWriter{taskItem: c.taskItem} customWriter := &CustomWriter{taskItem: c.taskItem}
var stdout, stderr bytes.Buffer var stdout, stderr bytes.Buffer
@ -145,22 +149,39 @@ func (c *CommandHelper) run(name string, arg ...string) (string, error) {
cmd.Dir = c.workDir cmd.Dir = c.workDir
} }
err := cmd.Run() if err := cmd.Start(); err != nil {
return "", fmt.Errorf("cmd.Start() failed with '%s'\n", err)
}
if c.taskItem != nil { if c.taskItem != nil {
customWriter.Flush() customWriter.Flush()
} }
if c.timeout != 0 {
if newContext != nil && errors.Is(newContext.Err(), context.DeadlineExceeded) { done := make(chan error, 1)
return "", buserr.New("ErrCmdTimeout") go func() {
done <- cmd.Wait()
}()
select {
case err := <-done:
if err != nil {
return handleErr(stdout, stderr, c.IgnoreExist1, err)
} }
} return stdout.String(), nil
if err != nil { case <-newContext.Done():
if err.Error() == "signal: killed" { if cmd.Process != nil && cmd.Process.Pid > 0 {
return "", buserr.New("ErrShutDown") syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
} }
return handleErr(stdout, stderr, c.IgnoreExist1, err) var err error
switch newContext.Err() {
case context.DeadlineExceeded:
err = buserr.New("ErrCmdTimeout")
case context.Canceled:
err = buserr.New("ErrShutDown")
default:
err = newContext.Err()
}
<-done
return "", err
} }
return stdout.String(), nil
} }
func WithContext(ctx context.Context) Option { func WithContext(ctx context.Context) Option {