Skip to content

Commit 7c809a0

Browse files
committed
feat: propagate signals to launched subprocesses so that they can properly clean up resources
1 parent 09dec2d commit 7c809a0

2 files changed

Lines changed: 59 additions & 1 deletion

File tree

command/cleanup.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import (
77
"log"
88
"os"
99
"os/exec"
10+
"os/signal"
1011
"strconv"
1112
"strings"
13+
"syscall"
1214

1315
"github.com/google/go-github/github"
1416
cli "gopkg.in/urfave/cli.v1"
@@ -71,7 +73,24 @@ func CmdCleanup(c *cli.Context) (err error) {
7173
cmd.Stdout = os.Stdout
7274
cmd.Stderr = os.Stderr
7375

76+
signalChannel := make(chan os.Signal, 1)
77+
signal.Notify(signalChannel, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
78+
go func() {
79+
for sig := range signalChannel {
80+
if cmd.Process != nil {
81+
err := cmd.Process.Signal(sig)
82+
if err != nil {
83+
log.Printf("error sending signal to child process (%d): %s\n", cmd.Process.Pid, err)
84+
}
85+
} else {
86+
// TODO: is this always the right thing to do if we're not running a subprocess?
87+
os.Exit(1)
88+
}
89+
}
90+
}()
91+
7492
err = cmd.Run()
93+
signal.Stop(signalChannel)
7594
if err != nil {
7695
log.Println("undeploy error: ", err)
7796

@@ -108,7 +127,25 @@ func listDeployedPullRequests(listScript string) ([]string, error) {
108127
cmd := exec.Command(listScript)
109128
cmd.Stdout = &stdout
110129

111-
if err := cmd.Run(); err != nil {
130+
signalChannel := make(chan os.Signal, 1)
131+
signal.Notify(signalChannel, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
132+
go func() {
133+
for sig := range signalChannel {
134+
if cmd.Process != nil {
135+
err := cmd.Process.Signal(sig)
136+
if err != nil {
137+
log.Printf("error sending signal to child process (%d): %s\n", cmd.Process.Pid, err)
138+
}
139+
} else {
140+
// TODO: is this always the right thing to do if we're not running a subprocess?
141+
os.Exit(1)
142+
}
143+
}
144+
}()
145+
146+
err := cmd.Run()
147+
signal.Stop(signalChannel)
148+
if err != nil {
112149
return nil, err
113150
}
114151

command/please.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import (
99
"log"
1010
"os"
1111
"os/exec"
12+
"os/signal"
1213
"strconv"
1314
"strings"
15+
"syscall"
1416

1517
"github.com/google/go-github/github"
1618
cli "gopkg.in/urfave/cli.v1"
@@ -151,6 +153,23 @@ func CmdPlease(c *cli.Context) (err error) {
151153
}
152154

153155
// Start deploy script
156+
signalChannel := make(chan os.Signal, 1)
157+
signal.Notify(signalChannel, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
158+
go func() {
159+
cmd := cmd
160+
for sig := range signalChannel {
161+
if cmd.Process != nil {
162+
err := cmd.Process.Signal(sig)
163+
if err != nil {
164+
log.Printf("error sending signal to child process (%d): %s\n", cmd.Process.Pid, err)
165+
}
166+
} else {
167+
// TODO: is this always the right thing to do if we're not running a subprocess?
168+
os.Exit(1)
169+
}
170+
}
171+
}()
172+
154173
err = cmd.Start()
155174
if err != nil {
156175
err2 := updateStatus(StateError, "")
@@ -169,6 +188,8 @@ func CmdPlease(c *cli.Context) (err error) {
169188

170189
// Wait on the deploy to finish
171190
err = cmd.Wait()
191+
signal.Stop(signalChannel)
192+
172193
if err != nil {
173194
err2 := updateStatus(StateFailure, "")
174195
if err2 != nil {

0 commit comments

Comments
 (0)