diff --git a/pkg/api/http_server.go b/pkg/api/http_server.go index fa27eabbf24..38858606579 100644 --- a/pkg/api/http_server.go +++ b/pkg/api/http_server.go @@ -60,6 +60,16 @@ func (hs *HTTPServer) Start(ctx context.Context) error { hs.log.Info("Initializing HTTP Server", "address", listenAddr, "protocol", setting.Protocol, "subUrl", setting.AppSubUrl, "socket", setting.SocketPath) hs.httpSrv = &http.Server{Addr: listenAddr, Handler: hs.macaron} + + // handle http shutdown on server context done + go func() { + <-ctx.Done() + if err := hs.httpSrv.Shutdown(context.Background()); err != nil { + hs.log.Error("Failed to shutdown server", "error", err) + } + hs.log.Info("Stopped HTTP Server") + }() + switch setting.Protocol { case setting.HTTP: err = hs.httpSrv.ListenAndServe() diff --git a/pkg/cmd/grafana-server/server.go b/pkg/cmd/grafana-server/server.go index 911cf092b47..71a1560215f 100644 --- a/pkg/cmd/grafana-server/server.go +++ b/pkg/cmd/grafana-server/server.go @@ -92,6 +92,8 @@ func (g *GrafanaServerImpl) Start() error { serviceGraph.Provide(&inject.Object{Value: dashboards.NewProvisioningService()}) serviceGraph.Provide(&inject.Object{Value: api.NewRouteRegister(middleware.RequestMetrics, middleware.RequestTracing)}) serviceGraph.Provide(&inject.Object{Value: api.HTTPServer{}}) + + // self registered services services := registry.GetServices() // Add all services to dependency graph @@ -172,15 +174,12 @@ func (g *GrafanaServerImpl) startHttpServer() error { func (g *GrafanaServerImpl) Shutdown(code int, reason string) { g.log.Info("Shutdown started", "code", code, "reason", reason) - err := g.HttpServer.Shutdown(g.context) - if err != nil { - g.log.Error("Failed to shutdown server", "error", err) - } - + // call cancel func on root context g.shutdownFn() - err = g.childRoutines.Wait() - if err != nil && err != context.Canceled { - g.log.Error("Server shutdown completed with an error", "error", err) + + // wait for chid routines + if err := g.childRoutines.Wait(); err != nil && err != context.Canceled { + g.log.Error("Server shutdown completed", "error", err) } }