Skip to content

Commit

Permalink
add peerUpdate when calling hostupdate api (#3005)
Browse files Browse the repository at this point in the history
  • Loading branch information
yabinma authored Jul 17, 2024
1 parent 023956d commit 3dce62a
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions controllers/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ func hostUpdateFallback(w http.ResponseWriter, r *http.Request) {
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
return
}

var sendPeerUpdate bool
var replacePeers bool
var hostUpdate models.HostUpdate
err = json.NewDecoder(r.Body).Decode(&hostUpdate)
if err != nil {
Expand All @@ -244,22 +245,32 @@ func hostUpdateFallback(w http.ResponseWriter, r *http.Request) {
slog.Info("recieved host update", "name", hostUpdate.Host.Name, "id", hostUpdate.Host.ID)
switch hostUpdate.Action {
case models.CheckIn:
_ = mq.HandleHostCheckin(&hostUpdate.Host, currentHost)
sendPeerUpdate = mq.HandleHostCheckin(&hostUpdate.Host, currentHost)

case models.UpdateHost:

_ = logic.UpdateHostFromClient(&hostUpdate.Host, currentHost)
if hostUpdate.Host.PublicKey != currentHost.PublicKey {
//remove old peer entry
replacePeers = true
}
sendPeerUpdate = logic.UpdateHostFromClient(&hostUpdate.Host, currentHost)
err := logic.UpsertHost(currentHost)
if err != nil {
slog.Error("failed to update host", "id", currentHost.ID, "error", err)
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
return
}

case models.UpdateMetrics:
mq.UpdateMetricsFallBack(hostUpdate.Node.ID.String(), hostUpdate.NewMetrics)
}
logic.ReturnSuccessResponse(w, r, "updated host data")

if sendPeerUpdate {
err := mq.PublishPeerUpdate(replacePeers)
if err != nil {
slog.Error("failed to publish peer update", "error", err)
}
}
logic.ReturnSuccessResponse(w, r, "updated host data")
}

// swagger:route DELETE /api/hosts/{hostid} hosts deleteHost
Expand Down

0 comments on commit 3dce62a

Please sign in to comment.