Only start the webhook server with at least one webhook input.

This commit is contained in:
Cyril Duez 2016-05-27 10:40:12 +02:00
parent b95244cd65
commit d401b71262
2 changed files with 12 additions and 8 deletions

View File

@ -329,8 +329,9 @@ func (a *Agent) Run(shutdown chan struct{}) error {
metricC := make(chan telegraf.Metric, 10000) metricC := make(chan telegraf.Metric, 10000)
webserver := webserver.NewWebserver(a.Config.Agent.WebhookServiceAddress) webserver := webserver.NewWebserver(a.Config.Agent.WebhookServiceAddress)
// Handle special input plugin: Start any ServiceInput and
// Register any WebhookInput
for _, input := range a.Config.Inputs { for _, input := range a.Config.Inputs {
// Start service of any ServicePlugins
switch p := input.Input.(type) { switch p := input.Input.(type) {
case telegraf.ServiceInput: case telegraf.ServiceInput:
acc := createAccumulatorForInput(a, input, metricC) acc := createAccumulatorForInput(a, input, metricC)
@ -347,9 +348,9 @@ func (a *Agent) Run(shutdown chan struct{}) error {
input.Name, err.Error()) input.Name, err.Error())
return err return err
} }
webserver.StartOnce()
} }
} }
webserver.Start()
// Round collection to nearest interval by sleeping // Round collection to nearest interval by sleeping
if a.Config.Agent.RoundInterval { if a.Config.Agent.RoundInterval {

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"sync"
"github.com/gorilla/mux" "github.com/gorilla/mux"
) )
@ -11,21 +12,23 @@ import (
type Webserver struct { type Webserver struct {
ServiceAddress string ServiceAddress string
Router *mux.Router Router *mux.Router
onceStart sync.Once
} }
func NewWebserver(serviceAddress string) *Webserver { func NewWebserver(serviceAddress string) *Webserver {
return &Webserver{Router: mux.NewRouter(), ServiceAddress: serviceAddress} return &Webserver{Router: mux.NewRouter(), ServiceAddress: serviceAddress}
} }
func (wb *Webserver) Listen() { func (wb *Webserver) listen() {
log.Printf("Started the webhook server on %s\n", wb.ServiceAddress)
err := http.ListenAndServe(fmt.Sprintf("%s", wb.ServiceAddress), wb.Router) err := http.ListenAndServe(fmt.Sprintf("%s", wb.ServiceAddress), wb.Router)
if err != nil { if err != nil {
log.Printf("Error starting server: %v", err) log.Printf("Error starting webhook server: %v", err)
} }
} }
func (wb *Webserver) Start() error { func (wb *Webserver) StartOnce() {
go wb.Listen() wb.onceStart.Do(func() {
log.Printf("Started the webhook server on %s\n", wb.ServiceAddress) go wb.listen()
return nil })
} }