Skip to content

Commit

Permalink
Cloud: Fix stack wake-up function
Browse files Browse the repository at this point in the history
Closes #1725

In #1545, the stack readiness check was improved by calling the `/api/health` endpoint instead of just the stack URL
While this does provide a better indication that the instance is up and running, the `/api/health` subpath does not trigger Grafana Cloud's stack wake-up
With this PR, we'll call both the stack's base URL and its health endpoint
  • Loading branch information
julienduchesne committed Jul 29, 2024
1 parent 4dcbd6b commit 8e9661e
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions internal/resources/cloud/resource_cloud_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,25 +445,37 @@ func waitForStackReadiness(ctx context.Context, timeout time.Duration, stackURL
return diag.FromErr(joinErr)
}
err := retry.RetryContext(ctx, timeout, func() *retry.RetryError {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, healthURL, nil)
// Query the instance URL directly. This makes the stack wake-up if it has been paused.
// The health endpoint is helpful to check that the stack is ready, but it doesn't wake up the stack.
stackReq, err := http.NewRequestWithContext(ctx, http.MethodGet, stackURL, nil)
if err != nil {
return retry.NonRetryableError(err)
}
resp, err := http.DefaultClient.Do(req)
stackResp, err := http.DefaultClient.Do(stackReq)
if err != nil {
return retry.RetryableError(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
defer stackResp.Body.Close()

healthReq, err := http.NewRequestWithContext(ctx, http.MethodGet, healthURL, nil)
if err != nil {
return retry.NonRetryableError(err)
}
healthResp, err := http.DefaultClient.Do(healthReq)
if err != nil {
return retry.RetryableError(err)
}
defer healthResp.Body.Close()
if healthResp.StatusCode != 200 {
buf := new(bytes.Buffer)
body := ""
_, err = buf.ReadFrom(resp.Body)
_, err = buf.ReadFrom(healthResp.Body)
if err != nil {
body = "unable to read response body, error: " + err.Error()
} else {
body = buf.String()
}
return retry.RetryableError(fmt.Errorf("stack was not ready in %s. Status code: %d, Body: %s", timeout, resp.StatusCode, body))
return retry.RetryableError(fmt.Errorf("stack was not ready in %s. Status code: %d, Body: %s", timeout, healthResp.StatusCode, body))
}

return nil
Expand Down

0 comments on commit 8e9661e

Please sign in to comment.