From d02de1ca4f1763b1e29d1000f67971847c1c8692 Mon Sep 17 00:00:00 2001 From: Jonas Leder <jonas@jonasled.de> Date: Sun, 26 Jan 2025 16:14:50 +0100 Subject: [PATCH] configure client to use local database connection --- main.go | 12 +++++++----- messageworker/{main.go => client.go} | 17 +++++++++++++---- 2 files changed, 20 insertions(+), 9 deletions(-) rename messageworker/{main.go => client.go} (88%) diff --git a/main.go b/main.go index 1c83f09..c9c67ba 100644 --- a/main.go +++ b/main.go @@ -15,7 +15,6 @@ import ( func main() { log.Init() - queue.Init() if os.Getenv("OUTPUT_DATABSE") != "" { @@ -27,10 +26,13 @@ func main() { log.Log.Info("Starting embedded MQTT server") mqttserver.Start() } - mqttclient.Init() - go messageworker.Run() - for { - time.Sleep(time.Second) // reduce CPU usage by adding a short sleep here instead of a empty for loop + + if os.Getenv("LOGGER_CLIENT") != "false" && os.Getenv("LOGGER_SERVER") != "true" { + mqttclient.Init() + go messageworker.RunClient() + for { + time.Sleep(time.Second) // reduce CPU usage by adding a short sleep here instead of a empty for loop + } } } diff --git a/messageworker/main.go b/messageworker/client.go similarity index 88% rename from messageworker/main.go rename to messageworker/client.go index 3b0cebb..8ebe615 100644 --- a/messageworker/main.go +++ b/messageworker/client.go @@ -2,6 +2,7 @@ package messageworker import ( "encoding/json" + "os" "time" "jonasled.dev/jonasled/ems-esp-logger/database" @@ -12,7 +13,15 @@ import ( "jonasled.dev/jonasled/ems-esp-logger/types" ) -func Run() { +func RunClient() { + if os.Getenv("CLIENT_USE_SERVER") != "true" { + + log.Log.Info("Intialized local client with direct datbase connection") + runLocalDbClient() + } +} + +func runLocalDbClient() { for { taskData, taskId, err := queue.MainQueue.Dequeue() if err != nil { @@ -24,14 +33,14 @@ func Run() { err = json.Unmarshal([]byte(taskData), &task) if err != nil { log.Log.Error("Failed decoding task as JSON: ", err.Error()) - return + continue } var instance tables.Instance err = database.Db.Where("name = ?", task.Instance).First(&instance).Error if err != nil { log.Log.Error("Failed retreiving instance from database, pushing task back to queue: ", err.Error()) queue.MainQueue.Enqueue(taskData, 60) - return + continue } var jsonData map[string]interface{} @@ -39,7 +48,7 @@ func Run() { if err != nil { log.Log.Error("Failed decoding boiler JSON: ", err.Error()) queue.MainQueue.Enqueue(taskData, 60) - return + continue } valuesToInsert := []tables.Value{} for key, value := range jsonData { -- GitLab