mirror of
https://github.com/docker/compose.git
synced 2025-07-23 21:54:40 +02:00
introduce stopAndRemoveContainer to share logic scaling down
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
parent
143ac0f1b6
commit
1551fcb4e6
@ -134,14 +134,7 @@ func (c *convergence) ensureService(ctx context.Context, project *types.Project,
|
|||||||
container := container
|
container := container
|
||||||
traceOpts := append(tracing.ServiceOptions(service), tracing.ContainerOptions(container)...)
|
traceOpts := append(tracing.ServiceOptions(service), tracing.ContainerOptions(container)...)
|
||||||
eg.Go(tracing.SpanWrapFuncForErrGroup(ctx, "service/scale/down", traceOpts, func(ctx context.Context) error {
|
eg.Go(tracing.SpanWrapFuncForErrGroup(ctx, "service/scale/down", traceOpts, func(ctx context.Context) error {
|
||||||
timeoutInSecond := utils.DurationSecondToInt(timeout)
|
return c.service.stopAndRemoveContainer(ctx, container, timeout, false)
|
||||||
err := c.service.apiClient().ContainerStop(ctx, container.ID, containerType.StopOptions{
|
|
||||||
Timeout: timeoutInSecond,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return c.service.apiClient().ContainerRemove(ctx, container.ID, containerType.RemoveOptions{})
|
|
||||||
}))
|
}))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -99,8 +99,7 @@ func (s *composeService) create(ctx context.Context, project *types.Project, opt
|
|||||||
orphans := observedState.filter(isNotService(allServiceNames...))
|
orphans := observedState.filter(isNotService(allServiceNames...))
|
||||||
if len(orphans) > 0 && !options.IgnoreOrphans {
|
if len(orphans) > 0 && !options.IgnoreOrphans {
|
||||||
if options.RemoveOrphans {
|
if options.RemoveOrphans {
|
||||||
w := progress.ContextWriter(ctx)
|
err := s.removeContainers(ctx, orphans, nil, false)
|
||||||
err := s.removeContainers(ctx, w, orphans, nil, false)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -76,7 +76,7 @@ func (s *composeService) down(ctx context.Context, projectName string, options a
|
|||||||
|
|
||||||
err = InReverseDependencyOrder(ctx, project, func(c context.Context, service string) error {
|
err = InReverseDependencyOrder(ctx, project, func(c context.Context, service string) error {
|
||||||
serviceContainers := containers.filter(isService(service))
|
serviceContainers := containers.filter(isService(service))
|
||||||
err := s.removeContainers(ctx, w, serviceContainers, options.Timeout, options.Volumes)
|
err := s.removeContainers(ctx, serviceContainers, options.Timeout, options.Volumes)
|
||||||
return err
|
return err
|
||||||
}, WithRootNodesAndDown(options.Services))
|
}, WithRootNodesAndDown(options.Services))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -85,7 +85,7 @@ func (s *composeService) down(ctx context.Context, projectName string, options a
|
|||||||
|
|
||||||
orphans := containers.filter(isOrphaned(project))
|
orphans := containers.filter(isOrphaned(project))
|
||||||
if options.RemoveOrphans && len(orphans) > 0 {
|
if options.RemoveOrphans && len(orphans) > 0 {
|
||||||
err := s.removeContainers(ctx, w, orphans, options.Timeout, false)
|
err := s.removeContainers(ctx, orphans, options.Timeout, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -303,11 +303,19 @@ func (s *composeService) stopContainers(ctx context.Context, w progress.Writer,
|
|||||||
return eg.Wait()
|
return eg.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *composeService) removeContainers(ctx context.Context, w progress.Writer, containers []moby.Container, timeout *time.Duration, volumes bool) error {
|
func (s *composeService) removeContainers(ctx context.Context, containers []moby.Container, timeout *time.Duration, volumes bool) error {
|
||||||
eg, _ := errgroup.WithContext(ctx)
|
eg, _ := errgroup.WithContext(ctx)
|
||||||
for _, container := range containers {
|
for _, container := range containers {
|
||||||
container := container
|
container := container
|
||||||
eg.Go(func() error {
|
eg.Go(func() error {
|
||||||
|
return s.stopAndRemoveContainer(ctx, container, timeout, volumes)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return eg.Wait()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *composeService) stopAndRemoveContainer(ctx context.Context, container moby.Container, timeout *time.Duration, volumes bool) error {
|
||||||
|
w := progress.ContextWriter(ctx)
|
||||||
eventName := getContainerProgressName(container)
|
eventName := getContainerProgressName(container)
|
||||||
err := s.stopContainer(ctx, w, container, timeout)
|
err := s.stopContainer(ctx, w, container, timeout)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -324,9 +332,6 @@ func (s *composeService) removeContainers(ctx context.Context, w progress.Writer
|
|||||||
}
|
}
|
||||||
w.Event(progress.RemovedEvent(eventName))
|
w.Event(progress.RemovedEvent(eventName))
|
||||||
return nil
|
return nil
|
||||||
})
|
|
||||||
}
|
|
||||||
return eg.Wait()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *composeService) getProjectWithResources(ctx context.Context, containers Containers, projectName string) (*types.Project, error) {
|
func (s *composeService) getProjectWithResources(ctx context.Context, containers Containers, projectName string) (*types.Project, error) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user