diff --git a/sighndlr_bsdsol.go b/sighndlr_bsdsol.go index b5542c0..4289d7f 100644 --- a/sighndlr_bsdsol.go +++ b/sighndlr_bsdsol.go @@ -11,7 +11,6 @@ package main import ( - "context" "fmt" "log" "os" @@ -22,13 +21,13 @@ import ( "golang.org/x/sys/unix" ) -//SignalNotifySetup sets up the signals and their channel -func SignalNotifySetup(ch chan os.Signal) { - signal.Notify(ch, os.Interrupt, unix.SIGTERM, unix.SIGHUP, unix.SIGPIPE, unix.SIGINFO) +//signalNotifySetup sets up the signals and their channel +func (ac *AgentController) signalNotifySetup() { + signal.Notify(ac.sig, os.Interrupt, unix.SIGTERM, unix.SIGHUP, unix.SIGPIPE, unix.SIGINFO) } -//HandleSignals handles exiting the program based on different signals -func HandleSignals(cancel context.CancelFunc, ch chan os.Signal) { +//handleSignals handles exiting the program based on different signals +func (ac *AgentController) handleSignals() { const stackTraceBufferSize = 1 * units.MiB //pre-allocate a buffer for stacktrace @@ -36,13 +35,14 @@ func HandleSignals(cancel context.CancelFunc, ch chan os.Signal) { for { select { - case sig := <-ch: + case sig := <-ac.sig: log.Printf("signal %s received\n", sig.String()) switch sig { case os.Interrupt, unix.SIGTERM: - cancel() - log.Println("waiting for final metric flushes... press CTRL+C to exit without flushing") - break + ac.cncl() + return + // log.Println("waiting for final metric flushes... press CTRL+C to exit without flushing") + // break case unix.SIGPIPE, unix.SIGHUP: // Noop case unix.SIGINFO: @@ -51,6 +51,8 @@ func HandleSignals(cancel context.CancelFunc, ch chan os.Signal) { default: log.Printf("signal %s unsupported", sig.String()) } + case <-ac.ctx.Done(): + return } } } diff --git a/sighndlr_windows.go b/sighndlr_windows.go index 6242339..240a54e 100644 --- a/sighndlr_windows.go +++ b/sighndlr_windows.go @@ -9,7 +9,6 @@ package main import ( - "context" "fmt" "log" "os" @@ -20,13 +19,13 @@ import ( "github.com/alecthomas/units" ) -//SignalNotifySetup sets up the signals and their channel -func SignalNotifySetup(ch chan os.Signal) { - signal.Notify(ch, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGPIPE, syscall.SIGTRAP) +//signalNotifySetup sets up the signals and their channel +func (ac *AgentController) signalNotifySetup() { + signal.Notify(ac.sig, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGPIPE, syscall.SIGTRAP) } -//HandleSignals handles exiting the program based on different signals -func HandleSignals(cancel context.CancelFunc, ch chan os.Signal) { +//handleSignals handles exiting the program based on different signals +func (ac *AgentController) handleSignals() { const stackTraceBufferSize = 1 * units.MiB //pre-allocate a buffer for stacktrace @@ -34,13 +33,12 @@ func HandleSignals(cancel context.CancelFunc, ch chan os.Signal) { for { select { - case sig := <-ch: + case sig := <-ac.sig: log.Printf("signal %s received\n", sig.String()) switch sig { case os.Interrupt, syscall.SIGTERM: - cancel() - log.Println("waiting for final metric flushes... press CTRL+C to exit without flushing") - break + ac.cncl() + return case syscall.SIGPIPE, syscall.SIGHUP: // Noop case syscall.SIGTRAP: @@ -49,6 +47,8 @@ func HandleSignals(cancel context.CancelFunc, ch chan os.Signal) { default: log.Printf("signal %s unsupported", sig.String()) } + case <-ac.ctx.Done(): + return } } } diff --git a/signlhndlr_linux.go b/signlhndlr_linux.go index 532449f..a42bf07 100644 --- a/signlhndlr_linux.go +++ b/signlhndlr_linux.go @@ -9,7 +9,6 @@ package main import ( - "context" "fmt" "log" "os" @@ -20,13 +19,13 @@ import ( "golang.org/x/sys/unix" ) -//SignalNotifySetup sets up the signals and their channel -func SignalNotifySetup(ch chan os.Signal) { - signal.Notify(ch, os.Interrupt, unix.SIGTERM, unix.SIGHUP, unix.SIGPIPE, unix.SIGTRAP) +//signalNotifySetup sets up the signals and their channel +func (ac *AgentController) signalNotifySetup() { + signal.Notify(ac.sig, os.Interrupt, unix.SIGTERM, unix.SIGHUP, unix.SIGPIPE, unix.SIGTRAP) } -//HandleSignals handles exiting the program based on different signals -func HandleSignals(cancel context.CancelFunc, ch chan os.Signal) { +//handleSignals handles exiting the program based on different signals +func (ac *AgentController) handleSignals() { const stackTraceBufferSize = 1 * units.MiB //pre-allocate a buffer for stacktrace @@ -34,13 +33,12 @@ func HandleSignals(cancel context.CancelFunc, ch chan os.Signal) { for { select { - case sig := <-ch: + case sig := <-ac.sig: log.Printf("signal %s received\n", sig.String()) switch sig { case os.Interrupt, unix.SIGTERM: - cancel() - log.Println("waiting for final metric flushes... press CTRL+C to exit without flushing") - break + ac.cncl() + return case unix.SIGPIPE, unix.SIGHUP: // Noop case unix.SIGTRAP: @@ -49,6 +47,8 @@ func HandleSignals(cancel context.CancelFunc, ch chan os.Signal) { default: log.Printf("signal %s unsupported", sig.String()) } + case <-ac.ctx.Done(): + return } } }