diff --git a/goji.go b/goji.go index 80d3ce9..b68b93f 100644 --- a/goji.go +++ b/goji.go @@ -59,6 +59,7 @@ func Serve() { listener := bind.Default() log.Println("Starting Goji on", listener.Addr()) + graceful.HandleSignals() bind.Ready() err := graceful.Serve(listener, http.DefaultServeMux) diff --git a/graceful/einhorn.go b/graceful/einhorn.go index 8803ad9..082d1c4 100644 --- a/graceful/einhorn.go +++ b/graceful/einhorn.go @@ -3,7 +3,6 @@ package graceful import ( - "log" "os" "strconv" "syscall" @@ -18,7 +17,5 @@ func init() { if err != nil || mpid != os.Getppid() { return } - - log.Print("graceful: Einhorn detected, adding SIGUSR2 handler") - AddSignal(syscall.SIGUSR2) + stdSignals = append(stdSignals, syscall.SIGUSR2) } diff --git a/graceful/signal.go b/graceful/signal.go index c231a80..5c2ab15 100644 --- a/graceful/signal.go +++ b/graceful/signal.go @@ -24,28 +24,28 @@ var hookLock sync.Mutex var prehooks = make([]func(), 0) var posthooks = make([]func(), 0) +var stdSignals = []os.Signal{os.Interrupt} var sigchan = make(chan os.Signal, 1) func init() { - AddSignal(os.Interrupt) go waitForSignal() } +// HandleSignals installs signal handlers for a set of standard signals. By +// default, this set only includes keyboard interrupts, however when the package +// detects that it is running under Einhorn, a SIGUSR2 handler is installed as +// well. +func HandleSignals() { + AddSignal(stdSignals...) +} + // AddSignal adds the given signal to the set of signals that trigger a graceful -// shutdown. Note that for convenience the default interrupt (SIGINT) handler is -// installed at package load time, and unless you call ResetSignals() will be -// listened for in addition to any signals you provide by calling this function. +// shutdown. func AddSignal(sig ...os.Signal) { signal.Notify(sigchan, sig...) } // ResetSignals resets the list of signals that trigger a graceful shutdown. -// Useful if, for instance, you don't want to use the default interrupt (SIGINT) -// handler. Since we necessarily install the SIGINT handler before you have a -// chance to call ResetSignals(), there will be a brief window during which the -// set of signals this package listens for will not be as you intend. Therefore, -// if you intend on using this function, we encourage you to call it as soon as -// possible. func ResetSignals() { signal.Stop(sigchan) }