Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
Use a general signals handler
Browse files Browse the repository at this point in the history
  • Loading branch information
inercia committed May 6, 2015
1 parent 5603867 commit dbc4b68
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 25 deletions.
38 changes: 38 additions & 0 deletions common/signals.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package common

import (
"os"
"os/signal"
"runtime"
"syscall"
)

// A subsystem/server/... that can be stopped or queried about the status with a signal
type SignalsReceiver interface {
Status() string
Stop() error
}

func SignalHandlerLoop(ss ...SignalsReceiver) {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGUSR1)
buf := make([]byte, 1<<20)
for {
sig := <-sigs
switch sig {
case syscall.SIGINT:
Info.Printf("=== received SIGINT ===\n*** exiting\n")
for _, subsystem := range ss {
subsystem.Stop()
}
os.Exit(0)
case syscall.SIGQUIT:
stacklen := runtime.Stack(buf, true)
Info.Printf("=== received SIGQUIT ===\n*** goroutine dump...\n%s\n*** end\n", buf[:stacklen])
case syscall.SIGUSR1:
for _, subsystem := range ss {
Info.Printf("=== received SIGUSR1 ===\n*** status...\n%s\n*** end\n", subsystem.Status())
}
}
}
}
28 changes: 3 additions & 25 deletions weavedns/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import (
weavenet "github.com/weaveworks/weave/net"
"net"
"os"
"os/signal"
"runtime"
"syscall"
)

var version = "(unreleased version)"
Expand Down Expand Up @@ -90,30 +87,11 @@ func main() {
}
Info.Println("Upstream", srv.Upstream)

Debug.Printf("Starting the signals handler")
go handleSignals(srv)

go SignalHandlerLoop(srv)
go weavedns.ListenHTTP(version, srv, domain, zone, httpPort)

err = srv.Start()
if err != nil {
Error.Fatal("Failed to start the WeaveDNS server", err)
}
}

func handleSignals(s *weavedns.DNSServer) {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGQUIT)
buf := make([]byte, 1<<20)
for {
sig := <-sigs
switch sig {
case syscall.SIGINT:
Info.Printf("=== received SIGINT ===\n*** exiting\n")
s.Stop()
os.Exit(0)
case syscall.SIGQUIT:
stacklen := runtime.Stack(buf, true)
Info.Printf("=== received SIGQUIT ===\n*** goroutine dump...\n%s\n*** end\n", buf[:stacklen])
}
Error.Fatal("[main] Failed to start the WeaveDNS server: ", err)
}
}

0 comments on commit dbc4b68

Please sign in to comment.