Skip to content

Commit

Permalink
Add polling for the get instance
Browse files Browse the repository at this point in the history
  • Loading branch information
mkumatag committed Dec 16, 2024
1 parent 2d0d6fe commit 1e641c6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
14 changes: 12 additions & 2 deletions builder/powervs/step_create_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,20 @@ func (s *StepCreateInstance) Run(_ context.Context, state multistep.StateBag) mu
return multistep.ActionHalt
}

in, err := instanceClient.Get(insIDs[0])
if err != nil {
var in *models.PVMInstance

if err := pollUntil(time.Tick(30*time.Second), time.After(5*time.Minute), func() (bool, error) {

Check failure on line 66 in builder/powervs/step_create_instance.go

View workflow job for this annotation

GitHub Actions / Go Lint check

SA1015: using time.Tick leaks the underlying ticker, consider using it only in endless functions, tests and the main package, and use time.NewTicker here (staticcheck)
in, err = instanceClient.Get(insIDs[0])
if err != nil || in == nil {
ui.Message("No response or error encountered while retrieving the instance. Retrying...")
return false, nil
}
return true, nil
}); err != nil {
ui.Error(fmt.Sprintf("failed to get instance: %v", err))
return multistep.ActionHalt
}

ui.Message(fmt.Sprintf("Instance Created, Name: %s, ID: %s", *in.ServerName, *in.PvmInstanceID))

state.Put("instance", in)
Expand Down
24 changes: 24 additions & 0 deletions builder/powervs/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package powervs

import (
"fmt"
"time"
)

// pollUntil validates if a certain condition is met at defined poll intervals.
// If a timeout is reached, an associated error is returned to the caller.
// condition contains the use-case specific code that returns true when a certain condition is achieved.
func pollUntil(pollInterval, timeOut <-chan time.Time, condition func() (bool, error)) error {
for {
select {
case <-timeOut:
return fmt.Errorf("timed out while waiting for job to complete")
case <-pollInterval:
if done, err := condition(); err != nil {
return err
} else if done {
return nil
}
}
}
}

0 comments on commit 1e641c6

Please sign in to comment.