restart.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "context"
  16. "strings"
  17. "github.com/compose-spec/compose-go/types"
  18. "github.com/docker/compose/v2/pkg/api"
  19. "github.com/docker/compose/v2/pkg/progress"
  20. "github.com/docker/compose/v2/pkg/utils"
  21. containerType "github.com/docker/docker/api/types/container"
  22. "golang.org/x/sync/errgroup"
  23. )
  24. func (s *composeService) Restart(ctx context.Context, projectName string, options api.RestartOptions) error {
  25. return progress.Run(ctx, func(ctx context.Context) error {
  26. return s.restart(ctx, strings.ToLower(projectName), options)
  27. })
  28. }
  29. func (s *composeService) restart(ctx context.Context, projectName string, options api.RestartOptions) error {
  30. containers, err := s.getContainers(ctx, projectName, oneOffExclude, true)
  31. if err != nil {
  32. return err
  33. }
  34. project := options.Project
  35. if project == nil {
  36. project, err = s.getProjectWithResources(ctx, containers, projectName)
  37. if err != nil {
  38. return err
  39. }
  40. }
  41. // ignore depends_on relations which are not impacted by restarting service
  42. for i, service := range project.Services {
  43. for name, r := range service.DependsOn {
  44. if !r.Restart {
  45. delete(service.DependsOn, name)
  46. }
  47. }
  48. project.Services[i] = service
  49. }
  50. if len(options.Services) != 0 {
  51. err = project.ForServices(options.Services, types.IncludeDependents)
  52. if err != nil {
  53. return err
  54. }
  55. }
  56. w := progress.ContextWriter(ctx)
  57. return InDependencyOrder(ctx, project, func(c context.Context, service string) error {
  58. eg, ctx := errgroup.WithContext(ctx)
  59. for _, container := range containers.filter(isService(service)) {
  60. container := container
  61. eg.Go(func() error {
  62. eventName := getContainerProgressName(container)
  63. w.Event(progress.RestartingEvent(eventName))
  64. timeout := utils.DurationSecondToInt(options.Timeout)
  65. err := s.apiClient().ContainerRestart(ctx, container.ID, containerType.StopOptions{Timeout: timeout})
  66. if err == nil {
  67. w.Event(progress.StartedEvent(eventName))
  68. }
  69. return err
  70. })
  71. }
  72. return eg.Wait()
  73. })
  74. }