restart.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.RunWithTitle(ctx, func(ctx context.Context) error {
  26. return s.restart(ctx, strings.ToLower(projectName), options)
  27. }, s.stdinfo(), "Restarting")
  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. if options.NoDeps {
  42. err := project.ForServices(options.Services, types.IgnoreDependencies)
  43. if err != nil {
  44. return err
  45. }
  46. }
  47. // ignore depends_on relations which are not impacted by restarting service or not required
  48. for i, service := range project.Services {
  49. for name, r := range service.DependsOn {
  50. if !r.Restart {
  51. delete(service.DependsOn, name)
  52. }
  53. }
  54. project.Services[i] = service
  55. }
  56. if len(options.Services) != 0 {
  57. err = project.ForServices(options.Services, types.IncludeDependents)
  58. if err != nil {
  59. return err
  60. }
  61. }
  62. w := progress.ContextWriter(ctx)
  63. return InDependencyOrder(ctx, project, func(c context.Context, service string) error {
  64. eg, ctx := errgroup.WithContext(ctx)
  65. for _, container := range containers.filter(isService(service)) {
  66. container := container
  67. eg.Go(func() error {
  68. eventName := getContainerProgressName(container)
  69. w.Event(progress.RestartingEvent(eventName))
  70. timeout := utils.DurationSecondToInt(options.Timeout)
  71. err := s.apiClient().ContainerRestart(ctx, container.ID, containerType.StopOptions{Timeout: timeout})
  72. if err == nil {
  73. w.Event(progress.StartedEvent(eventName))
  74. }
  75. return err
  76. })
  77. }
  78. return eg.Wait()
  79. })
  80. }