mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-12-17 21:08:25 +08:00
fix: Fix cronjob manual stop not working (#11300)
This commit is contained in:
parent
8606427e8c
commit
a9c9f0272a
1 changed files with 31 additions and 10 deletions
|
|
@ -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 {
|
if err != nil {
|
||||||
if err.Error() == "signal: killed" {
|
|
||||||
return "", buserr.New("ErrShutDown")
|
|
||||||
}
|
|
||||||
return handleErr(stdout, stderr, c.IgnoreExist1, err)
|
return handleErr(stdout, stderr, c.IgnoreExist1, err)
|
||||||
}
|
}
|
||||||
return stdout.String(), nil
|
return stdout.String(), nil
|
||||||
|
case <-newContext.Done():
|
||||||
|
if cmd.Process != nil && cmd.Process.Pid > 0 {
|
||||||
|
syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
|
||||||
|
}
|
||||||
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func WithContext(ctx context.Context) Option {
|
func WithContext(ctx context.Context) Option {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue