From 3954c1ecf0c72f0b669b5b181f759a959a7b746e Mon Sep 17 00:00:00 2001 From: Raphael Randschau Date: Fri, 13 Jan 2017 11:49:16 +0100 Subject: [PATCH] provider/scaleway: delay between requests when waiting (#11184) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit the CI sees many failing Scaleway tests due to request quotas being exceeded. this PR aims to address this issue by switching from `resource.Retry`, which waits 100ms between retries, to `resource.StateChangeConf` with a configured delay of 5s between retries. this should help us fixing the quota issue… --- builtin/providers/scaleway/helpers.go | 57 ++++++++++++++------------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/builtin/providers/scaleway/helpers.go b/builtin/providers/scaleway/helpers.go index 7162eda08..d9876707a 100644 --- a/builtin/providers/scaleway/helpers.go +++ b/builtin/providers/scaleway/helpers.go @@ -48,21 +48,7 @@ func deleteRunningServer(scaleway *api.ScalewayAPI, server *api.ScalewayServer) return err } - return resource.Retry(20*time.Minute, func() *resource.RetryError { - _, err := scaleway.GetServer(server.Identifier) - - if err == nil { - return resource.RetryableError(fmt.Errorf("Waiting for server %q to be deleted", server.Identifier)) - } - - if serr, ok := err.(api.ScalewayAPIError); ok { - if serr.StatusCode == 404 { - return nil - } - } - - return resource.RetryableError(err) - }) + return waitForServerState(scaleway, server.Identifier, "stopped") } // deleteStoppedServer needs to cleanup attached root volumes. this is not done @@ -83,20 +69,37 @@ func deleteStoppedServer(scaleway *api.ScalewayAPI, server *api.ScalewayServer) // NOTE copied from github.com/scaleway/scaleway-cli/pkg/api/helpers.go // the helpers.go file pulls in quite a lot dependencies, and they're just convenience wrappers anyway +var allStates = []string{"starting", "running", "stopping", "stopped"} + func waitForServerState(scaleway *api.ScalewayAPI, serverID, targetState string) error { - return resource.Retry(60*time.Minute, func() *resource.RetryError { - scaleway.ClearCache() - - s, err := scaleway.GetServer(serverID) - - if err != nil { - return resource.NonRetryableError(err) + pending := []string{} + for _, state := range allStates { + if state != targetState { + pending = append(pending, state) } + } + stateConf := &resource.StateChangeConf{ + Pending: pending, + Target: []string{targetState}, + Refresh: func() (interface{}, string, error) { + s, err := scaleway.GetServer(serverID) - if s.State != targetState { - return resource.RetryableError(fmt.Errorf("Waiting for server to enter %q state", targetState)) - } + if err == nil { + return 42, s.State, nil + } - return nil - }) + if serr, ok := err.(api.ScalewayAPIError); ok { + if serr.StatusCode == 404 { + return 42, "stopped", nil + } + } + + return 42, s.State, err + }, + Timeout: 60 * time.Minute, + MinTimeout: 5 * time.Second, + Delay: 5 * time.Second, + } + _, err := stateConf.WaitForState() + return err }