diff options
author | Valery Piashchynski <[email protected]> | 2020-03-03 18:57:52 +0300 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2020-03-03 18:57:52 +0300 |
commit | aa0ba5496c101e41a497285b4e0f4e9f2820b5e7 (patch) | |
tree | dadbacfbbcd99e2306fa0996393b31106a0542bb /cmd | |
parent | 9fabf648f1c3cb797ec03377c3e2182397fb7a1a (diff) |
Fix typos
Update signals handling mechanism
http proper stopping
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/rr/cmd/serve.go | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/cmd/rr/cmd/serve.go b/cmd/rr/cmd/serve.go index 1e8f53b2..e472767c 100644 --- a/cmd/rr/cmd/serve.go +++ b/cmd/rr/cmd/serve.go @@ -24,34 +24,40 @@ import ( "github.com/spf13/cobra" "os" "os/signal" + "sync" "syscall" ) -var stopSignal = make(chan os.Signal, 1) - func init() { CLI.AddCommand(&cobra.Command{ Use: "serve", Short: "Serve RoadRunner service(s)", RunE: serveHandler, }) - - signal.Notify(stopSignal, os.Interrupt, syscall.SIGTERM, syscall.SIGINT) } func serveHandler(cmd *cobra.Command, args []string) error { - stopped := make(chan interface{}) + // https://golang.org/pkg/os/signal/#Notify + // should be of buffer size at least 1 + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGINT) + + wg := &sync.WaitGroup{} go func() { - <-stopSignal + wg.Add(1) + defer wg.Done() + // get the signal + <-c Container.Stop() - close(stopped) }() + // blocking operation if err := Container.Serve(); err != nil { return err } - <-stopped + wg.Wait() + return nil } |