From 5e899d50d3039e56d7ae1df7cdad601289c26762 Mon Sep 17 00:00:00 2001 From: Sonja Happ Date: Tue, 1 Jun 2021 11:33:11 +0200 Subject: [PATCH] send ping to externally managed ICs on startup of backend and upon creation of new external IC to obtain full status update #66 --- .../infrastructure-component/ic_amqpclient.go | 52 ++++++++++++++++--- start.go | 6 +++ 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/routes/infrastructure-component/ic_amqpclient.go b/routes/infrastructure-component/ic_amqpclient.go index 7fb05fe..b7236fb 100644 --- a/routes/infrastructure-component/ic_amqpclient.go +++ b/routes/infrastructure-component/ic_amqpclient.go @@ -196,6 +196,41 @@ func sendActionAMQP(action Action, destinationUUID string) error { } +func SendPing(uuid string) error { + var ping Action + ping.Act = "ping" + + payload, err := json.Marshal(ping) + if err != nil { + return err + } + + msg := amqp.Publishing{ + DeliveryMode: 2, + Timestamp: time.Now(), + ContentType: "application/json", + ContentEncoding: "utf-8", + Priority: 0, + Body: payload, + } + + // set message headers + msg.Headers = make(map[string]interface{}) // empty map + msg.Headers["uuid"] = uuid // leave uuid empty if ping should go to all ICs + + err = CheckConnection() + if err != nil { + return err + } + + err = client.sendCh.Publish(VILLAS_EXCHANGE, + "", + false, + false, + msg) + return err +} + func CheckConnection() error { if client.connection != nil { @@ -235,18 +270,18 @@ func processMessage(message amqp.Delivery) error { return fmt.Errorf("AMQP: Could not unmarshal message to JSON: %v err: %v", string(message.Body), err) } + if payload.Action != "" { + // if a message contains an action, it is not intended for the backend + //log.Println("AMQP: Ignoring action message ", payload) + return nil + } + ICUUID := payload.Properties.UUID _, err = uuid.Parse(ICUUID) if err != nil { return fmt.Errorf("AMQP: UUID not valid: %v, message ignored: %v \n", ICUUID, string(message.Body)) } - if payload.Action != "" { - // if a message contains an action, it is not intended for the backend - log.Println("AMQP: Ignoring action message for action", payload.Action, "sent to IC", ICUUID) - return nil - } - var sToBeUpdated InfrastructureComponent err = sToBeUpdated.byUUID(ICUUID) @@ -318,7 +353,10 @@ func createExternalIC(payload ICUpdate, ICUUID string, body []byte) error { } log.Println("AMQP: Created IC with UUID ", newIC.UUID) - return nil + + // send ping to get full status update of this IC + err = SendPing(ICUUID) + return err } func (s *InfrastructureComponent) updateExternalIC(payload ICUpdate, body []byte) error { diff --git a/start.go b/start.go index 3d5e23f..b86a1bc 100644 --- a/start.go +++ b/start.go @@ -126,6 +126,12 @@ func main() { if err != nil { log.Fatal(err) } + + // send Ping to all externally managed ICs + err = infrastructure_component.SendPing("") + if err != nil { + log.Println("error sending ping action via AMQP: ", err) + } } // Make sure that at least one admin user exists in DB