Merge pull request #1017 from prometheus/crash-on-web-error

Exit when web server encounters a startup error
This commit is contained in:
Julius Volz 2015-08-21 13:15:06 +02:00
commit a7c248e3b1
2 changed files with 21 additions and 19 deletions

View File

@ -157,6 +157,8 @@ func Main() int {
log.Warn("Received SIGTERM, exiting gracefully...") log.Warn("Received SIGTERM, exiting gracefully...")
case <-webHandler.Quit(): case <-webHandler.Quit():
log.Warn("Received termination request via web service, exiting gracefully...") log.Warn("Received termination request via web service, exiting gracefully...")
case err := <-webHandler.ListenError():
log.Errorln("Error starting web server, exiting gracefully:", err)
} }
log.Info("See you next time!") log.Info("See you next time!")

View File

@ -59,11 +59,12 @@ type Handler struct {
apiLegacy *legacy.API apiLegacy *legacy.API
federation *Federation federation *Federation
router *route.Router router *route.Router
quitCh chan struct{} listenErrCh chan error
reloadCh chan struct{} quitCh chan struct{}
options *Options reloadCh chan struct{}
statusInfo *PrometheusStatus options *Options
statusInfo *PrometheusStatus
muAlerts sync.Mutex muAlerts sync.Mutex
} }
@ -110,11 +111,12 @@ func New(st local.Storage, qe *promql.Engine, rm *rules.Manager, status *Prometh
router := route.New() router := route.New()
h := &Handler{ h := &Handler{
router: router, router: router,
quitCh: make(chan struct{}), listenErrCh: make(chan error),
reloadCh: make(chan struct{}), quitCh: make(chan struct{}),
options: o, reloadCh: make(chan struct{}),
statusInfo: status, options: o,
statusInfo: status,
ruleManager: rm, ruleManager: rm,
queryEngine: qe, queryEngine: qe,
@ -179,11 +181,17 @@ func New(st local.Storage, qe *promql.Engine, rm *rules.Manager, status *Prometh
return h return h
} }
// ListenError returns the receive-only channel that signals errors while starting the web server.
func (h *Handler) ListenError() <-chan error {
return h.listenErrCh
}
// Quit returns the receive-only quit channel. // Quit returns the receive-only quit channel.
func (h *Handler) Quit() <-chan struct{} { func (h *Handler) Quit() <-chan struct{} {
return h.quitCh return h.quitCh
} }
// Reload returns the receive-only channel that signals configuration reload requests.
func (h *Handler) Reload() <-chan struct{} { func (h *Handler) Reload() <-chan struct{} {
return h.reloadCh return h.reloadCh
} }
@ -191,15 +199,7 @@ func (h *Handler) Reload() <-chan struct{} {
// Run serves the HTTP endpoints. // Run serves the HTTP endpoints.
func (h *Handler) Run() { func (h *Handler) Run() {
log.Infof("Listening on %s", h.options.ListenAddress) log.Infof("Listening on %s", h.options.ListenAddress)
h.listenErrCh <- http.ListenAndServe(h.options.ListenAddress, h.router)
// If we cannot bind to a port, retry after 30 seconds.
for {
err := http.ListenAndServe(h.options.ListenAddress, h.router)
if err != nil {
log.Errorf("Could not listen on %s: %s", h.options.ListenAddress, err)
}
time.Sleep(30 * time.Second)
}
} }
func (h *Handler) alerts(w http.ResponseWriter, r *http.Request) { func (h *Handler) alerts(w http.ResponseWriter, r *http.Request) {