Bläddra i källkod

avoid calling os.Exit outside main.go (#12459)

I don't think we should call os.Exit outside main.go. Calling
os.Exit breaks the application control flow.
Carl Bergquist 7 år sedan
förälder
incheckning
88783d6e0a
2 ändrade filer med 8 tillägg och 5 borttagningar
  1. 6 1
      pkg/cmd/grafana-server/main.go
  2. 2 4
      pkg/cmd/grafana-server/server.go

+ 6 - 1
pkg/cmd/grafana-server/main.go

@@ -14,6 +14,7 @@ import (
 	"net/http"
 	_ "net/http/pprof"
 
+	"github.com/grafana/grafana/pkg/log"
 	"github.com/grafana/grafana/pkg/metrics"
 	"github.com/grafana/grafana/pkg/setting"
 
@@ -87,7 +88,11 @@ func main() {
 
 	err := server.Run()
 
-	server.Exit(err)
+	code := server.Exit(err)
+	trace.Stop()
+	log.Close()
+
+	os.Exit(code)
 }
 
 func listenToSystemSignals(server *GrafanaServerImpl) {

+ 2 - 4
pkg/cmd/grafana-server/server.go

@@ -175,7 +175,7 @@ func (g *GrafanaServerImpl) Shutdown(reason string) {
 	g.childRoutines.Wait()
 }
 
-func (g *GrafanaServerImpl) Exit(reason error) {
+func (g *GrafanaServerImpl) Exit(reason error) int {
 	// default exit code is 1
 	code := 1
 
@@ -185,9 +185,7 @@ func (g *GrafanaServerImpl) Exit(reason error) {
 	}
 
 	g.log.Error("Server shutdown", "reason", reason)
-
-	log.Close()
-	os.Exit(code)
+	return code
 }
 
 func (g *GrafanaServerImpl) writePIDFile() {