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

Allow configuring of the termination signal #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ Main features:
failed state.
- is about **200 loc** simple to copy and adapt to your custom needs.

Configuration:
- By default applications are killed using the `TERM` signal. This can be configured to the `INT` signal by setting the `GOUP_TERM_SIGNAL` environment variable to either `TERM` or `INT`.
20 changes: 19 additions & 1 deletion goup.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package main
import (
"bytes"
"encoding/json"
"fmt"
"go/build"
"io"
"io/ioutil"
Expand All @@ -23,6 +24,8 @@ var logger = log.New(os.Stderr, "goup -> ", 0)
var watchOps = []fsnotify.Op{fsnotify.Write, fsnotify.Create, fsnotify.Remove}
var watchExt = []string{".go"}

var termSignal = os.Getenv("GOUP_TERM_SIGNAL")

type project struct {
Name string
Deps []string
Expand All @@ -41,6 +44,10 @@ func main() {
logger.Fatalf("failed import: %v", err)
}

// Ensure term-signal flag is valid
getTermSignal()
logger.Printf("using termination signal: %s", termSignal)

c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
Expand Down Expand Up @@ -87,11 +94,22 @@ func main() {
<-done
}

// Get the signal used to terminal signals.
func getTermSignal() syscall.Signal {
if termSignal == "INT" {
return syscall.SIGINT
} else if termSignal == "TERM" || termSignal == "" {
return syscall.SIGTERM
} else {
panic(fmt.Sprintf("invalid GOUP_TERM_SIGNAL value: %s", termSignal))
}
}

func (p *project) terminate() {
// send termination signal, to running application
if p.cmd != nil && p.cmd.Process != nil {
logger.Println("terminating process:", p.cmd.Process.Pid)
if err := p.cmd.Process.Signal(syscall.SIGTERM); err != nil {
if err := p.cmd.Process.Signal(getTermSignal()); err != nil {
logger.Println("failed to terminate:", p.cmd.Process.Pid, "reason:", err)
}
}
Expand Down