Skip to content

Commit fbbd6f8

Browse files
committed
Avoid starting all services on rebuild
Signed-off-by: Joana Hrotko <[email protected]>
1 parent a000978 commit fbbd6f8

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

pkg/compose/start.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,13 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
214214
}
215215

216216
var (
217-
expected []string
217+
expected = utils.NewSet[string]()
218218
watched = map[string]int{}
219219
replaced []string
220220
)
221221
for _, c := range containers {
222222
if isRequired(c) {
223-
expected = append(expected, c.ID)
223+
expected.Add(c.ID)
224224
}
225225
watched[c.ID] = 0
226226
}
@@ -242,7 +242,7 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
242242
// be able to inspect in time before they're gone from the
243243
// API, so just remove the watch without erroring
244244
delete(watched, event.Container)
245-
expected = utils.Remove(expected, event.Container)
245+
expected.Remove(event.Container)
246246
return nil
247247
}
248248
return err
@@ -253,7 +253,6 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
253253
Labels: inspected.Config.Labels,
254254
}
255255
name := getContainerNameWithoutProject(container)
256-
257256
service := container.Labels[api.ServiceLabel]
258257
switch event.Status {
259258
case "stop":
@@ -278,7 +277,7 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
278277
}
279278

280279
delete(watched, container.ID)
281-
expected = utils.Remove(expected, container.ID)
280+
expected.Remove(container.ID)
282281
case "die":
283282
restarted := watched[container.ID]
284283
watched[container.ID] = restarted + 1
@@ -308,15 +307,15 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
308307
if !willRestart {
309308
// we're done with this one
310309
delete(watched, container.ID)
311-
expected = utils.Remove(expected, container.ID)
310+
expected.Remove(container.ID)
312311
}
313312
case "start":
314313
count, ok := watched[container.ID]
315314
mustAttach := ok && count > 0 // Container restarted, need to re-attach
316315
if !ok {
317316
// A new container has just been added to service by scale
318317
watched[container.ID] = 0
319-
expected = append(expected, container.ID)
318+
expected.Add(container.ID)
320319
mustAttach = true
321320
}
322321
if mustAttach {
@@ -333,17 +332,15 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
333332
if err != nil {
334333
return err
335334
}
336-
if utils.StringContains(expected, id) {
337-
expected = append(expected, inspected.ID)
335+
if expected.Has(id) {
336+
expected.Add(inspected.ID)
337+
expected.Add(container.ID)
338338
}
339339
watched[container.ID] = 1
340-
if utils.Contains(expected, id) {
341-
expected = append(expected, container.ID)
342-
}
343340
} else if ofInterest(container) {
344341
watched[container.ID] = 1
345342
if isRequired(container) {
346-
expected = append(expected, container.ID)
343+
expected.Add(container.ID)
347344
}
348345
}
349346
}

pkg/compose/watch.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,9 +512,15 @@ func (s *composeService) handleWatchBatch(ctx context.Context, project *types.Pr
512512
return err
513513
}
514514

515+
services := []string{serviceName}
516+
p, err := project.WithSelectedServices(services)
517+
if err != nil {
518+
return err
519+
}
515520
err = s.start(ctx, project.Name, api.StartOptions{
516-
Project: project,
517-
Services: []string{serviceName},
521+
Project: p,
522+
Services: services,
523+
AttachTo: services,
518524
}, nil)
519525
if err != nil {
520526
options.LogTo.Log(api.WatchLogger, fmt.Sprintf("Application failed to start after update. Error: %v", err))

0 commit comments

Comments
 (0)