Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: propagate signals to launched subprocesses so that they can properly clean up resources #63

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
"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 @@ -86,8 +88,13 @@
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)

Check failure on line 93 in command/cleanup.go

View workflow job for this annotation

GitHub Actions / lint

only one cuddle assignment allowed before go statement (wsl)

err = cmd.Run()
signal.Stop(signalChannel)

Check failure on line 96 in command/cleanup.go

View workflow job for this annotation

GitHub Actions / lint

only cuddled expressions if assigning variable or using from line above (wsl)
if err != nil {

Check failure on line 97 in command/cleanup.go

View workflow job for this annotation

GitHub Actions / lint

if statements should only be cuddled with assignments (wsl)
log.Println("undeploy error: ", err)

lastErr = err
Expand Down Expand Up @@ -118,7 +125,13 @@
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)

Check failure on line 130 in command/cleanup.go

View workflow job for this annotation

GitHub Actions / lint

only one cuddle assignment allowed before go statement (wsl)

err := cmd.Run()
signal.Stop(signalChannel)

Check failure on line 133 in command/cleanup.go

View workflow job for this annotation

GitHub Actions / lint

only cuddled expressions if assigning variable or using from line above (wsl)
if err != nil {

Check failure on line 134 in command/cleanup.go

View workflow job for this annotation

GitHub Actions / lint

if statements should only be cuddled with assignments (wsl)
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 @@
"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 @@
}

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

Check failure on line 158 in command/please.go

View workflow job for this annotation

GitHub Actions / lint

only one cuddle assignment allowed before go statement (wsl)

err = cmd.Start()
if err != nil {
err2 := updateStatus(StateError, "")
Expand All @@ -169,6 +175,8 @@

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

Check failure on line 178 in command/please.go

View workflow job for this annotation

GitHub Actions / lint

only cuddled expressions if assigning variable or using from line above (wsl)

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)
}
}
}
Loading