Avoid starting all services on rebuild

Signed-off-by: Joana Hrotko <joana.hrotko@docker.com>
This commit is contained in:
Joana Hrotko 2024-10-31 14:59:04 +00:00 committed by Joana H
parent a000978980
commit fbbd6f83d7
2 changed files with 18 additions and 15 deletions

View File

@ -214,13 +214,13 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
} }
var ( var (
expected []string expected = utils.NewSet[string]()
watched = map[string]int{} watched = map[string]int{}
replaced []string replaced []string
) )
for _, c := range containers { for _, c := range containers {
if isRequired(c) { if isRequired(c) {
expected = append(expected, c.ID) expected.Add(c.ID)
} }
watched[c.ID] = 0 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 // be able to inspect in time before they're gone from the
// API, so just remove the watch without erroring // API, so just remove the watch without erroring
delete(watched, event.Container) delete(watched, event.Container)
expected = utils.Remove(expected, event.Container) expected.Remove(event.Container)
return nil return nil
} }
return err return err
@ -253,7 +253,6 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
Labels: inspected.Config.Labels, Labels: inspected.Config.Labels,
} }
name := getContainerNameWithoutProject(container) name := getContainerNameWithoutProject(container)
service := container.Labels[api.ServiceLabel] service := container.Labels[api.ServiceLabel]
switch event.Status { switch event.Status {
case "stop": case "stop":
@ -278,7 +277,7 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
} }
delete(watched, container.ID) delete(watched, container.ID)
expected = utils.Remove(expected, container.ID) expected.Remove(container.ID)
case "die": case "die":
restarted := watched[container.ID] restarted := watched[container.ID]
watched[container.ID] = restarted + 1 watched[container.ID] = restarted + 1
@ -308,7 +307,7 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
if !willRestart { if !willRestart {
// we're done with this one // we're done with this one
delete(watched, container.ID) delete(watched, container.ID)
expected = utils.Remove(expected, container.ID) expected.Remove(container.ID)
} }
case "start": case "start":
count, ok := watched[container.ID] count, ok := watched[container.ID]
@ -316,7 +315,7 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
if !ok { if !ok {
// A new container has just been added to service by scale // A new container has just been added to service by scale
watched[container.ID] = 0 watched[container.ID] = 0
expected = append(expected, container.ID) expected.Add(container.ID)
mustAttach = true mustAttach = true
} }
if mustAttach { if mustAttach {
@ -333,17 +332,15 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
if err != nil { if err != nil {
return err return err
} }
if utils.StringContains(expected, id) { if expected.Has(id) {
expected = append(expected, inspected.ID) expected.Add(inspected.ID)
expected.Add(container.ID)
} }
watched[container.ID] = 1 watched[container.ID] = 1
if utils.Contains(expected, id) {
expected = append(expected, container.ID)
}
} else if ofInterest(container) { } else if ofInterest(container) {
watched[container.ID] = 1 watched[container.ID] = 1
if isRequired(container) { if isRequired(container) {
expected = append(expected, container.ID) expected.Add(container.ID)
} }
} }
} }

View File

@ -512,9 +512,15 @@ func (s *composeService) handleWatchBatch(ctx context.Context, project *types.Pr
return err return err
} }
services := []string{serviceName}
p, err := project.WithSelectedServices(services)
if err != nil {
return err
}
err = s.start(ctx, project.Name, api.StartOptions{ err = s.start(ctx, project.Name, api.StartOptions{
Project: project, Project: p,
Services: []string{serviceName}, Services: services,
AttachTo: services,
}, nil) }, nil)
if err != nil { if err != nil {
options.LogTo.Log(api.WatchLogger, fmt.Sprintf("Application failed to start after update. Error: %v", err)) options.LogTo.Log(api.WatchLogger, fmt.Sprintf("Application failed to start after update. Error: %v", err))