Commit 7be29516 authored by Sonja Happ's avatar Sonja Happ
Browse files

AMQP: improve error handling and debug output; remove uptime update for now

parent dc0e0599
...@@ -26,9 +26,9 @@ import ( ...@@ -26,9 +26,9 @@ import (
"fmt" "fmt"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/database" "git.rwth-aachen.de/acs/public/villas/web-backend-go/database"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/streadway/amqp" "github.com/streadway/amqp"
"log" "log"
"math"
"time" "time"
) )
...@@ -49,8 +49,8 @@ type Action struct { ...@@ -49,8 +49,8 @@ type Action struct {
} }
type Update struct { type Update struct {
State string `json:"state"` State string `json:"state"`
Uptime float64 `json:"uptime"` //Uptime float64 `json:"uptime"`
Properties struct { Properties struct {
UUID string `json:"uuid"` UUID string `json:"uuid"`
} `json:"properties"` } `json:"properties"`
...@@ -115,6 +115,8 @@ func ConnectAMQP(uri string) error { ...@@ -115,6 +115,8 @@ func ConnectAMQP(uri string) error {
// consuming queue // consuming queue
go func() { go func() {
db := database.GetDB()
for message := range client.replies { for message := range client.replies {
//err = message.Ack(false) //err = message.Ack(false)
//if err != nil { //if err != nil {
...@@ -122,44 +124,39 @@ func ConnectAMQP(uri string) error { ...@@ -122,44 +124,39 @@ func ConnectAMQP(uri string) error {
//} //}
var payload Update var payload Update
if err := json.Unmarshal(message.Body, &payload); err != nil { err := json.Unmarshal(message.Body, &payload)
panic(err) if err != nil {
} log.Println("AMQP: Could not unmarshal message to JSON:", string(message.Body), "err: ", err)
//content := string(message.Body)
fmt.Println("APQM: message payload", string(message.Body))
// any action message sent by the VILLAScontroller should be ignored by the web backend
/*if strings.Contains(content, "action") {
continue continue
}*/ }
ICUUID := payload.Properties.UUID ICUUID := payload.Properties.UUID
_, err = uuid.Parse(ICUUID)
if ICUUID == "" { if err != nil {
log.Println("AMQP: Could not extract UUID of IC from content of received message, COMPONENT NOT UPDATED") log.Printf("AMQP: UUID not valid: %v, message ignored: %v \n", ICUUID, string(message.Body))
} else { } else {
var sToBeUpdated database.InfrastructureComponent var sToBeUpdated database.InfrastructureComponent
db := database.GetDB() err = db.Find(&sToBeUpdated, "UUID = ?", ICUUID).Error
err = db.Where("UUID = ?", ICUUID).Find(sToBeUpdated).Error
if err != nil { if err != nil {
log.Println("AMQP: Unable to find IC with UUID: ", ICUUID, " DB error message: ", err) log.Println("AMQP: Unable to find IC with UUID: ", ICUUID, " DB error message: ", err)
continue continue
} }
var stateUpdateAt = message.Timestamp.UTC() //var stateUpdateAt = message.Timestamp.UTC()
err = db.Model(&sToBeUpdated).Updates(map[string]interface{}{ err = db.Model(&sToBeUpdated).Updates(map[string]interface{}{
//"Host": gjson.Get(content, "host"), //"Host": gjson.Get(content, "host"),
//"Type": gjson.Get(content, "model"), //"Type": gjson.Get(content, "model"),
"Uptime": math.Round(payload.Uptime), //"Uptime": math.Round(payload.Uptime),
"State": payload.State, "State": payload.State,
"StateUpdateAt": stateUpdateAt.Format(time.RFC1123), //"StateUpdateAt": stateUpdateAt.Format(time.RFC1123),
//"RawProperties": gjson.Get(content, "properties"), //"RawProperties": gjson.Get(content, "properties"),
}).Error }).Error
if err != nil { if err != nil {
log.Println("AMQP: Unable to update IC in DB: ", err) log.Println("AMQP: Unable to update IC", sToBeUpdated.Name, "in DB: ", err)
} }
log.Println("AMQP: Updated IC with UUID ", ICUUID) log.Println("AMQP: Updated IC ", sToBeUpdated.Name)
} }
} }
}() }()
......
...@@ -7,6 +7,7 @@ require ( ...@@ -7,6 +7,7 @@ require (
github.com/gin-gonic/gin v1.4.0 github.com/gin-gonic/gin v1.4.0
github.com/go-ini/ini v1.51.0 // indirect github.com/go-ini/ini v1.51.0 // indirect
github.com/go-playground/universal-translator v0.17.0 // indirect github.com/go-playground/universal-translator v0.17.0 // indirect
github.com/google/uuid v1.1.2
github.com/jinzhu/gorm v1.9.11 github.com/jinzhu/gorm v1.9.11
github.com/leodido/go-urn v1.2.0 // indirect github.com/leodido/go-urn v1.2.0 // indirect
github.com/lib/pq v1.1.1 github.com/lib/pq v1.1.1
...@@ -17,7 +18,6 @@ require ( ...@@ -17,7 +18,6 @@ require (
github.com/stretchr/testify v1.4.0 github.com/stretchr/testify v1.4.0
github.com/swaggo/gin-swagger v1.2.0 github.com/swaggo/gin-swagger v1.2.0
github.com/swaggo/swag v1.6.3 github.com/swaggo/swag v1.6.3
github.com/tidwall/gjson v1.3.4
github.com/zpatrick/go-config v0.0.0-20191104215613-50bc2709703f github.com/zpatrick/go-config v0.0.0-20191104215613-50bc2709703f
golang.org/x/crypto v0.0.0-20191112222119-e1110fd1c708 golang.org/x/crypto v0.0.0-20191112222119-e1110fd1c708
gopkg.in/go-playground/validator.v9 v9.30.0 gopkg.in/go-playground/validator.v9 v9.30.0
......
...@@ -90,6 +90,8 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw ...@@ -90,6 +90,8 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
...@@ -191,12 +193,6 @@ github.com/swaggo/gin-swagger v1.2.0/go.mod h1:qlH2+W7zXGZkczuL+r2nEBR2JTT+/lX05 ...@@ -191,12 +193,6 @@ github.com/swaggo/gin-swagger v1.2.0/go.mod h1:qlH2+W7zXGZkczuL+r2nEBR2JTT+/lX05
github.com/swaggo/swag v1.5.1/go.mod h1:1Bl9F/ZBpVWh22nY0zmYyASPO1lI/zIwRDrpZU+tv8Y= github.com/swaggo/swag v1.5.1/go.mod h1:1Bl9F/ZBpVWh22nY0zmYyASPO1lI/zIwRDrpZU+tv8Y=
github.com/swaggo/swag v1.6.3 h1:N+uVPGP4H2hXoss2pt5dctoSUPKKRInr6qcTMOm0usI= github.com/swaggo/swag v1.6.3 h1:N+uVPGP4H2hXoss2pt5dctoSUPKKRInr6qcTMOm0usI=
github.com/swaggo/swag v1.6.3/go.mod h1:wcc83tB4Mb2aNiL/HP4MFeQdpHUrca+Rp/DRNgWAUio= github.com/swaggo/swag v1.6.3/go.mod h1:wcc83tB4Mb2aNiL/HP4MFeQdpHUrca+Rp/DRNgWAUio=
github.com/tidwall/gjson v1.3.4 h1:On5waDnyKKk3SWE4EthbjjirAWXp43xx5cKCUZY1eZw=
github.com/tidwall/gjson v1.3.4/go.mod h1:P256ACg0Mn+j1RXIDXoss50DeIABTYK1PULOJHhxOls=
github.com/tidwall/match v1.0.1 h1:PnKP62LPNxHKTwvHHZZzdOAOCtsJTjo6dZLCwpKm5xc=
github.com/tidwall/match v1.0.1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E=
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
github.com/ugorji/go v1.1.5-pre h1:jyJKFOSEbdOc2HODrf2qcCkYOdq7zzXqA9bhW5oV4fM= github.com/ugorji/go v1.1.5-pre h1:jyJKFOSEbdOc2HODrf2qcCkYOdq7zzXqA9bhW5oV4fM=
github.com/ugorji/go v1.1.5-pre/go.mod h1:FwP/aQVg39TXzItUBMwnWp9T9gPQnXw4Poh4/oBQZ/0= github.com/ugorji/go v1.1.5-pre/go.mod h1:FwP/aQVg39TXzItUBMwnWp9T9gPQnXw4Poh4/oBQZ/0=
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment