9
9
"io"
10
10
"os"
11
11
"os/exec"
12
+ "os/signal"
12
13
"path/filepath"
13
14
"strconv"
14
15
"strings"
@@ -167,6 +168,9 @@ func StartProcessManager(
167
168
}
168
169
169
170
func runProcessManagerInForeground (cmd * exec.Cmd , config * globalProcessComposeConfig , port int , projectDir string , w io.Writer ) error {
171
+ sigs := make (chan os.Signal , 1 )
172
+ signal .Notify (sigs , syscall .SIGINT , syscall .SIGTERM , syscall .SIGHUP , syscall .SIGTSTP )
173
+
170
174
if err := cmd .Start (); err != nil {
171
175
return fmt .Errorf ("failed to start process-compose: %w" , err )
172
176
}
@@ -186,11 +190,31 @@ func runProcessManagerInForeground(cmd *exec.Cmd, config *globalProcessComposeCo
186
190
// We're waiting now, so we can unlock the file
187
191
config .File .Close ()
188
192
189
- err = cmd .Wait ()
193
+ done := make (chan error , 1 )
194
+ go func () {
195
+ done <- cmd .Wait ()
196
+ }()
197
+
198
+ go func () {
199
+ for sig := range sigs {
200
+ fmt .Printf ("Syscall Received: %s\n " , sig )
201
+ err := syscall .Kill (cmd .Process .Pid , syscall .SIGINT )
202
+ if err != nil {
203
+ fmt .Fprint (w , "Unable to terminate process-compose cleanly. You may need to manually terminate your services" )
204
+ }
205
+ }
206
+ }()
207
+
208
+ err = <- done
209
+ signal .Stop (sigs )
210
+ close (sigs )
190
211
if err != nil {
191
- if err .Error () == "exit status 1" {
192
- fmt .Fprintf (w , "Process-compose was terminated remotely, %s\n " , err .Error ())
193
- return nil
212
+ var exitErr * exec.ExitError
213
+ if errors .As (err , & exitErr ) {
214
+ if exitErr .ExitCode () == 1 {
215
+ fmt .Fprintf (w , "Process-compose was terminated, error: %s\n " , exitErr .Error ())
216
+ return nil
217
+ }
194
218
}
195
219
return err
196
220
}
@@ -200,8 +224,9 @@ func runProcessManagerInForeground(cmd *exec.Cmd, config *globalProcessComposeCo
200
224
return err
201
225
}
202
226
203
- config = readGlobalProcessComposeJSON ( configFile )
227
+ defer configFile . Close ( )
204
228
229
+ config = readGlobalProcessComposeJSON (configFile )
205
230
delete (config .Instances , projectDir )
206
231
return writeGlobalProcessComposeJSON (config , configFile )
207
232
}
0 commit comments