mirror of
https://git.rwth-aachen.de/acs/public/villas/web-backend-go/
synced 2025-03-30 00:00:12 +01:00
AMQP: improve error handling and debug output; remove uptime update for now
This commit is contained in:
parent
dc0e0599c6
commit
7be2951675
3 changed files with 22 additions and 29 deletions
|
@ -26,9 +26,9 @@ import (
|
|||
"fmt"
|
||||
"git.rwth-aachen.de/acs/public/villas/web-backend-go/database"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"github.com/streadway/amqp"
|
||||
"log"
|
||||
"math"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -49,8 +49,8 @@ type Action struct {
|
|||
}
|
||||
|
||||
type Update struct {
|
||||
State string `json:"state"`
|
||||
Uptime float64 `json:"uptime"`
|
||||
State string `json:"state"`
|
||||
//Uptime float64 `json:"uptime"`
|
||||
Properties struct {
|
||||
UUID string `json:"uuid"`
|
||||
} `json:"properties"`
|
||||
|
@ -115,6 +115,8 @@ func ConnectAMQP(uri string) error {
|
|||
|
||||
// consuming queue
|
||||
go func() {
|
||||
|
||||
db := database.GetDB()
|
||||
for message := range client.replies {
|
||||
//err = message.Ack(false)
|
||||
//if err != nil {
|
||||
|
@ -122,44 +124,39 @@ func ConnectAMQP(uri string) error {
|
|||
//}
|
||||
|
||||
var payload Update
|
||||
if err := json.Unmarshal(message.Body, &payload); err != nil {
|
||||
panic(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") {
|
||||
err := json.Unmarshal(message.Body, &payload)
|
||||
if err != nil {
|
||||
log.Println("AMQP: Could not unmarshal message to JSON:", string(message.Body), "err: ", err)
|
||||
continue
|
||||
}*/
|
||||
}
|
||||
|
||||
ICUUID := payload.Properties.UUID
|
||||
_, err = uuid.Parse(ICUUID)
|
||||
|
||||
if ICUUID == "" {
|
||||
log.Println("AMQP: Could not extract UUID of IC from content of received message, COMPONENT NOT UPDATED")
|
||||
if err != nil {
|
||||
log.Printf("AMQP: UUID not valid: %v, message ignored: %v \n", ICUUID, string(message.Body))
|
||||
} else {
|
||||
|
||||
var sToBeUpdated database.InfrastructureComponent
|
||||
db := database.GetDB()
|
||||
|
||||
err = db.Where("UUID = ?", ICUUID).Find(sToBeUpdated).Error
|
||||
err = db.Find(&sToBeUpdated, "UUID = ?", ICUUID).Error
|
||||
if err != nil {
|
||||
log.Println("AMQP: Unable to find IC with UUID: ", ICUUID, " DB error message: ", err)
|
||||
continue
|
||||
}
|
||||
var stateUpdateAt = message.Timestamp.UTC()
|
||||
//var stateUpdateAt = message.Timestamp.UTC()
|
||||
err = db.Model(&sToBeUpdated).Updates(map[string]interface{}{
|
||||
//"Host": gjson.Get(content, "host"),
|
||||
//"Type": gjson.Get(content, "model"),
|
||||
"Uptime": math.Round(payload.Uptime),
|
||||
"State": payload.State,
|
||||
"StateUpdateAt": stateUpdateAt.Format(time.RFC1123),
|
||||
//"Uptime": math.Round(payload.Uptime),
|
||||
"State": payload.State,
|
||||
//"StateUpdateAt": stateUpdateAt.Format(time.RFC1123),
|
||||
//"RawProperties": gjson.Get(content, "properties"),
|
||||
}).Error
|
||||
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)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
|
2
go.mod
2
go.mod
|
@ -7,6 +7,7 @@ require (
|
|||
github.com/gin-gonic/gin v1.4.0
|
||||
github.com/go-ini/ini v1.51.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/leodido/go-urn v1.2.0 // indirect
|
||||
github.com/lib/pq v1.1.1
|
||||
|
@ -17,7 +18,6 @@ require (
|
|||
github.com/stretchr/testify v1.4.0
|
||||
github.com/swaggo/gin-swagger v1.2.0
|
||||
github.com/swaggo/swag v1.6.3
|
||||
github.com/tidwall/gjson v1.3.4
|
||||
github.com/zpatrick/go-config v0.0.0-20191104215613-50bc2709703f
|
||||
golang.org/x/crypto v0.0.0-20191112222119-e1110fd1c708
|
||||
gopkg.in/go-playground/validator.v9 v9.30.0
|
||||
|
|
8
go.sum
8
go.sum
|
@ -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/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/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/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
|
||||
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
|
|||
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/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.5-pre h1:jyJKFOSEbdOc2HODrf2qcCkYOdq7zzXqA9bhW5oV4fM=
|
||||
github.com/ugorji/go v1.1.5-pre/go.mod h1:FwP/aQVg39TXzItUBMwnWp9T9gPQnXw4Poh4/oBQZ/0=
|
||||
|
|
Loading…
Add table
Reference in a new issue