Skip to content

Commit 2d25f56

Browse files
committed
exoscale: use the new utils.WaitForSpecificOrError function to wait for job to finish
Instead of using a custom loop, use the newly introduced `utils.WaitForSpecificOrError` function. Beware that on success, we use the successful response to get the VM object (outside the loop). Signed-off-by: Vincent Bernat <Vincent.Bernat@exoscale.ch>
1 parent 2a60a4e commit 2d25f56

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

drivers/exoscale/exoscale.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/docker/machine/drivers"
1414
"github.com/docker/machine/log"
1515
"github.com/docker/machine/state"
16+
"github.com/docker/machine/utils"
1617
"github.com/pyr/egoscale/src/egoscale"
1718
)
1819

@@ -421,28 +422,24 @@ func (d *Driver) Kill() error {
421422

422423
func (d *Driver) waitForVM(client *egoscale.Client, jobid string) (*egoscale.DeployVirtualMachineResponse, error) {
423424
log.Infof("Waiting for VM...")
424-
maxRepeats := 60
425-
i := 0
426425
var resp *egoscale.QueryAsyncJobResultResponse
427426
var err error
428-
WaitLoop:
429-
for ; i < maxRepeats; i++ {
427+
if err = utils.WaitForSpecificOrError(func() (bool, error) {
430428
resp, err = client.PollAsyncJob(jobid)
431429
if err != nil {
432-
return nil, err
430+
return true, err
433431
}
434432
switch resp.Jobstatus {
435433
case 0: // Job is still in progress
436434
case 1: // Job has successfully completed
437-
break WaitLoop
435+
return true, nil
438436
case 2: // Job has failed to complete
439-
return nil, fmt.Errorf("Operation failed to complete")
437+
return true, fmt.Errorf("Operation failed to complete")
440438
default: // Some other code
441439
}
442-
time.Sleep(2 * time.Second)
443-
}
444-
if i == maxRepeats {
445-
return nil, fmt.Errorf("Timeout while waiting for VM")
440+
return false, nil
441+
}, 60, 2*time.Second); err != nil {
442+
return nil, err
446443
}
447444
vm, err := client.AsyncToVirtualMachine(*resp)
448445
if err != nil {

0 commit comments

Comments
 (0)