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