From 6ef8cde22595ad1addb17f0bc7d8abb935d83d72 Mon Sep 17 00:00:00 2001 From: mr Date: Wed, 21 Aug 2024 15:46:16 +0200 Subject: [PATCH] Booking trace API --- models/peer/peer_cache.go | 14 +++++++++++++- tools/api.go | 21 +++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/models/peer/peer_cache.go b/models/peer/peer_cache.go index 8c1d30c..ef4c664 100644 --- a/models/peer/peer_cache.go +++ b/models/peer/peer_cache.go @@ -33,7 +33,19 @@ type PeerCache struct { } func (p *PeerCache) checkPeerStatus(peerID string) bool { - return true + api := tools.API{} + access := (&Peer{}).GetAccessor(nil) + res, code, _ := access.LoadOne(peerID) + if code != 200 { + return false + } + url := res.(*Peer).Url + if strings.Contains(url, "localhost") || strings.Contains(url, "127.0.0.1") { + url = strings.ReplaceAll(url, "localhost", utils.PEER.API()) + + } + state := api.CheckRemotePeer(url + ":8093") + return state != tools.DEAD } func (p *PeerCache) GetAccessor(caller *tools.HTTPCaller) utils.Accessor { diff --git a/tools/api.go b/tools/api.go index a83ee8f..354926a 100644 --- a/tools/api.go +++ b/tools/api.go @@ -20,6 +20,15 @@ const ( DEAD ) +func ToState(str string) State { + for _, s := range []State{ALIVE, REDUCED_SERVICE, UNPROCESSABLE_ENTITY, DB_FALLOUT, TEAPOT, DEAD} { + if s.String() == str { + return s + } + } + return DEAD +} + func (s State) String() string { return [...]string{"alive", "reduced service", "unprocessable entity", "database fallout", "some things boils in here, i'm probably a teapot", "dead"}[s] @@ -47,6 +56,18 @@ func (a *API) GetState() (State, int, error) { return ALIVE, 200, nil } +func (a *API) CheckRemotePeer(url string) State { + // Check if the database is up + caller := NewHTTPCaller(map[string]map[METHOD]string{}) + var resp APIStatusResponse + b, err := caller.CallGet(url, "/status") + if err != nil { + return DEAD + } + json.Unmarshal(b, &resp) + return ToState(resp.Data.State) +} + func (a *API) CheckRemoteAPIs(urls []string) (State, int, error) { // Check if the database is up caller := NewHTTPCaller(map[string]map[METHOD]string{})