restart.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/v2/types"
  18. "github.com/docker/docker/api/types/container"
  19. "golang.org/x/sync/errgroup"
  20. "github.com/docker/compose/v5/pkg/api"
  21. "github.com/docker/compose/v5/pkg/utils"
  22. )
  23. func (s *composeService) Restart(ctx context.Context, projectName string, options api.RestartOptions) error {
  24. return Run(ctx, func(ctx context.Context) error {
  25. return s.restart(ctx, strings.ToLower(projectName), options)
  26. }, "restart", s.events)
  27. }
  28. func (s *composeService) restart(ctx context.Context, projectName string, options api.RestartOptions) error { //nolint:gocyclo
  29. containers, err := s.getContainers(ctx, projectName, oneOffExclude, true)
  30. if err != nil {
  31. return err
  32. }
  33. project := options.Project
  34. if project == nil {
  35. project, err = s.getProjectWithResources(ctx, containers, projectName)
  36. if err != nil {
  37. return err
  38. }
  39. }
  40. if options.NoDeps {
  41. project, err = project.WithSelectedServices(options.Services, types.IgnoreDependencies)
  42. if err != nil {
  43. return err
  44. }
  45. }
  46. // ignore depends_on relations which are not impacted by restarting service or not required
  47. project, err = project.WithServicesTransform(func(_ string, s types.ServiceConfig) (types.ServiceConfig, error) {
  48. for name, r := range s.DependsOn {
  49. if !r.Restart {
  50. delete(s.DependsOn, name)
  51. }
  52. }
  53. return s, nil
  54. })
  55. if err != nil {
  56. return err
  57. }
  58. if len(options.Services) != 0 {
  59. project, err = project.WithSelectedServices(options.Services, types.IncludeDependents)
  60. if err != nil {
  61. return err
  62. }
  63. }
  64. return InDependencyOrder(ctx, project, func(c context.Context, service string) error {
  65. config := project.Services[service]
  66. err = s.waitDependencies(ctx, project, service, config.DependsOn, containers, 0)
  67. if err != nil {
  68. return err
  69. }
  70. eg, ctx := errgroup.WithContext(ctx)
  71. for _, ctr := range containers.filter(isService(service)) {
  72. eg.Go(func() error {
  73. def := project.Services[service]
  74. for _, hook := range def.PreStop {
  75. err = s.runHook(ctx, ctr, def, hook, nil)
  76. if err != nil {
  77. return err
  78. }
  79. }
  80. eventName := getContainerProgressName(ctr)
  81. s.events.On(restartingEvent(eventName))
  82. timeout := utils.DurationSecondToInt(options.Timeout)
  83. err = s.apiClient().ContainerRestart(ctx, ctr.ID, container.StopOptions{Timeout: timeout})
  84. if err != nil {
  85. return err
  86. }
  87. s.events.On(startedEvent(eventName))
  88. for _, hook := range def.PostStart {
  89. err = s.runHook(ctx, ctr, def, hook, nil)
  90. if err != nil {
  91. return err
  92. }
  93. }
  94. return nil
  95. })
  96. }
  97. return eg.Wait()
  98. })
  99. }