From 5c1660b3948959a1739064898a86d29b4c9ec441 Mon Sep 17 00:00:00 2001 From: Noah Huppert Date: Fri, 12 Aug 2022 15:19:21 -0400 Subject: [PATCH 1/4] added term-signal flag --- goup.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/goup.go b/goup.go index 2e7b785..46fae6b 100644 --- a/goup.go +++ b/goup.go @@ -5,6 +5,7 @@ package main import ( "bytes" "encoding/json" + "flag" "go/build" "io" "io/ioutil" @@ -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 = flag.String("term-signal", "TERM", "The signal used to terminate the binary between restarts (allowed values: TERM, INT)") + type project struct { Name string Deps []string @@ -36,11 +39,17 @@ type project struct { } func main() { + flag.Parse() + prj, err := read() if err != nil { 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() { @@ -87,11 +96,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" { + return syscall.SIGTERM + } else { + panic("invalid term-signal flag") + } +} + 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) } } From ddbe09e19f4e636534a3a9cf2384b0969ac93811 Mon Sep 17 00:00:00 2001 From: Noah Huppert Date: Fri, 12 Aug 2022 15:35:02 -0400 Subject: [PATCH 2/4] Made configure term signal via env var --- README.md | 2 ++ goup.go | 14 ++++++-------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index a354f22..6aade12 100644 --- a/README.md +++ b/README.md @@ -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`. diff --git a/goup.go b/goup.go index 46fae6b..fd65976 100644 --- a/goup.go +++ b/goup.go @@ -5,7 +5,7 @@ package main import ( "bytes" "encoding/json" - "flag" + "fmt" "go/build" "io" "io/ioutil" @@ -24,7 +24,7 @@ var logger = log.New(os.Stderr, "goup -> ", 0) var watchOps = []fsnotify.Op{fsnotify.Write, fsnotify.Create, fsnotify.Remove} var watchExt = []string{".go"} -var termSignal = flag.String("term-signal", "TERM", "The signal used to terminate the binary between restarts (allowed values: TERM, INT)") +var termSignal = os.Getenv("GOUP_TERM_SIGNAL") type project struct { Name string @@ -39,8 +39,6 @@ type project struct { } func main() { - flag.Parse() - prj, err := read() if err != nil { logger.Fatalf("failed import: %v", err) @@ -48,7 +46,7 @@ func main() { // Ensure term-signal flag is valid getTermSignal() - logger.Printf("using termination signal: %s", *termSignal) + logger.Printf("using termination signal: %s", termSignal) c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt, syscall.SIGTERM) @@ -98,12 +96,12 @@ func main() { // Get the signal used to terminal signals. func getTermSignal() syscall.Signal { - if *termSignal == "INT" { + if termSignal == "INT" { return syscall.SIGINT - } else if *termSignal == "TERM" { + } else if termSignal == "TERM" || termSignal == "" { return syscall.SIGTERM } else { - panic("invalid term-signal flag") + panic(fmt.Sprintf("invalid GOUP_TERM_SIGNAL value: %s", termSignal)) } } From 911fbde4877dbeb090b33230b92a1f12c218fc7b Mon Sep 17 00:00:00 2001 From: Noah Huppert Date: Fri, 12 Aug 2022 15:46:31 -0400 Subject: [PATCH 3/4] debug --- goup.go | 1 + 1 file changed, 1 insertion(+) diff --git a/goup.go b/goup.go index fd65976..2ceec38 100644 --- a/goup.go +++ b/goup.go @@ -39,6 +39,7 @@ type project struct { } func main() { + panic(os.Getenv("GOUP_TERM_SIGNAL")) prj, err := read() if err != nil { logger.Fatalf("failed import: %v", err) From 02d17367db96b19513eaa316e7f7007f639b955a Mon Sep 17 00:00:00 2001 From: Noah Huppert Date: Fri, 12 Aug 2022 15:48:11 -0400 Subject: [PATCH 4/4] rm dev --- goup.go | 1 - 1 file changed, 1 deletion(-) diff --git a/goup.go b/goup.go index 2ceec38..fd65976 100644 --- a/goup.go +++ b/goup.go @@ -39,7 +39,6 @@ type project struct { } func main() { - panic(os.Getenv("GOUP_TERM_SIGNAL")) prj, err := read() if err != nil { logger.Fatalf("failed import: %v", err)