diff options
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 } |