Increment waitgroup before goroutine

This commit is contained in:
Søren Rasmussen 2021-09-30 20:37:10 +02:00
parent 8325f2f3c6
commit bc6f62af23

View file

@ -49,8 +49,7 @@ func bool2ChamberState(state bool) dal.ChamberState {
return dal.ChamberStateIdle
}
func mainLoop(db *dal.DAL) {
wg.Add(1)
func mainLoop(db *dal.DAL, wg *sync.WaitGroup) {
defer wg.Done()
for {
@ -89,13 +88,13 @@ func mainLoop(db *dal.DAL) {
switch reading.Sensor {
case config.Temperature.AmbientSensor:
hysteresis.UpdateAmbientTemperature(reading.Value)
hysteresis.UpdateAmbientTemperature(reading.Degrees())
case config.Temperature.ChamberSensor:
hysteresis.UpdateChamberTemperature(reading.Value)
hysteresis.UpdateChamberTemperature(reading.Degrees())
case config.Temperature.WortSensor:
hysteresis.UpdateWortTemperature(reading.Value)
hysteresis.UpdateWortTemperature(reading.Degrees())
}
dbCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
@ -103,7 +102,7 @@ func mainLoop(db *dal.DAL) {
if err != nil {
log.Error().Err(err).Msg("Failed to start DB transaction")
} else {
dal.SaveTemperatureReading(dbCtx, tx, reading.Sensor, reading.Value)
dal.SaveTemperatureReading(dbCtx, tx, reading.Sensor, reading.MilliDegrees)
err = tx.Commit()
if err != nil {
log.Error().Err(err).Msg("Failed to commit DB transaction")
@ -118,7 +117,7 @@ func main() {
// Sentry
err := sentry.Init(sentry.ClientOptions{
Dsn: "https://7278625538334140991ce433e0ad292f@sentry.joco.dk/24",
//TracesSampleRate: 0.1,
TracesSampleRate: 0.01,
})
if err != nil {
log.Fatal().Err(err).Msg("Failed to initialize Sentry.")
@ -158,8 +157,6 @@ func main() {
reloadConfig()
// Database
log.Info().Str("dsn", config.Db.Dsn).Msg("Initializing DB")
migrator, err := dal.NewMigrator(config.Db.Dsn)
if err != nil {
log.Fatal().Err(err).Msg("Error initializing DB migrations")
@ -205,8 +202,9 @@ func main() {
mux.HandleFunc("/state-changes", api.GetStateChanges)
// Main
wg.Add(2)
go temperature.Serve(ctx, wg)
go mainLoop(db)
go mainLoop(db, wg)
go srv.ListenAndServe()
done := make(chan os.Signal, 1)