Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid starting all services on rebuild #12258

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions pkg/compose/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -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":
Expand All @@ -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
Expand Down Expand Up @@ -308,15 +307,15 @@ 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]
mustAttach := ok && count > 0 // Container restarted, need to re-attach
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 {
Expand All @@ -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)
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions pkg/compose/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Loading