Skip to content

Commit 795b7d5

Browse files
authored
Merge pull request #5788 from thaJeztah/progress_clean
service/progress: ServiceProgress: avoid fuzzy matching service ID in loop
2 parents 919bd6a + a428800 commit 795b7d5

1 file changed

Lines changed: 18 additions & 19 deletions

File tree

cli/command/service/progress/progress.go

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,6 @@ func ServiceProgress(ctx context.Context, apiClient client.APIClient, serviceID
7979
signal.Notify(sigint, os.Interrupt)
8080
defer signal.Stop(sigint)
8181

82-
taskFilter := filters.NewArgs()
83-
taskFilter.Add("service", serviceID)
84-
taskFilter.Add("_up-to-date", "true")
85-
86-
getUpToDateTasks := func() ([]swarm.Task, error) {
87-
return apiClient.TaskList(ctx, types.TaskListOptions{Filters: taskFilter})
88-
}
89-
9082
var (
9183
updater progressUpdater
9284
converged bool
@@ -151,7 +143,10 @@ func ServiceProgress(ctx context.Context, apiClient client.APIClient, serviceID
151143
return nil
152144
}
153145

154-
tasks, err := getUpToDateTasks()
146+
tasks, err := apiClient.TaskList(ctx, types.TaskListOptions{Filters: filters.NewArgs(
147+
filters.KeyValuePair{Key: "service", Value: service.ID},
148+
filters.KeyValuePair{Key: "_up-to-date", Value: "true"},
149+
)})
155150
if err != nil {
156151
return err
157152
}
@@ -218,7 +213,10 @@ func ServiceProgress(ctx context.Context, apiClient client.APIClient, serviceID
218213
}
219214
}
220215

221-
func getActiveNodes(ctx context.Context, apiClient client.APIClient) (map[string]struct{}, error) {
216+
// getActiveNodes returns all nodes that are currently not in status [swarm.NodeStateDown].
217+
//
218+
// TODO(thaJeztah): this should really be a filter on [apiClient.NodeList] instead of being filtered on the client side.
219+
func getActiveNodes(ctx context.Context, apiClient client.NodeAPIClient) (map[string]struct{}, error) {
222220
nodes, err := apiClient.NodeList(ctx, types.NodeListOptions{})
223221
if err != nil {
224222
return nil, err
@@ -573,16 +571,17 @@ type replicatedJobProgressUpdater struct {
573571
}
574572

575573
func newReplicatedJobProgressUpdater(service swarm.Service, progressOut progress.Output) *replicatedJobProgressUpdater {
576-
u := &replicatedJobProgressUpdater{
577-
progressOut: progressOut,
578-
concurrent: int(*service.Spec.Mode.ReplicatedJob.MaxConcurrent),
579-
total: int(*service.Spec.Mode.ReplicatedJob.TotalCompletions),
580-
jobIteration: service.JobStatus.JobIteration.Index,
581-
}
582-
u.progressDigits = len(strconv.Itoa(u.total))
583-
u.activeDigits = len(strconv.Itoa(u.concurrent))
574+
concurrent := int(*service.Spec.Mode.ReplicatedJob.MaxConcurrent)
575+
total := int(*service.Spec.Mode.ReplicatedJob.TotalCompletions)
584576

585-
return u
577+
return &replicatedJobProgressUpdater{
578+
progressOut: progressOut,
579+
concurrent: concurrent,
580+
total: total,
581+
jobIteration: service.JobStatus.JobIteration.Index,
582+
progressDigits: len(strconv.Itoa(total)),
583+
activeDigits: len(strconv.Itoa(concurrent)),
584+
}
586585
}
587586

588587
// update writes out the progress of the replicated job.

0 commit comments

Comments
 (0)