fix(pkg/server): logging and save values into postgres

This commit is contained in:
2020-01-18 14:55:43 +01:00
parent 6f45c2957a
commit 671a3eb748
8 changed files with 158 additions and 34 deletions

View File

@ -86,7 +86,7 @@ func PrintSensors(cnf *config.Configuration, w io.Writer) error {
}
if sensor.I2CAddress != nil {
fmt.Fprintf(tw, "%v\t", string(*sensor.I2CAddress))
fmt.Fprintf(tw, "%#v\t", *sensor.I2CAddress)
} else {
fmt.Fprintf(tw, "\t")
}

View File

@ -1,7 +1,7 @@
package daemon
import (
"fmt"
"context"
"net/url"
"sync"
@ -56,7 +56,7 @@ func (cs *cacheStore) WriteToEndpoint() error {
case "file":
return cs.logfile()
case "postgres":
return fmt.Errorf("Not yet implemented to store values into a postgres database")
return cs.postgres()
}
return nil
}
@ -91,3 +91,8 @@ func (cs *cacheStore) logfile() error {
return nil
}
func (cs *cacheStore) postgres() error {
ctx := context.Background()
return storage.Write(ctx, cs.cache, cs.URL)
}

View File

@ -15,9 +15,13 @@ import (
)
var (
flogger = logger.NewDefaultLogger(logger.LogLevelDebug)
flogger = logger.NewSilentLogger()
)
func SetLogger(logger logger.Logger) {
flogger = logger
}
// Start the daemon
func Start(cnf *config.Configuration, cacheSize uint, compression bool, round float64) error {
@ -129,7 +133,10 @@ func Start(cnf *config.Configuration, cacheSize uint, compression bool, round fl
flogger.Fatal("Received a fatal error: %v", fatal)
case <-interruptChannel:
flogger.Debug("Write %v cached values into storage endpoint", cache.Size())
cache.WriteToEndpoint()
err := cache.WriteToEndpoint()
if err != nil {
flogger.Error("%v", err)
}
flogger.Debug("Close context")
cancel()
flogger.Debug("Close channels")