From 96ab1c4b613889cc9de3506304286b17ec6d5c8e Mon Sep 17 00:00:00 2001 From: Jonas L Date: Tue, 15 Aug 2023 15:31:01 +0200 Subject: [PATCH] fix: send more precise progress values (#304) 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 https://github.com/hetznercloud/cli/issues/528 (cherry picked from commit 867aa632521ad3acfb04beb52b6307330740fc68) --- hcloud/action.go | 17 +++++++++++++---- hcloud/action_test.go | 2 +- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/hcloud/action.go b/hcloud/action.go index 3131266f..2f4d01e3 100644 --- a/hcloud/action.go +++ b/hcloud/action.go @@ -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 { @@ -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 } diff --git a/hcloud/action_test.go b/hcloud/action_test.go index 0f69b852..70b0f3f8 100644 --- a/hcloud/action_test.go +++ b/hcloud/action_test.go @@ -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) }