顶层函数增加panic捕获

pull/21/merge
ouqiang 2017-05-29 15:09:41 +08:00
parent 8e6b212b1d
commit 2bf6ea0b6a
3 changed files with 12 additions and 3 deletions

View File

@ -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

View File

@ -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

View File

@ -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