fix(pkg/sensor): use context to terminate go routines
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
package daemon
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
@ -9,6 +10,8 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/go-flucky/flucky/pkg/config"
|
||||
"github.com/go-flucky/flucky/pkg/internal/collect"
|
||||
"github.com/go-flucky/flucky/pkg/internal/prittyprint"
|
||||
"github.com/go-flucky/flucky/pkg/logfile"
|
||||
"github.com/go-flucky/flucky/pkg/rgbled"
|
||||
"github.com/go-flucky/flucky/pkg/sensor"
|
||||
@ -16,7 +19,7 @@ import (
|
||||
)
|
||||
|
||||
// Start the daemon
|
||||
func Start(cnf *config.Configuration, cleanCacheIntervall time.Duration, compression bool) error {
|
||||
func Start(cnf *config.Configuration, cleanCacheIntervall time.Duration, compression bool, round float64) error {
|
||||
|
||||
ticker := time.Tick(cleanCacheIntervall)
|
||||
|
||||
@ -27,14 +30,18 @@ func Start(cnf *config.Configuration, cleanCacheIntervall time.Duration, compres
|
||||
//humidityChannel := make(chan *types.Humidity, 0)
|
||||
temperatureChannel := make(chan *types.Temperature, 0)
|
||||
|
||||
ctx := context.Background()
|
||||
childContext, cancel := context.WithCancel(ctx)
|
||||
|
||||
// go sensor.ReadHumiditiesContinuously(cnf.GetHumiditySensors(config.ENABLED), humidityChannel, errorChannel)
|
||||
go sensor.ReadTemperaturesContinuously(cnf.GetTemperatureSensors(config.ENABLED), temperatureChannel, errorChannel)
|
||||
go sensor.ReadTemperaturesContinuously(childContext, cnf.GetTemperatureSensors(config.ENABLED), temperatureChannel, errorChannel)
|
||||
|
||||
temperatures := make([]*types.Temperature, 0)
|
||||
|
||||
rgbLEDs := cnf.GetRGBLEDs(config.ENABLED)
|
||||
err := rgbled.Green(rgbLEDs)
|
||||
if err != nil {
|
||||
cancel()
|
||||
return fmt.Errorf("Can not turn on blue info light: %v", err)
|
||||
}
|
||||
|
||||
@ -43,43 +50,50 @@ func Start(cnf *config.Configuration, cleanCacheIntervall time.Duration, compres
|
||||
case <-ticker:
|
||||
err := rgbled.Blue(rgbLEDs)
|
||||
if err != nil {
|
||||
cancel()
|
||||
return fmt.Errorf("Can not turn on yellow info light: %v", err)
|
||||
}
|
||||
|
||||
log.Printf("Write measured values into logfile")
|
||||
|
||||
log.Printf("%v new measured temperature values", len(temperatures))
|
||||
err = logfile.WriteTemperatures(temperatures, cnf.Device.TemperatureLogfile, compression)
|
||||
if err != nil {
|
||||
cancel()
|
||||
return fmt.Errorf("Can not save temperatures: %v", err)
|
||||
}
|
||||
temperatures = make([]*types.Temperature, 0)
|
||||
|
||||
err = rgbled.Green(rgbLEDs)
|
||||
if err != nil {
|
||||
cancel()
|
||||
return fmt.Errorf("Can not turn on green info light: %v", err)
|
||||
}
|
||||
|
||||
case temperature, more := <-temperatureChannel:
|
||||
if more {
|
||||
temperatures = append(temperatures, temperature)
|
||||
} else {
|
||||
log.Printf("Temperature Channel closed. Write remaining values into the logfile")
|
||||
err := logfile.WriteTemperatures(temperatures, cnf.Device.TemperatureLogfile, compression)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Can not save temperatures: %v", err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
case killSignal := <-interrupt:
|
||||
log.Printf("Daemon was interruped by system signal %v\n", killSignal)
|
||||
|
||||
cancel()
|
||||
|
||||
err := rgbled.Red(rgbLEDs)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Can not turn on info light: %v", err)
|
||||
}
|
||||
|
||||
//close(humidityChannel)
|
||||
close(temperatureChannel)
|
||||
return fmt.Errorf("Daemon was interruped by system signal %v", killSignal)
|
||||
errors := collect.Errors(errorChannel)
|
||||
if len(errors) > 0 {
|
||||
log.Println(prittyprint.FormatErrors(errors))
|
||||
}
|
||||
|
||||
err = logfile.WriteTemperatures(temperatures, cnf.Device.TemperatureLogfile, compression)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Can not save temperatures: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user