Skip to content

Commit

Permalink
Avoid infinite loop while waiting for 'DONE' operation status
Browse files Browse the repository at this point in the history
  • Loading branch information
powerkimhub committed Oct 24, 2024
1 parent e9c2f35 commit 569a822
Showing 1 changed file with 36 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -484,24 +484,46 @@ func (vmHandler *GCPVMHandler) StartVM(vmReqInfo irs.VMReqInfo) (irs.VMInfo, err

// check operation status, wait until operation is completed
// This process is required because some operations have not error message but failed.
timeoutDuration := 1 * time.Hour
timeout := time.After(timeoutDuration)

retryCount := 0
maxRetries := 3

for {
result, err := vmHandler.Client.ZoneOperations.Get(projectID, zone, op.Name).Context(context.Background()).Do()
if err != nil {
cblogger.Errorf("Failed to get operation: %v", err)
}
if result.Status == "DONE" {
if result.Error != nil {
var errorMessages []string
for _, err := range result.Error.Errors {
cblogger.Errorf("Operation error: %v", err.Message)
errorMessages = append(errorMessages, err.Message)
select {
case <-timeout:
return irs.VMInfo{}, fmt.Errorf("Operation %s in project %s, zone %s timed out after %v", op.Name, projectID, zone, timeoutDuration)
default:
result, err := vmHandler.Client.ZoneOperations.Get(projectID, zone, op.Name).Context(context.Background()).Do()
if err != nil {
retryCount++
if retryCount < maxRetries {
cblogger.Infof("Failed to get operation (retry %d/%d): %v. Retrying...", retryCount, maxRetries, err)
continue
} else {
return irs.VMInfo{}, fmt.Errorf("Failed to get operation %s in project %s, zone %s after %d retries: %v", op.Name, projectID, zone, maxRetries, err)
}
} else {
retryCount = 0
}

// result.Status Possible values: "DONE", "PENDING", "RUNNING"
if result.Status == "DONE" {
if result.Error != nil {
var errorMessages []string
for _, err := range result.Error.Errors {
cblogger.Errorf("Operation error: %v", err.Message)
errorMessages = append(errorMessages, err.Message)
}
combinedError := fmt.Errorf("Operation errors: %s", strings.Join(errorMessages, ", "))
return irs.VMInfo{}, combinedError
}
combinedError := fmt.Errorf("Operation errors: %s", strings.Join(errorMessages, ", "))
return irs.VMInfo{}, combinedError
break
}
break

time.Sleep(15 * time.Second)
}
time.Sleep(15 * time.Second)
}

/*
Expand Down

0 comments on commit 569a822

Please sign in to comment.