pass timeout to "up" backend implementation

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2021-03-02 14:33:26 +01:00
parent fada87ca19
commit e9c9a1983c
No known key found for this signature in database
GPG Key ID: 9858809D6F8F6E7E
4 changed files with 33 additions and 29 deletions

View File

@ -77,6 +77,8 @@ type CreateOptions struct {
RecreateDependencies string RecreateDependencies string
// Inherit reuse anonymous volumes from previous container // Inherit reuse anonymous volumes from previous container
Inherit bool Inherit bool
// Timeout set delay to wait for container to gracelfuly stop before sending SIGKILL
Timeout *time.Duration
} }
// StartOptions group options of the Start API // StartOptions group options of the Start API

View File

@ -19,12 +19,6 @@ package compose
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/docker/compose-cli/api/client"
"github.com/docker/compose-cli/api/compose"
"github.com/docker/compose-cli/api/context/store"
"github.com/docker/compose-cli/api/progress"
"github.com/docker/compose-cli/cli/cmd"
"github.com/docker/compose-cli/cli/formatter"
"os" "os"
"os/signal" "os/signal"
"path/filepath" "path/filepath"
@ -37,6 +31,13 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
"github.com/docker/compose-cli/api/client"
"github.com/docker/compose-cli/api/compose"
"github.com/docker/compose-cli/api/context/store"
"github.com/docker/compose-cli/api/progress"
"github.com/docker/compose-cli/cli/cmd"
"github.com/docker/compose-cli/cli/formatter"
) )
// composeOptions hold options common to `up` and `run` to run compose project // composeOptions hold options common to `up` and `run` to run compose project
@ -68,26 +69,34 @@ type upOptions struct {
noInherit bool noInherit bool
} }
func (o upOptions) recreateStrategy() string { func (opts upOptions) recreateStrategy() string {
if o.noRecreate { if opts.noRecreate {
return compose.RecreateNever return compose.RecreateNever
} }
if o.forceRecreate { if opts.forceRecreate {
return compose.RecreateForce return compose.RecreateForce
} }
return compose.RecreateDiverged return compose.RecreateDiverged
} }
func (o upOptions) dependenciesRecreateStrategy() string { func (opts upOptions) dependenciesRecreateStrategy() string {
if o.noRecreate { if opts.noRecreate {
return compose.RecreateNever return compose.RecreateNever
} }
if o.recreateDeps { if opts.recreateDeps {
return compose.RecreateForce return compose.RecreateForce
} }
return compose.RecreateDiverged return compose.RecreateDiverged
} }
func (opts upOptions) GetTimeout() *time.Duration {
if opts.timeChanged {
t := time.Duration(opts.timeout) * time.Second
return &t
}
return nil
}
func (opts upOptions) apply(project *types.Project, services []string) error { func (opts upOptions) apply(project *types.Project, services []string) error {
if opts.noDeps { if opts.noDeps {
enabled, err := project.GetServices(services...) enabled, err := project.GetServices(services...)
@ -109,14 +118,6 @@ func (opts upOptions) apply(project *types.Project, services []string) error {
} }
} }
if opts.timeChanged {
timeoutValue := types.Duration(time.Duration(opts.timeout) * time.Second)
for i, s := range project.Services {
s.StopGracePeriod = &timeoutValue
project.Services[i] = s
}
}
for _, scale := range opts.scale { for _, scale := range opts.scale {
split := strings.Split(scale, "=") split := strings.Split(scale, "=")
if len(split) != 2 { if len(split) != 2 {
@ -235,6 +236,7 @@ func runCreateStart(ctx context.Context, opts upOptions, services []string) erro
Recreate: opts.recreateStrategy(), Recreate: opts.recreateStrategy(),
RecreateDependencies: opts.dependenciesRecreateStrategy(), RecreateDependencies: opts.dependenciesRecreateStrategy(),
Inherit: !opts.noInherit, Inherit: !opts.noInherit,
Timeout: opts.GetTimeout(),
}) })
if err != nil { if err != nil {
return "", err return "", err

View File

@ -42,7 +42,7 @@ const (
"Remove the custom name to scale the service.\n" "Remove the custom name to scale the service.\n"
) )
func (s *composeService) ensureScale(ctx context.Context, project *types.Project, service types.ServiceConfig) (*errgroup.Group, []moby.Container, error) { func (s *composeService) ensureScale(ctx context.Context, project *types.Project, service types.ServiceConfig, timeout *time.Duration) (*errgroup.Group, []moby.Container, error) {
cState, err := GetContextContainerState(ctx) cState, err := GetContextContainerState(ctx)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
@ -73,7 +73,7 @@ func (s *composeService) ensureScale(ctx context.Context, project *types.Project
for i := scale; i < len(actual); i++ { for i := scale; i < len(actual); i++ {
container := actual[i] container := actual[i]
eg.Go(func() error { eg.Go(func() error {
err := s.apiClient.ContainerStop(ctx, container.ID, nil) err := s.apiClient.ContainerStop(ctx, container.ID, timeout)
if err != nil { if err != nil {
return err return err
} }
@ -85,8 +85,8 @@ func (s *composeService) ensureScale(ctx context.Context, project *types.Project
return eg, actual, nil return eg, actual, nil
} }
func (s *composeService) ensureService(ctx context.Context, project *types.Project, service types.ServiceConfig, recreate string, inherit bool) error { func (s *composeService) ensureService(ctx context.Context, project *types.Project, service types.ServiceConfig, recreate string, inherit bool, timeout *time.Duration) error {
eg, actual, err := s.ensureScale(ctx, project, service) eg, actual, err := s.ensureScale(ctx, project, service, timeout)
if err != nil { if err != nil {
return err return err
} }
@ -106,7 +106,7 @@ func (s *composeService) ensureService(ctx context.Context, project *types.Proje
diverged := container.Labels[configHashLabel] != expected diverged := container.Labels[configHashLabel] != expected
if diverged || recreate == compose.RecreateForce || service.Extensions[extLifecycle] == forceRecreate { if diverged || recreate == compose.RecreateForce || service.Extensions[extLifecycle] == forceRecreate {
eg.Go(func() error { eg.Go(func() error {
return s.recreateContainer(ctx, project, service, container, inherit) return s.recreateContainer(ctx, project, service, container, inherit, timeout)
}) })
continue continue
} }
@ -209,10 +209,10 @@ func (s *composeService) createContainer(ctx context.Context, project *types.Pro
return nil return nil
} }
func (s *composeService) recreateContainer(ctx context.Context, project *types.Project, service types.ServiceConfig, container moby.Container, inherit bool) error { func (s *composeService) recreateContainer(ctx context.Context, project *types.Project, service types.ServiceConfig, container moby.Container, inherit bool, timeout *time.Duration) error {
w := progress.ContextWriter(ctx) w := progress.ContextWriter(ctx)
w.Event(progress.NewEvent(getContainerProgressName(container), progress.Working, "Recreate")) w.Event(progress.NewEvent(getContainerProgressName(container), progress.Working, "Recreate"))
err := s.apiClient.ContainerStop(ctx, container.ID, nil) err := s.apiClient.ContainerStop(ctx, container.ID, timeout)
if err != nil { if err != nil {
return err return err
} }

View File

@ -102,9 +102,9 @@ func (s *composeService) Create(ctx context.Context, project *types.Project, opt
return InDependencyOrder(ctx, project, func(c context.Context, service types.ServiceConfig) error { return InDependencyOrder(ctx, project, func(c context.Context, service types.ServiceConfig) error {
if contains(opts.Services, service.Name) { if contains(opts.Services, service.Name) {
return s.ensureService(c, project, service, opts.Recreate, opts.Inherit) return s.ensureService(c, project, service, opts.Recreate, opts.Inherit, opts.Timeout)
} }
return s.ensureService(c, project, service, opts.RecreateDependencies, opts.Inherit) return s.ensureService(c, project, service, opts.RecreateDependencies, opts.Inherit, opts.Timeout)
}) })
} }