fix(pkg/config): use storage endpoints
changes: - Only one storage endpoint can be defined. This consists of a URL which can be used to specify whether the data is to be stored in a file or in a database.
This commit is contained in:
@ -9,11 +9,9 @@ import (
|
||||
|
||||
"github.com/Masterminds/semver"
|
||||
"github.com/go-flucky/flucky/pkg/config"
|
||||
"github.com/go-flucky/flucky/pkg/rgbled"
|
||||
"github.com/go-flucky/flucky/pkg/sensor"
|
||||
"github.com/go-flucky/flucky/pkg/storage"
|
||||
"github.com/go-flucky/flucky/pkg/storage/db"
|
||||
"github.com/go-flucky/flucky/pkg/storage/logfile"
|
||||
"github.com/go-flucky/flucky/pkg/types"
|
||||
"github.com/volker-raschek/go-logger/pkg/logger"
|
||||
)
|
||||
@ -25,12 +23,8 @@ var (
|
||||
postgresUser = "postgres"
|
||||
postgresPassword = "postgres"
|
||||
|
||||
flogger logger.Logger
|
||||
)
|
||||
|
||||
func init() {
|
||||
flogger = logger.NewSilentLogger()
|
||||
}
|
||||
)
|
||||
|
||||
func SetLogger(logger logger.Logger) {
|
||||
flogger = logger
|
||||
@ -39,69 +33,63 @@ func SetLogger(logger logger.Logger) {
|
||||
// Start the daemon
|
||||
func Start(cnf *config.Configuration, cleanCacheInterval time.Duration, compression bool, round float64, version *semver.Version) {
|
||||
|
||||
// Context
|
||||
parentCtx := context.Background()
|
||||
ctx, cancel := context.WithCancel(parentCtx)
|
||||
|
||||
// Ticker
|
||||
// saveTicker := time.Tick(cleanCacheInterval)
|
||||
|
||||
// channels
|
||||
debugChannel := make(chan string, 0)
|
||||
infoChannel := make(chan string, 0)
|
||||
warnChannel := make(chan string, 0)
|
||||
errorChannel := make(chan error, 0)
|
||||
fatalChannel := make(chan error, 1)
|
||||
interruptChannel := make(chan os.Signal, 1)
|
||||
signal.Notify(interruptChannel, os.Interrupt, os.Kill, syscall.SIGTERM)
|
||||
measuredValueChannel := make(chan *types.MeasuredValue, 0)
|
||||
|
||||
// Info
|
||||
flogger.Info("Use clean-cache-interval: %v", cleanCacheInterval.String())
|
||||
flogger.Info("Use compression: %v", compression)
|
||||
flogger.Info("Round: %v", round)
|
||||
|
||||
ticker := time.Tick(cleanCacheInterval)
|
||||
|
||||
interrupt := make(chan os.Signal, 1)
|
||||
signal.Notify(interrupt, os.Interrupt, os.Kill, syscall.SIGTERM)
|
||||
|
||||
errorChannel := make(chan error, 0)
|
||||
measuredValuesChannel := make(chan []*types.MeasuredValue, 0)
|
||||
|
||||
ctx := context.Background()
|
||||
childContext, cancel := context.WithCancel(ctx)
|
||||
|
||||
measuredValuesLogfile := logfile.New(cnf.Logfile)
|
||||
|
||||
measuredValuesCache := make([]*types.MeasuredValue, 0)
|
||||
// measuredValuesLogfile := logfile.New(cnf.Logfile)
|
||||
|
||||
var postgres db.Database
|
||||
if cnf.DatabaseSettings != nil {
|
||||
p, err := db.New(cnf.DatabaseSettings)
|
||||
if err != nil {
|
||||
flogger.Error("%v", err)
|
||||
}
|
||||
if err := p.Schema(ctx, version); err != nil {
|
||||
flogger.Error("%v", err)
|
||||
}
|
||||
postgres = p
|
||||
checkDeviceInDatabase(ctx, cnf.Device, postgres)
|
||||
checkSensorsInDatabase(ctx, cnf.Sensors, postgres)
|
||||
defer postgres.Close()
|
||||
}
|
||||
// Producer
|
||||
go sensor.ReadContinuously(ctx, cnf.GetSensors(config.ENABLED), measuredValueChannel, errorChannel)
|
||||
|
||||
rgbLEDs := cnf.GetRGBLEDs(config.ENABLED)
|
||||
|
||||
go sensor.ReadContinuously(childContext, cnf.GetSensors(config.ENABLED), measuredValuesChannel, errorChannel)
|
||||
// Distributor
|
||||
//measuredValueChannels := distribute.MeasuredValues(ctx, 5, measuredValueChannel)
|
||||
|
||||
for {
|
||||
|
||||
err := rgbled.Run(rgbLEDs)
|
||||
if err != nil {
|
||||
flogger.Error("Can not turn on green info light: %v", err)
|
||||
}
|
||||
|
||||
select {
|
||||
case debug, _ := <-debugChannel:
|
||||
flogger.Debug("%v", debug)
|
||||
case info, _ := <-infoChannel:
|
||||
flogger.Info("%v", info)
|
||||
case warn, _ := <-warnChannel:
|
||||
flogger.Warn("%v", warn)
|
||||
case err, _ := <-errorChannel:
|
||||
|
||||
flogger.Error("%v", err)
|
||||
|
||||
err = rgbled.Error(rgbLEDs)
|
||||
if err != nil {
|
||||
flogger.Error("Can not turn on red info light: %v", err)
|
||||
}
|
||||
|
||||
time.Sleep(time.Second * 2)
|
||||
case fatal, _ := <-fatalChannel:
|
||||
flogger.Fatal("Received a fatal error: %v", fatal)
|
||||
case interrupt := <-interruptChannel:
|
||||
flogger.Info("Received OS Signal: %v", interrupt)
|
||||
flogger.Info("Close context")
|
||||
cancel()
|
||||
flogger.Info("Close channels")
|
||||
close(debugChannel)
|
||||
close(infoChannel)
|
||||
close(warnChannel)
|
||||
close(errorChannel)
|
||||
close(interruptChannel)
|
||||
return
|
||||
|
||||
case <-ticker:
|
||||
err := rgbled.Logfile(rgbLEDs)
|
||||
if err != nil {
|
||||
flogger.Error("Can not turn on blue info light: %v", err)
|
||||
}
|
||||
|
||||
if round != 0 {
|
||||
storage.Round(measuredValuesCache, round)
|
||||
@ -111,46 +99,14 @@ func Start(cnf *config.Configuration, cleanCacheInterval time.Duration, compress
|
||||
measuredValuesCache = storage.Compression(measuredValuesCache)
|
||||
}
|
||||
|
||||
if err := logfile.Append(measuredValuesLogfile, measuredValuesCache); err != nil {
|
||||
err2 := rgbled.Error(rgbLEDs)
|
||||
if err2 != nil {
|
||||
flogger.Error("Can not turn on red info light: %v", err2)
|
||||
}
|
||||
flogger.Error("Can not save caches measured values in logfile: %v", err)
|
||||
}
|
||||
|
||||
if postgres != nil {
|
||||
if err := postgres.InsertMeasuredValues(ctx, measuredValuesCache); err != nil {
|
||||
err2 := rgbled.Error(rgbLEDs)
|
||||
if err2 != nil {
|
||||
flogger.Error("Can not turn on red info light: %v", err)
|
||||
}
|
||||
flogger.Error("Can not save cached measured values in database: %v", err)
|
||||
}
|
||||
}
|
||||
// if err := logfile.Append(measuredValuesLogfile, measuredValuesCache); err != nil {
|
||||
// flogger.Error("Can not save caches measured values in logfile: %v", err)
|
||||
// }
|
||||
|
||||
measuredValuesCache = make([]*types.MeasuredValue, 0)
|
||||
|
||||
case measuredValues, _ := <-measuredValuesChannel:
|
||||
measuredValuesCache = append(measuredValuesCache, measuredValues...)
|
||||
|
||||
case killSignal := <-interrupt:
|
||||
flogger.Warn("Daemon was interruped by system signal %v\n", killSignal)
|
||||
|
||||
cancel()
|
||||
|
||||
err := rgbled.Error(rgbLEDs)
|
||||
if err != nil {
|
||||
flogger.Error("Can not turn on red info light: %v", err)
|
||||
}
|
||||
|
||||
flogger.Warn("Save remaining data from the cache")
|
||||
err = logfile.Append(measuredValuesLogfile, measuredValuesCache)
|
||||
if err != nil {
|
||||
flogger.Fatal("%v", err)
|
||||
}
|
||||
|
||||
return
|
||||
case measuredValue, _ := <-measuredValueChannel:
|
||||
measuredValuesCache = append(measuredValuesCache, measuredValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user