Commit 7b911aea by Torkel Ödegaard

fix(shutdown flow): improved shutdown flow and log closing, listing to kill and…

fix(shutdown flow): improved shutdown flow and log closing, listing to kill and and SIGTERM as well, closes #2516
parent af95daad
...@@ -8,6 +8,7 @@ import ( ...@@ -8,6 +8,7 @@ import (
"path/filepath" "path/filepath"
"runtime" "runtime"
"strconv" "strconv"
"syscall"
"time" "time"
"github.com/grafana/grafana/pkg/cmd" "github.com/grafana/grafana/pkg/cmd"
...@@ -30,6 +31,7 @@ var buildstamp string ...@@ -30,6 +31,7 @@ var buildstamp string
var configFile = flag.String("config", "", "path to config file") var configFile = flag.String("config", "", "path to config file")
var homePath = flag.String("homepath", "", "path to grafana install/home path, defaults to working directory") var homePath = flag.String("homepath", "", "path to grafana install/home path, defaults to working directory")
var pidFile = flag.String("pidfile", "", "path to pid file") var pidFile = flag.String("pidfile", "", "path to pid file")
var exitChan = make(chan int)
func init() { func init() {
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())
...@@ -42,15 +44,9 @@ func main() { ...@@ -42,15 +44,9 @@ func main() {
setting.BuildCommit = commit setting.BuildCommit = commit
setting.BuildStamp = buildstampInt64 setting.BuildStamp = buildstampInt64
go func() { go listenToSystemSignels()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
os.Exit(0)
}()
flag.Parse() flag.Parse()
writePIDFile() writePIDFile()
initRuntime() initRuntime()
...@@ -69,8 +65,7 @@ func main() { ...@@ -69,8 +65,7 @@ func main() {
} }
cmd.StartServer() cmd.StartServer()
exitChan <- 0
log.Close()
} }
func initRuntime() { func initRuntime() {
...@@ -105,3 +100,27 @@ func writePIDFile() { ...@@ -105,3 +100,27 @@ func writePIDFile() {
log.Fatal(3, "Failed to write pidfile", err) log.Fatal(3, "Failed to write pidfile", err)
} }
} }
func listenToSystemSignels() {
signalChan := make(chan os.Signal, 1)
code := 0
signal.Notify(signalChan, os.Interrupt)
signal.Notify(signalChan, os.Kill)
signal.Notify(signalChan, syscall.SIGTERM)
select {
case sig := <-signalChan:
log.Info("Received signal %s. shutting down", sig)
case code = <-exitChan:
switch code {
case 0:
log.Info("Shutting down")
default:
log.Warn("Shutting down")
}
}
log.Close()
os.Exit(code)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment