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 (
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)
}
}
}

View File

@ -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))