Move NewSources input prep to server

Signed-off-by: Tim Raymond <tim@timraymond.com>
pull/1695/head
Jared Scheib 2017-07-06 14:12:07 -07:00 committed by Tim Raymond
parent b30fd6e260
commit 1c72fd338b
2 changed files with 31 additions and 23 deletions

View File

@ -626,22 +626,14 @@ type LayoutStore interface {
Update(context.Context, Layout) error
}
// SourceAndKapacitor is used to parse any NewSources server flag arguments
type SourceAndKapacitor struct {
Source Source `json:"influxdb"`
Kapacitor Server `json:"kapacitor"`
}
// NewSources adds sources to BoltDb idempotently by name, as well as respective kapacitors
func NewSources(ctx context.Context, sourcesStore SourcesStore, serversStore ServersStore, newSources string, logger Logger) error {
if newSources == "" {
return nil
}
type SourceAndKapacitor struct {
Source Source `json:"influxdb"`
Kapacitor Server `json:"kapacitor"`
}
var srcsKaps []SourceAndKapacitor
// On JSON unmarshal error, continue server process without new source and write error to log
if err := json.Unmarshal([]byte(newSources), &srcsKaps); err != nil {
return err
}
func NewSources(ctx context.Context, sourcesStore SourcesStore, serversStore ServersStore, srcsKaps []SourceAndKapacitor, logger Logger) error {
srcs, err := sourcesStore.All(ctx)
if err != nil {
return err

View File

@ -3,6 +3,7 @@ package server
import (
"context"
"crypto/tls"
"encoding/json"
"log"
"math/rand"
"net"
@ -300,14 +301,29 @@ func (s *Server) Serve(ctx context.Context) error {
}
service := openService(ctx, s.BoltPath, layoutBuilder, sourcesBuilder, kapacitorBuilder, logger, s.useAuth())
// Add any new sources and kapacitors as specified via server flag
if err = chronograf.NewSources(ctx, service.SourcesStore, service.ServersStore, s.NewSources, logger); err != nil {
// Continue with server run even if adding NewSources fails
logger.
WithField("component", "server").
WithField("NewSources", "invalid").
Error(err)
}
go func() {
if s.NewSources == "" {
return
}
var srcsKaps []chronograf.SourceAndKapacitor
// On JSON unmarshal error, continue server process without new source and write error to log
if err := json.Unmarshal([]byte(s.NewSources), &srcsKaps); err != nil {
logger.
WithField("component", "server").
WithField("NewSources", "invalid").
Error(err)
}
// Add any new sources and kapacitors as specified via server flag
if err = chronograf.NewSources(ctx, service.SourcesStore, service.ServersStore, srcsKaps, logger); err != nil {
// Continue with server run even if adding NewSources fails
logger.
WithField("component", "server").
WithField("NewSources", "invalid").
Error(err)
}
}()
basepath = s.Basepath
if basepath != "" && s.PrefixRoutes == false {