Openstatus www.openstatus.dev

🥅 add retry (#502)

authored by

Thibault Le Ouay and committed by
GitHub
6f2de396 704a7d3f

+34 -102
+32 -101
apps/checker/main.go
··· 29 29 func main() { 30 30 r := chi.NewRouter() 31 31 r.Use(middleware.Logger) 32 - r.Post("/", func(w http.ResponseWriter, r *http.Request) { 32 + 33 + // That's the new checker sending to the correct ingest endpoint 34 + r.Post("/checker", func(w http.ResponseWriter, r *http.Request) { 33 35 if r.Header.Get("Authorization") != "Basic "+os.Getenv("CRON_SECRET") { 34 36 http.Error(w, "Unauthorized", 401) 35 37 return ··· 51 53 return 52 54 } 53 55 var u InputData 54 - // region := os.Getenv("FLY_REGION") 56 + region := os.Getenv("FLY_REGION") 55 57 56 58 err = json.NewDecoder(r.Body).Decode(&u) 57 59 ··· 69 71 response, error := ping(client, u) 70 72 71 73 if error != nil { 72 - sendToTinybird(response) 73 - if u.Status == "active" { 74 - // updateStatus(UpdateData{ 75 - // MonitorId: u.MonitorId, 76 - // Status: "error", 77 - // Message: error.Error(), 78 - // Region: region, 79 - // }) 74 + // Add one more retry 75 + response, error = ping(client, u) 76 + if error != nil { 77 + sendToTinybirdNew(response) 78 + if u.Status == "active" { 79 + updateStatus(UpdateData{ 80 + MonitorId: u.MonitorId, 81 + Status: "error", 82 + Message: error.Error(), 83 + Region: region, 84 + }) 85 + } 86 + w.Write([]byte("Ok")) 87 + w.WriteHeader(200) 88 + return 80 89 } 81 - w.Write([]byte("Ok")) 82 - w.WriteHeader(200) 83 - return 84 90 } 85 - 86 - sendToTinybird(response) 87 91 88 92 if response.StatusCode < 200 || response.StatusCode >= 300 { 89 - // If the status code is not within the 200 range, we update the status to error 90 - // updateStatus(UpdateData{ 91 - // MonitorId: u.MonitorId, 92 - // Status: "error", 93 - // StatusCode: response.StatusCode, 94 - // Region: region, 95 - // }) 96 - } 97 - if u.Status == "error" { 98 - // If the status was error, we update it to active 99 - // updateStatus(UpdateData{ 100 - // MonitorId: u.MonitorId, 101 - // Status: "active", 102 - // Region: region, 103 - // StatusCode: response.StatusCode, 104 - // }) 105 - } 106 - 107 - fmt.Printf("⏱️ End checker for %+v with latency %+d and statusCode %+d", u, response.Latency, response.StatusCode) 108 - w.Write([]byte("Ok")) 109 - w.WriteHeader(200) 110 - return 111 - }) 112 - // That's the new checker sending to the correct ingest endpoint 113 - r.Post("/checker", func(w http.ResponseWriter, r *http.Request) { 114 - if r.Header.Get("Authorization") != "Basic "+os.Getenv("CRON_SECRET") { 115 - http.Error(w, "Unauthorized", 401) 116 - return 117 - } 118 - i, err := strconv.Atoi(r.Header.Get("X-CloudTasks-TaskRetryCount")) 119 - if err != nil { 120 - http.Error(w, "Something went whont", 400) 121 - return 122 - } 123 - // If something went wrong we only try it twice 124 - if i > 1 { 125 - w.WriteHeader(http.StatusOK) 126 - w.Write([]byte("Ok")) 127 - return 128 - } 129 - 130 - if r.Body == nil { 131 - http.Error(w, "Please send a request body", 400) 132 - return 133 - } 134 - var u InputData 135 - region := os.Getenv("FLY_REGION") 136 - 137 - err = json.NewDecoder(r.Body).Decode(&u) 138 - 139 - fmt.Printf("🚀 Start checker for %+v \n", u) 140 - 141 - if err != nil { 142 - w.Write([]byte("Ok")) 143 - w.WriteHeader(200) 144 - return 145 - } 146 - 147 - client := &http.Client{} 148 - defer client.CloseIdleConnections() 149 - 150 - response, error := ping(client, u) 151 - 152 - if error != nil { 153 - sendToTinybirdNew(response) 154 - if u.Status == "active" { 93 + // Add one more retry 94 + response, error = ping(client, u) 95 + if response.StatusCode < 200 || response.StatusCode >= 300 && u.Status == "active" { 96 + // If the status code is not within the 200 range, we update the status to error 155 97 updateStatus(UpdateData{ 156 - MonitorId: u.MonitorId, 157 - Status: "error", 158 - Message: error.Error(), 159 - Region: region, 98 + MonitorId: u.MonitorId, 99 + Status: "error", 100 + StatusCode: response.StatusCode, 101 + Region: region, 160 102 }) 161 103 } 162 - w.Write([]byte("Ok")) 163 - w.WriteHeader(200) 164 - return 165 104 } 166 105 167 - sendToTinybirdNew(response) 168 - 169 - if response.StatusCode < 200 || response.StatusCode >= 300 { 170 - // If the status code is not within the 200 range, we update the status to error 171 - updateStatus(UpdateData{ 172 - MonitorId: u.MonitorId, 173 - Status: "error", 174 - StatusCode: response.StatusCode, 175 - Region: region, 176 - }) 177 - } 178 - if u.Status == "error" { 106 + // If the status was error and the status code is within the 200 range, we update the status to active 107 + if u.Status == "error" && response.StatusCode >= 200 && response.StatusCode < 300 { 179 108 // If the status was error, we update it to active 180 109 updateStatus(UpdateData{ 181 110 MonitorId: u.MonitorId, ··· 184 113 StatusCode: response.StatusCode, 185 114 }) 186 115 } 116 + // We send the data to Tinybird 117 + sendToTinybirdNew(response) 187 118 188 - fmt.Printf("⏱️ End checker for %+v with latency %+d and statusCode %+d", u, response.Latency, response.StatusCode) 119 + fmt.Printf("⏱️ End checker for %v with latency %d and statusCode %d", u, response.Latency, response.StatusCode) 189 120 w.Write([]byte("Ok")) 190 121 w.WriteHeader(200) 191 122 return
+2 -1
apps/docs/package.json
··· 3 3 "version": "0.0.0", 4 4 "private": true, 5 5 "scripts": { 6 - "dev": "npx mintlify@latest dev" 6 + "dev": "npx mintlify@latest dev", 7 + "verify": "npx mintlify@latest broken-links" 7 8 }, 8 9 "keywords": [], 9 10 "author": "OpenStatus"