Add api/-/reload endpoint #353

This commit is contained in:
zhaozy 2016-06-06 21:42:16 +08:00
parent 7b20b776ea
commit 13fb17be37
2 changed files with 27 additions and 3 deletions

15
api.go
View File

@ -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 (

15
main.go
View File

@ -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...")