From fbbd6f83d7d76d13887857215ad72f1d86534d40 Mon Sep 17 00:00:00 2001 From: Joana Hrotko Date: Thu, 31 Oct 2024 14:59:04 +0000 Subject: [PATCH] Avoid starting all services on rebuild Signed-off-by: Joana Hrotko --- pkg/compose/start.go | 23 ++++++++++------------- pkg/compose/watch.go | 10 ++++++++-- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/pkg/compose/start.go b/pkg/compose/start.go index e4ce16fd4..f5dbd9101 100644 --- a/pkg/compose/start.go +++ b/pkg/compose/start.go @@ -214,13 +214,13 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo } var ( - expected []string + expected = utils.NewSet[string]() watched = map[string]int{} replaced []string ) for _, c := range containers { if isRequired(c) { - expected = append(expected, c.ID) + expected.Add(c.ID) } watched[c.ID] = 0 } @@ -242,7 +242,7 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo // be able to inspect in time before they're gone from the // API, so just remove the watch without erroring delete(watched, event.Container) - expected = utils.Remove(expected, event.Container) + expected.Remove(event.Container) return nil } return err @@ -253,7 +253,6 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo Labels: inspected.Config.Labels, } name := getContainerNameWithoutProject(container) - service := container.Labels[api.ServiceLabel] switch event.Status { case "stop": @@ -278,7 +277,7 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo } delete(watched, container.ID) - expected = utils.Remove(expected, container.ID) + expected.Remove(container.ID) case "die": restarted := watched[container.ID] watched[container.ID] = restarted + 1 @@ -308,7 +307,7 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo if !willRestart { // we're done with this one delete(watched, container.ID) - expected = utils.Remove(expected, container.ID) + expected.Remove(container.ID) } case "start": count, ok := watched[container.ID] @@ -316,7 +315,7 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo if !ok { // A new container has just been added to service by scale watched[container.ID] = 0 - expected = append(expected, container.ID) + expected.Add(container.ID) mustAttach = true } if mustAttach { @@ -333,17 +332,15 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo if err != nil { return err } - if utils.StringContains(expected, id) { - expected = append(expected, inspected.ID) + if expected.Has(id) { + expected.Add(inspected.ID) + expected.Add(container.ID) } watched[container.ID] = 1 - if utils.Contains(expected, id) { - expected = append(expected, container.ID) - } } else if ofInterest(container) { watched[container.ID] = 1 if isRequired(container) { - expected = append(expected, container.ID) + expected.Add(container.ID) } } } diff --git a/pkg/compose/watch.go b/pkg/compose/watch.go index 15d2c44c4..f469820b1 100644 --- a/pkg/compose/watch.go +++ b/pkg/compose/watch.go @@ -512,9 +512,15 @@ func (s *composeService) handleWatchBatch(ctx context.Context, project *types.Pr return err } + services := []string{serviceName} + p, err := project.WithSelectedServices(services) + if err != nil { + return err + } err = s.start(ctx, project.Name, api.StartOptions{ - Project: project, - Services: []string{serviceName}, + Project: p, + Services: services, + AttachTo: services, }, nil) if err != nil { options.LogTo.Log(api.WatchLogger, fmt.Sprintf("Application failed to start after update. Error: %v", err))