11package commands
22
33import (
4+ "context"
45 "fmt"
56 "io/ioutil"
67 "os/exec"
78 "sync"
8- "syscall"
99 "time"
1010
1111 "github.com/gofrs/uuid"
@@ -88,12 +88,12 @@ func execute(command string, stdin []byte, environment map[string]string) ([]byt
8888 mux .RLock ()
8989 defer mux .RUnlock ()
9090
91- cmdConfig , ok := commands [command ]
91+ cmd , ok := commands [command ]
9292 if ! ok {
9393 return nil , nil , errors .New ("command does not exist" )
9494 }
9595
96- cmdArgs , err := ParseCommandLine (cmdConfig .Command )
96+ cmdArgs , err := ParseCommandLine (cmd .Command )
9797 if err != nil {
9898 return nil , nil , errors .Wrap (err , "parse command error" )
9999 }
@@ -105,27 +105,28 @@ func execute(command string, stdin []byte, environment map[string]string) ([]byt
105105 "command" : command ,
106106 "exec" : cmdArgs [0 ],
107107 "args" : cmdArgs [1 :],
108- "max_execution_duration" : cmdConfig .MaxExecutionDuration ,
108+ "max_execution_duration" : cmd .MaxExecutionDuration ,
109109 }).Info ("commands: executing command" )
110110
111- cmd := exec . Command ( cmdArgs [ 0 ], cmdArgs [ 1 :] ... )
112- cmd . SysProcAttr = & syscall. SysProcAttr { Setpgid : true }
111+ ctx , cancel := context . WithDeadline ( context . Background (), time . Now (). Add ( cmd . MaxExecutionDuration ) )
112+ defer cancel ()
113113
114+ cmdCtx := exec .CommandContext (ctx , cmdArgs [0 ], cmdArgs [1 :]... )
114115 for k , v := range environment {
115- cmd .Env = append (cmd .Env , fmt .Sprintf ("%s=%s" , k , v ))
116+ cmdCtx .Env = append (cmdCtx .Env , fmt .Sprintf ("%s=%s" , k , v ))
116117 }
117118
118- stdinPipe , err := cmd .StdinPipe ()
119+ stdinPipe , err := cmdCtx .StdinPipe ()
119120 if err != nil {
120121 return nil , nil , errors .Wrap (err , "get stdin pipe error" )
121122 }
122123
123- stdoutPipe , err := cmd .StdoutPipe ()
124+ stdoutPipe , err := cmdCtx .StdoutPipe ()
124125 if err != nil {
125126 return nil , nil , errors .Wrap (err , "get stdout pipe error" )
126127 }
127128
128- stderrPipe , err := cmd .StderrPipe ()
129+ stderrPipe , err := cmdCtx .StderrPipe ()
129130 if err != nil {
130131 return nil , nil , errors .Wrap (err , "get stderr pipe error" )
131132 }
@@ -137,23 +138,14 @@ func execute(command string, stdin []byte, environment map[string]string) ([]byt
137138 }
138139 }()
139140
140- if err := cmd .Start (); err != nil {
141+ if err := cmdCtx .Start (); err != nil {
141142 return nil , nil , errors .Wrap (err , "starting command error" )
142143 }
143144
144- time .AfterFunc (cmdConfig .MaxExecutionDuration , func () {
145- pgid , err := syscall .Getpgid (cmd .Process .Pid )
146- if err == nil {
147- if err := syscall .Kill (- pgid , syscall .SIGKILL ); err != nil {
148- panic (err )
149- }
150- }
151- })
152-
153145 stdoutB , _ := ioutil .ReadAll (stdoutPipe )
154146 stderrB , _ := ioutil .ReadAll (stderrPipe )
155147
156- if err := cmd .Wait (); err != nil {
148+ if err := cmdCtx .Wait (); err != nil {
157149 return nil , nil , errors .Wrap (err , "waiting for command to finish error" )
158150 }
159151
0 commit comments