Browse Source

added request ack

0xdcarns 2 years ago
parent
commit
a1f5d73a58
3 changed files with 16 additions and 4 deletions
  1. 7 0
      controllers/hosts.go
  2. 2 0
      models/host.go
  3. 7 4
      mq/handlers.go

+ 7 - 0
controllers/hosts.go

@@ -13,6 +13,7 @@ import (
 	"github.com/gravitl/netmaker/logic/hostactions"
 	"github.com/gravitl/netmaker/models"
 	"github.com/gravitl/netmaker/mq"
+	"github.com/gravitl/netmaker/servercfg"
 	"golang.org/x/crypto/bcrypt"
 )
 
@@ -236,6 +237,12 @@ func addHostToNetwork(w http.ResponseWriter, r *http.Request) {
 		Host:   *currHost,
 		Node:   *newNode,
 	})
+	if servercfg.IsMessageQueueBackend() {
+		mq.HostUpdate(&models.HostUpdate{
+			Action: models.RequestAck,
+			Host:   *currHost,
+		})
+	}
 
 	logger.Log(2, r.Header.Get("user"), fmt.Sprintf("added host %s to network %s", currHost.Name, network))
 	w.WriteHeader(http.StatusOK)

+ 2 - 0
models/host.go

@@ -76,6 +76,8 @@ const (
 	JoinHostToNetwork = "JOIN_HOST_TO_NETWORK"
 	// Acknowledgement - ACK response for hosts
 	Acknowledgement = "ACK"
+	// RequestAck - request an ACK
+	RequestAck = "REQ_ACK"
 )
 
 // HostUpdate - struct for host update

+ 7 - 4
mq/handlers.go

@@ -147,11 +147,13 @@ func UpdateHost(client mqtt.Client, msg mqtt.Message) {
 		switch hostUpdate.Action {
 		case models.Acknowledgement:
 			hu := hostactions.GetAction(currentHost.ID.String())
-			if err = HostUpdate(hu); err != nil {
-				logger.Log(0, "failed to send new node to host", hostUpdate.Host.Name, currentHost.ID.String(), err.Error())
-				return
+			if hu != nil {
+				if err = HostUpdate(hu); err != nil {
+					logger.Log(0, "failed to send new node to host", hostUpdate.Host.Name, currentHost.ID.String(), err.Error())
+					return
+				}
+				sendPeerUpdate = true
 			}
-			sendPeerUpdate = true
 		case models.UpdateHost:
 			sendPeerUpdate = logic.UpdateHostFromClient(&hostUpdate.Host, currentHost)
 			err := logic.UpsertHost(currentHost)
@@ -170,6 +172,7 @@ func UpdateHost(client mqtt.Client, msg mqtt.Message) {
 			}
 			sendPeerUpdate = true
 		}
+
 		if sendPeerUpdate {
 			err := PublishPeerUpdate()
 			if err != nil {