Skip to content

Commit

Permalink
Fail when server is unable to bind port (#20409)
Browse files Browse the repository at this point in the history
* Server: Return error when unable to bind port
* Server: Exit if a service fails
* Build: Remove graceful kill from Bra config
  • Loading branch information
aknuds1 authored Nov 18, 2019
1 parent 85b7dde commit 82f4fc2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
2 changes: 0 additions & 2 deletions .bra.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,3 @@ cmds = [
["go", "run", "-mod=vendor", "build.go", "-dev", "build-server"],
["./bin/grafana-server", "-packaging=dev", "cfg:app_mode=development"]
]
interrupt_timout = 5
graceful_kill = true
4 changes: 2 additions & 2 deletions pkg/api/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/grafana/grafana/pkg/services/quota"
"github.com/grafana/grafana/pkg/services/rendering"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/util/errutil"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
macaron "gopkg.in/macaron.v1"
Expand Down Expand Up @@ -93,8 +94,7 @@ func (hs *HTTPServer) Run(ctx context.Context) error {
listenAddr := fmt.Sprintf("%s:%s", setting.HttpAddr, setting.HttpPort)
listener, err := net.Listen("tcp", listenAddr)
if err != nil {
hs.log.Debug("server was shutdown gracefully")
return nil
return errutil.Wrapf(err, "failed to open listener on address %s", listenAddr)
}

hs.log.Info("HTTP Server Listen", "address", listener.Addr().String(), "protocol", setting.Protocol, "subUrl", setting.AppSubUrl, "socket", setting.SocketPath)
Expand Down
13 changes: 8 additions & 5 deletions pkg/cmd/grafana-server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (s *Server) Run() (err error) {
s.log.Info("Initializing " + service.Name)

if err := service.Instance.Init(); err != nil {
return fmt.Errorf("Service init failed: %v", err)
return errutil.Wrapf(err, "Service init failed")
}
}

Expand All @@ -126,18 +126,21 @@ func (s *Server) Run() (err error) {
return nil
}

if err := service.Run(s.context); err != nil {
err := service.Run(s.context)
// Mark that we are in shutdown mode
// So no more services are started
s.shutdownInProgress = true
if err != nil {
if err != context.Canceled {
// Server has crashed.
s.log.Error("Stopped "+descriptor.Name, "reason", err)
} else {
s.log.Info("Stopped "+descriptor.Name, "reason", err)
}

return err
}

// Mark that we are in shutdown mode
// So more services are not started
s.shutdownInProgress = true
return nil
})
}
Expand Down

0 comments on commit 82f4fc2

Please sign in to comment.