From 13fb17be37b11026f92fbd088f0cab88f4ca1a62 Mon Sep 17 00:00:00 2001 From: zhaozy Date: Mon, 6 Jun 2016 21:42:16 +0800 Subject: [PATCH] Add api/-/reload endpoint #353 --- api.go | 15 +++++++++++++++ main.go | 15 ++++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/api.go b/api.go index 6a4a7af6..3f2ce2a7 100644 --- a/api.go +++ b/api.go @@ -58,6 +58,7 @@ type API struct { config string resolveTimeout time.Duration uptime time.Time + reloadCh chan struct{} groups func() AlertOverview @@ -74,6 +75,7 @@ func NewAPI(alerts provider.Alerts, silences provider.Silences, gf func() AlertO silences: silences, groups: gf, uptime: time.Now(), + reloadCh: make(chan struct{}), } } @@ -82,6 +84,9 @@ func NewAPI(alerts provider.Alerts, silences provider.Silences, gf func() AlertO func (api *API) Register(r *route.Router) { ihf := prometheus.InstrumentHandlerFunc + // Register reload API for reload configuration. + r.Post("/-/reload", api.reload) + // Register legacy forwarder for alert pushing. r.Post("/alerts", ihf("legacy_add_alerts", api.legacyAddAlerts)) @@ -109,6 +114,11 @@ func (api *API) Update(config string, resolveTimeout time.Duration) { api.resolveTimeout = resolveTimeout } +// Reload returns the receive-only channel that signals configuration reload requests. +func (api *API) Reload() <-chan struct{} { + return api.reloadCh +} + type errorType string const ( @@ -373,6 +383,11 @@ func (api *API) listSilences(w http.ResponseWriter, r *http.Request) { respond(w, sils) } +func (api *API) reload(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "Reloading configuration file...") + api.reloadCh <- struct{}{} +} + type status string const ( diff --git a/main.go b/main.go index 9da54e67..442b8fe4 100644 --- a/main.go +++ b/main.go @@ -194,18 +194,27 @@ func main() { go listen(router) var ( - hup = make(chan os.Signal) - term = make(chan os.Signal) + hup = make(chan os.Signal) + hupReady = make(chan bool) + term = make(chan os.Signal) ) signal.Notify(hup, syscall.SIGHUP) signal.Notify(term, os.Interrupt, syscall.SIGTERM) go func() { - for range hup { + <-hupReady + for { + select { + case <-hup: + case <-api.Reload(): + } reload() } }() + // Wait for reload or termination signals. + close(hupReady) // Unblock SIGHUP handler. + <-term log.Infoln("Received SIGTERM, exiting gracefully...")