Merge branch 'weave-common-signals' of https://github.com/inercia/weave into inercia-weave-common-signals

This commit is contained in:
Matthias Radestock committed 2015-05-06 19:33:06 +01:00
commit 8caa58af26
4 files changed
+49 -44

No files matched your search

+38
View File
@@ -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())
}
}
}
}
+5
View File
@@ -94,6 +94,11 @@ func (router *Router) Start() {
}
}
func (router *Router) Stop() error {
// TODO: perform greceful shutdown...
return nil
}
func (router *Router) UsingPassword() bool {
return router.Password != nil
}
+3 -25
View File
@@ -9,9 +9,6 @@ import (
weavenet "github.com/weaveworks/weave/net"
"net"
"os"
"os/signal"
"runtime"
"syscall"
)
var version = "(unreleased version)"
@@ -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)
}
}
+3 -19
View File
@@ -7,16 +7,15 @@ import (
"fmt"
"github.com/davecheney/profile"
"github.com/gorilla/mux"
. "github.com/weaveworks/weave/common"
weavenet "github.com/weaveworks/weave/net"
weave "github.com/weaveworks/weave/router"
"log"
"net"
"net/http"
"os"
"os/signal"
"runtime"
"strings"
"syscall"
)
var version = "(unreleased version)"
@@ -126,7 +125,8 @@ func main() {
if httpAddr != "" {
go handleHTTP(router, httpAddr)
}
handleSignals(router)
SignalHandlerLoop(router)
}
func options() map[string]string {
@@ -209,19 +209,3 @@ func handleHTTP(router *weave.Router, httpAddr string) {
log.Fatal("Unable to create http server", err)
}
}
func handleSignals(router *weave.Router) {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGQUIT, syscall.SIGUSR1)
buf := make([]byte, 1<<20)
for {
sig := <-sigs
switch sig {
case syscall.SIGQUIT:
stacklen := runtime.Stack(buf, true)
log.Printf("=== received SIGQUIT ===\n*** goroutine dump...\n%s\n*** end\n", buf[:stacklen])
case syscall.SIGUSR1:
log.Printf("=== received SIGUSR1 ===\n*** status...\n%s\n*** end\n", router.Status())
}
}
}