Skip to content

Commit

Permalink
feat: propagate signals to launched subprocesses so that they can pro…
Browse files Browse the repository at this point in the history
…perly clean up resources
  • Loading branch information
r-vdp committed Nov 18, 2024
1 parent 09dec2d commit 403c4af
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
15 changes: 14 additions & 1 deletion command/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"log"
"os"
"os/exec"
"os/signal"
"strconv"
"strings"
"syscall"

"github.com/google/go-github/github"
cli "gopkg.in/urfave/cli.v1"
Expand Down Expand Up @@ -71,7 +73,12 @@ func CmdCleanup(c *cli.Context) (err error) {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

signalChannel := make(chan os.Signal, 1)
signal.Notify(signalChannel, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
go propagateSignalsTo(cmd, signalChannel)

err = cmd.Run()
signal.Stop(signalChannel)
if err != nil {
log.Println("undeploy error: ", err)

Expand Down Expand Up @@ -108,7 +115,13 @@ func listDeployedPullRequests(listScript string) ([]string, error) {
cmd := exec.Command(listScript)
cmd.Stdout = &stdout

if err := cmd.Run(); err != nil {
signalChannel := make(chan os.Signal, 1)
signal.Notify(signalChannel, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
go propagateSignalsTo(cmd, signalChannel)

err := cmd.Run()
signal.Stop(signalChannel)
if err != nil {
return nil, err
}

Expand Down
8 changes: 8 additions & 0 deletions command/please.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
"log"
"os"
"os/exec"
"os/signal"
"strconv"
"strings"
"syscall"

"github.com/google/go-github/github"
cli "gopkg.in/urfave/cli.v1"
Expand Down Expand Up @@ -151,6 +153,10 @@ func CmdPlease(c *cli.Context) (err error) {
}

// Start deploy script
signalChannel := make(chan os.Signal, 1)
signal.Notify(signalChannel, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
go propagateSignalsTo(cmd, signalChannel)

err = cmd.Start()
if err != nil {
err2 := updateStatus(StateError, "")
Expand All @@ -169,6 +175,8 @@ func CmdPlease(c *cli.Context) (err error) {

// Wait on the deploy to finish
err = cmd.Wait()
signal.Stop(signalChannel)

if err != nil {
err2 := updateStatus(StateFailure, "")
if err2 != nil {
Expand Down
16 changes: 16 additions & 0 deletions command/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package command
import (
"context"
"log"
"os"
"os/exec"
"regexp"

"github.com/google/go-github/github"
Expand Down Expand Up @@ -61,3 +63,17 @@ func refString(str string) *string {
func refStringList(l []string) *[]string {
return &l
}

func propagateSignalsTo(cmd *exec.Cmd, signalChannel chan os.Signal) {
for sig := range signalChannel {
if cmd.Process != nil {
err := cmd.Process.Signal(sig)
if err != nil {
log.Printf("error sending signal to child process (%d): %s\n", cmd.Process.Pid, err)
}
} else {
// TODO: is this always the right thing to do if we're not running a subprocess?
os.Exit(1)
}
}
}

0 comments on commit 403c4af

Please sign in to comment.