Skip to content

Commit

Permalink
some log improvements - the name of the backend that produced an erro…
Browse files Browse the repository at this point in the history
…r during the template processing is now logged
  • Loading branch information
HeavyHorst committed Oct 30, 2016
1 parent 4bd1e75 commit f955855
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 27 deletions.
11 changes: 11 additions & 0 deletions backends/error/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,16 @@ package error

import "errors"

// BackendError contains an error message and the name of the backend that produced the error
type BackendError struct {
Backend string
Message string
}

// Error is for the error interface
func (e BackendError) Error() string {
return e.Message
}

// ErrNilConfig is returned if Connect is called on a nil Config
var ErrNilConfig = errors.New("config is nil")
4 changes: 4 additions & 0 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ func SetFormatter(format string) {
}
}

// some log middleware handlers

//withSource adds the field source to the logs
//example: source=resource.go:310
func withSource(l *log.Entry) *log.Entry {
_, file, line, ok := runtime.Caller(2)
if !ok {
Expand Down
56 changes: 29 additions & 27 deletions template/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"github.com/HeavyHorst/easyKV"
"github.com/HeavyHorst/memkv"
berr "github.com/HeavyHorst/remco/backends/error"
"github.com/HeavyHorst/remco/log"
"github.com/HeavyHorst/remco/template/fileutil"
"github.com/Sirupsen/logrus"
Expand Down Expand Up @@ -263,31 +264,19 @@ func (t *Resource) setFileMode() error {
// from the store, then we stage a candidate configuration file, and finally sync
// things up.
// It returns an error if any.
func (t *Resource) process(storeClient Backend) error {
func (t *Resource) process(storeClients []Backend) error {
if err := t.setFileMode(); err != nil {
return err
}
if err := t.setVars(storeClient); err != nil {
return err
}
if err := t.createStageFileAndSync(); err != nil {
return err
}
return nil
}

func (t *Resource) processAll() error {
//load all KV-pairs the first time
var err error
if err = t.setFileMode(); err != nil {
return err
}
for _, storeClient := range t.backends {
for _, storeClient := range storeClients {
if err := t.setVars(storeClient); err != nil {
return err
return berr.BackendError{
Message: err.Error(),
Backend: storeClient.Name,
}
}
}
if err = t.createStageFileAndSync(); err != nil {
if err := t.createStageFileAndSync(); err != nil {
return err
}
return nil
Expand All @@ -305,11 +294,12 @@ func (s Backend) watch(ctx context.Context, processChan chan Backend) {
index, err := s.WatchPrefix(s.Prefix, ctx, easyKV.WithKeys(keysPrefix), easyKV.WithWaitIndex(lastIndex))
if err != nil {
if err != easyKV.ErrWatchCanceled {
log.Error(err)
log.WithFields(logrus.Fields{
"backend": s.Name,
}).Error(err)
time.Sleep(2 * time.Second)
}
continue

}
processChan <- s
lastIndex = index
Expand Down Expand Up @@ -338,21 +328,27 @@ func (t *Resource) Monitor(ctx context.Context) {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
done := make(chan struct{})
//stopIntervalWatch := make(chan bool)

processChan := make(chan Backend)
defer close(processChan)

if err := t.processAll(); err != nil {
log.Error(err)
if err := t.process(t.backends); err != nil {
switch err.(type) {
case berr.BackendError:
err := err.(berr.BackendError)
log.WithFields(logrus.Fields{
"backend": err.Backend,
}).Error(err)
default:
log.Error(err)
}
}

for _, sc := range t.backends {
wg.Add(1)
if sc.Watch {
go func(s Backend) {
defer wg.Done()
//t.watch(s, stopChan, processChan)
s.watch(ctx, processChan)
}(sc)
} else {
Expand All @@ -372,8 +368,14 @@ func (t *Resource) Monitor(ctx context.Context) {
for {
select {
case storeClient := <-processChan:
if err := t.process(storeClient); err != nil {
log.Error(err)
if err := t.process([]Backend{storeClient}); err != nil {
if _, ok := err.(berr.BackendError); ok {
log.WithFields(logrus.Fields{
"backend": storeClient.Name,
}).Error(err)
} else {
log.Error(err)
}
}
case <-ctx.Done():
go func() {
Expand Down

0 comments on commit f955855

Please sign in to comment.