diff --git a/installer/cloud.go b/installer/cloud.go index 413ed49b8..177a98ebe 100644 --- a/installer/cloud.go +++ b/installer/cloud.go @@ -213,7 +213,7 @@ func Cloud(c *types.Config, update bool) error { } indexURL := "http://localhost:9200/.utm-geoip?pretty" - indexExists, err := utils.CheckIndexExists(indexURL) + indexExists, err := utils.CheckIndexExistsWithRetry(indexURL) if err != nil { return err } diff --git a/installer/master.go b/installer/master.go index 4fdd57c3a..27e540b65 100644 --- a/installer/master.go +++ b/installer/master.go @@ -201,7 +201,7 @@ func Master(c *types.Config) error { } indexURL := "http://localhost:9200/.utm-geoip?pretty" - indexExists, err := utils.CheckIndexExists(indexURL) + indexExists, err := utils.CheckIndexExistsWithRetry(indexURL) if err != nil { return err } diff --git a/installer/utils/elastic.go b/installer/utils/elastic.go index 9bb8f1cb5..dae3da2c2 100644 --- a/installer/utils/elastic.go +++ b/installer/utils/elastic.go @@ -3,6 +3,7 @@ package utils import ( "fmt" "net/http" + "time" ) // CheckIndexExists checks if the given Elasticsearch index exists by sending an HTTP GET @@ -26,3 +27,22 @@ func CheckIndexExists(url string) (bool, error) { return false, fmt.Errorf("unexpected status code: %d", resp.StatusCode) } } + +// retrying every 5 seconds up to a total duration of 1 minute. +func CheckIndexExistsWithRetry(url string) (bool, error) { + timeout := time.Minute + interval := 5 * time.Second + deadline := time.Now().Add(timeout) + + var lastErr error + for time.Now().Before(deadline) { + exists, err := CheckIndexExists(url) + if err == nil { + return exists, nil + } + lastErr = err + fmt.Printf("Attempt failed: %v. Retrying in %v...\n", err, interval) + time.Sleep(interval) + } + return false, fmt.Errorf("failed after retries: %v", lastErr) +}