mirror of https://github.com/ouqiang/gocron
顶层函数增加panic捕获
parent
8e6b212b1d
commit
2bf6ea0b6a
|
@ -12,6 +12,11 @@ import (
|
|||
type Server struct {}
|
||||
|
||||
func (s Server) Run(ctx context.Context, req *pb.TaskRequest) (*pb.TaskResponse, error) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
grpclog.Println(err)
|
||||
}
|
||||
} ()
|
||||
output, err := utils.ExecShell(ctx, req.Command)
|
||||
resp := new(pb.TaskResponse)
|
||||
resp.Output = output
|
||||
|
|
|
@ -27,7 +27,9 @@ func ExecShell(ctx context.Context, command string) (string, error) {
|
|||
}()
|
||||
select {
|
||||
case <- ctx.Done():
|
||||
syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
|
||||
if cmd.Process.Pid > 0 {
|
||||
syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
|
||||
}
|
||||
return "", errors.New("timeout killed")
|
||||
case result := <- resultChan:
|
||||
return result.output, result.err
|
||||
|
|
|
@ -29,8 +29,10 @@ func ExecShell(ctx context.Context, command string) (string, error) {
|
|||
}()
|
||||
select {
|
||||
case <- ctx.Done():
|
||||
exec.Command("taskkill", "/F", "/T", "/PID", strconv.Itoa(cmd.Process.Pid)).Run()
|
||||
cmd.Process.Kill()
|
||||
if cmd.Process.Pid > 0 {
|
||||
exec.Command("taskkill", "/F", "/T", "/PID", strconv.Itoa(cmd.Process.Pid)).Run()
|
||||
cmd.Process.Kill()
|
||||
}
|
||||
return "", errors.New("timeout killed")
|
||||
case result := <- resultChan:
|
||||
return ConvertEncoding(result.output), result.err
|
||||
|
|
Loading…
Reference in New Issue