File tree Expand file tree Collapse file tree 3 files changed +22
-2
lines changed
Expand file tree Collapse file tree 3 files changed +22
-2
lines changed Original file line number Diff line number Diff line change @@ -213,7 +213,7 @@ func Cloud(c *types.Config, update bool) error {
213213 }
214214
215215 indexURL := "http://localhost:9200/.utm-geoip?pretty"
216- indexExists , err := utils .CheckIndexExists (indexURL )
216+ indexExists , err := utils .CheckIndexExistsWithRetry (indexURL )
217217 if err != nil {
218218 return err
219219 }
Original file line number Diff line number Diff line change @@ -201,7 +201,7 @@ func Master(c *types.Config) error {
201201 }
202202
203203 indexURL := "http://localhost:9200/.utm-geoip?pretty"
204- indexExists , err := utils .CheckIndexExists (indexURL )
204+ indexExists , err := utils .CheckIndexExistsWithRetry (indexURL )
205205 if err != nil {
206206 return err
207207 }
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ package utils
33import (
44 "fmt"
55 "net/http"
6+ "time"
67)
78
89// CheckIndexExists checks if the given Elasticsearch index exists by sending an HTTP GET
@@ -26,3 +27,22 @@ func CheckIndexExists(url string) (bool, error) {
2627 return false , fmt .Errorf ("unexpected status code: %d" , resp .StatusCode )
2728 }
2829}
30+
31+ // retrying every 5 seconds up to a total duration of 1 minute.
32+ func CheckIndexExistsWithRetry (url string ) (bool , error ) {
33+ timeout := time .Minute
34+ interval := 5 * time .Second
35+ deadline := time .Now ().Add (timeout )
36+
37+ var lastErr error
38+ for time .Now ().Before (deadline ) {
39+ exists , err := CheckIndexExists (url )
40+ if err == nil {
41+ return exists , nil
42+ }
43+ lastErr = err
44+ fmt .Printf ("Attempt failed: %v. Retrying in %v...\n " , err , interval )
45+ time .Sleep (interval )
46+ }
47+ return false , fmt .Errorf ("failed after retries: %v" , lastErr )
48+ }
You can’t perform that action at this time.
0 commit comments