Skip to content

Commit

Permalink
fix: send more precise progress values (#304)
Browse files Browse the repository at this point in the history
Include the running action progress in the actions progress calculation.

The function now considers a failed action as completed, and is included in the progress. Not counting the failed action as completed might send some weird progress values e.g. [0%, 75%, (failed action occurs here) 50%], while if we count them, we should have [0%, 75%, 100%].

Related to hetznercloud/cli#528

(cherry picked from commit 867aa63)
  • Loading branch information
jooola authored and apricote committed Aug 17, 2023
1 parent 6390788 commit 96ab1c4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
17 changes: 13 additions & 4 deletions hcloud/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,15 @@ func (c *ActionClient) WatchOverallProgress(ctx context.Context, actions []*Acti
defer close(errCh)
defer close(progressCh)

successIDs := make([]int, 0, len(actions))
completedIDs := make([]int, 0, len(actions))
watchIDs := make(map[int]struct{}, len(actions))

for _, action := range actions {
watchIDs[action.ID] = struct{}{}
}

retries := 0
previousProgress := 0

for {
select {
Expand Down Expand Up @@ -224,20 +226,27 @@ func (c *ActionClient) WatchOverallProgress(ctx context.Context, actions []*Acti
return
}

progress := 0
for _, a := range as {
switch a.Status {
case ActionStatusRunning:
continue
progress += a.Progress
case ActionStatusSuccess:
delete(watchIDs, a.ID)
successIDs = append(successIDs, a.ID)
sendProgress(progressCh, int(float64(len(actions)-len(successIDs))/float64(len(actions))*100))
completedIDs = append(completedIDs, a.ID)
case ActionStatusError:
delete(watchIDs, a.ID)
completedIDs = append(completedIDs, a.ID)
errCh <- fmt.Errorf("action %d failed: %w", a.ID, a.Error())
}
}

progress += (len(completedIDs) * 100)
if progress != 0 && progress != previousProgress {
sendProgress(progressCh, int(progress/len(actions)))
previousProgress = progress
}

if len(watchIDs) == 0 {
return
}
Expand Down
2 changes: 1 addition & 1 deletion hcloud/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func TestActionClientWatchOverallProgress(t *testing.T) {
t.Fatalf("expected hcloud.Error, but got: %#v", err)
}

expectedProgressUpdates := []int{50}
expectedProgressUpdates := []int{50, 100}
if !reflect.DeepEqual(progressUpdates, expectedProgressUpdates) {
t.Fatalf("expected progresses %v but received %v", expectedProgressUpdates, progressUpdates)
}
Expand Down

0 comments on commit 96ab1c4

Please sign in to comment.