mirror of https://github.com/docker/compose.git
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
|
||||
}
|
||||
|
||||
// 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 {
|
||||
var names []string
|
||||
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 {
|
||||
serviceContainers, others := containers.split(isService(service.Name))
|
||||
serviceContainers := containers.filter(isService(service.Name))
|
||||
err := s.removeContainers(ctx, w, serviceContainers)
|
||||
containers = others
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if options.RemoveOrphans && len(containers) > 0 {
|
||||
err := s.removeContainers(ctx, w, containers)
|
||||
orphans := containers.filter(isNotService(options.Project.ServiceNames()...))
|
||||
if options.RemoveOrphans && len(orphans) > 0 {
|
||||
err := s.removeContainers(ctx, w, orphans)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -64,9 +64,7 @@ func TestDownRemoveOrphans(t *testing.T) {
|
|||
|
||||
ctx := context.Background()
|
||||
api.EXPECT().ContainerList(ctx, projectFilterListOpt(testProject)).Return(
|
||||
[]apitypes.Container{testContainer("service1", "123"),
|
||||
testContainer("service2", "789"),
|
||||
testContainer("service_orphan", "321")}, nil).Times(2)
|
||||
[]apitypes.Container{testContainer("service1", "123"), testContainer("service2", "789"), testContainer("service_orphan", "321")}, nil).Times(2)
|
||||
|
||||
api.EXPECT().ContainerStop(ctx, "123", nil).Return(nil)
|
||||
api.EXPECT().ContainerStop(ctx, "789", nil).Return(nil)
|
||||
|
|
Loading…
Reference in New Issue