mirror of
https://github.com/docker/compose.git
synced 2025-07-12 16:24:32 +02:00
Fix race (parallel update of collection) to remove orphan containers
Signed-off-by: Guillaume Tardif <guillaume.tardif@gmail.com>
This commit is contained in:
parent
fbe1ebc054
commit
d720eb6c03
@ -70,20 +70,6 @@ func (containers Containers) filter(predicate containerPredicate) Containers {
|
|||||||
return filtered
|
return filtered
|
||||||
}
|
}
|
||||||
|
|
||||||
// split return Containers with elements to match and those not to match predicate
|
|
||||||
func (containers Containers) split(predicate containerPredicate) (Containers, Containers) {
|
|
||||||
var right Containers
|
|
||||||
var left Containers
|
|
||||||
for _, c := range containers {
|
|
||||||
if predicate(c) {
|
|
||||||
right = append(right, c)
|
|
||||||
} else {
|
|
||||||
left = append(left, c)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return right, left
|
|
||||||
}
|
|
||||||
|
|
||||||
func (containers Containers) names() []string {
|
func (containers Containers) names() []string {
|
||||||
var names []string
|
var names []string
|
||||||
for _, c := range containers {
|
for _, c := range containers {
|
||||||
|
@ -57,17 +57,17 @@ func (s *composeService) Down(ctx context.Context, projectName string, options c
|
|||||||
}
|
}
|
||||||
|
|
||||||
err = InReverseDependencyOrder(ctx, options.Project, func(c context.Context, service types.ServiceConfig) error {
|
err = InReverseDependencyOrder(ctx, options.Project, func(c context.Context, service types.ServiceConfig) error {
|
||||||
serviceContainers, others := containers.split(isService(service.Name))
|
serviceContainers := containers.filter(isService(service.Name))
|
||||||
err := s.removeContainers(ctx, w, serviceContainers)
|
err := s.removeContainers(ctx, w, serviceContainers)
|
||||||
containers = others
|
|
||||||
return err
|
return err
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if options.RemoveOrphans && len(containers) > 0 {
|
orphans := containers.filter(isNotService(options.Project.ServiceNames()...))
|
||||||
err := s.removeContainers(ctx, w, containers)
|
if options.RemoveOrphans && len(orphans) > 0 {
|
||||||
|
err := s.removeContainers(ctx, w, orphans)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -64,9 +64,7 @@ func TestDownRemoveOrphans(t *testing.T) {
|
|||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
api.EXPECT().ContainerList(ctx, projectFilterListOpt(testProject)).Return(
|
api.EXPECT().ContainerList(ctx, projectFilterListOpt(testProject)).Return(
|
||||||
[]apitypes.Container{testContainer("service1", "123"),
|
[]apitypes.Container{testContainer("service1", "123"), testContainer("service2", "789"), testContainer("service_orphan", "321")}, nil).Times(2)
|
||||||
testContainer("service2", "789"),
|
|
||||||
testContainer("service_orphan", "321")}, nil).Times(2)
|
|
||||||
|
|
||||||
api.EXPECT().ContainerStop(ctx, "123", nil).Return(nil)
|
api.EXPECT().ContainerStop(ctx, "123", nil).Return(nil)
|
||||||
api.EXPECT().ContainerStop(ctx, "789", nil).Return(nil)
|
api.EXPECT().ContainerStop(ctx, "789", nil).Return(nil)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user