Skip to content

Commit c9a7faf

Browse files
authored
Merge pull request #1067 from utmstack/bugfix/10.6.1/improve-log-explorer-ui
add retries to index check
2 parents 3646987 + 045c524 commit c9a7faf

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

installer/cloud.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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
}

installer/master.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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
}

installer/utils/elastic.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package utils
33
import (
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+
}

0 commit comments

Comments
 (0)