cli: fix timeout behavior on up / restart / stop (#10672)

Do not set a hardcoded default timeout of 10 seconds when restarting / stopping but use the container `StopTimeout` (defaults to 10 seconds).

Also fixed custom timeout not used when invoking `up`.

Signed-off-by: Robbert Segeren <robbert.segeren@easyflex.nl>
This commit is contained in:
robbert-ef 2023-06-12 15:18:25 +02:00 committed by GitHub
parent 508d71c5df
commit 68bd0eb523
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 26 additions and 17 deletions

View File

@ -63,7 +63,7 @@ func downCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
flags := downCmd.Flags() flags := downCmd.Flags()
removeOrphans := utils.StringToBool(os.Getenv(ComposeRemoveOrphans)) removeOrphans := utils.StringToBool(os.Getenv(ComposeRemoveOrphans))
flags.BoolVar(&opts.removeOrphans, "remove-orphans", removeOrphans, "Remove containers for services not defined in the Compose file.") flags.BoolVar(&opts.removeOrphans, "remove-orphans", removeOrphans, "Remove containers for services not defined in the Compose file.")
flags.IntVarP(&opts.timeout, "timeout", "t", 10, "Specify a shutdown timeout in seconds") flags.IntVarP(&opts.timeout, "timeout", "t", 0, "Specify a shutdown timeout in seconds")
flags.BoolVarP(&opts.volumes, "volumes", "v", false, `Remove named volumes declared in the "volumes" section of the Compose file and anonymous volumes attached to containers.`) flags.BoolVarP(&opts.volumes, "volumes", "v", false, `Remove named volumes declared in the "volumes" section of the Compose file and anonymous volumes attached to containers.`)
flags.StringVar(&opts.images, "rmi", "", `Remove images used by services. "local" remove only images that don't have a custom tag ("local"|"all")`) flags.StringVar(&opts.images, "rmi", "", `Remove images used by services. "local" remove only images that don't have a custom tag ("local"|"all")`)
flags.SetNormalizeFunc(func(f *pflag.FlagSet, name string) pflag.NormalizedName { flags.SetNormalizeFunc(func(f *pflag.FlagSet, name string) pflag.NormalizedName {

View File

@ -28,8 +28,9 @@ import (
type restartOptions struct { type restartOptions struct {
*ProjectOptions *ProjectOptions
timeout int timeChanged bool
noDeps bool timeout int
noDeps bool
} }
func restartCommand(p *ProjectOptions, backend api.Service) *cobra.Command { func restartCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
@ -39,13 +40,16 @@ func restartCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
restartCmd := &cobra.Command{ restartCmd := &cobra.Command{
Use: "restart [OPTIONS] [SERVICE...]", Use: "restart [OPTIONS] [SERVICE...]",
Short: "Restart service containers", Short: "Restart service containers",
PreRun: func(cmd *cobra.Command, args []string) {
opts.timeChanged = cmd.Flags().Changed("timeout")
},
RunE: Adapt(func(ctx context.Context, args []string) error { RunE: Adapt(func(ctx context.Context, args []string) error {
return runRestart(ctx, backend, opts, args) return runRestart(ctx, backend, opts, args)
}), }),
ValidArgsFunction: completeServiceNames(p), ValidArgsFunction: completeServiceNames(p),
} }
flags := restartCmd.Flags() flags := restartCmd.Flags()
flags.IntVarP(&opts.timeout, "timeout", "t", 10, "Specify a shutdown timeout in seconds") flags.IntVarP(&opts.timeout, "timeout", "t", 0, "Specify a shutdown timeout in seconds")
flags.BoolVar(&opts.noDeps, "no-deps", false, "Don't restart dependent services.") flags.BoolVar(&opts.noDeps, "no-deps", false, "Don't restart dependent services.")
return restartCmd return restartCmd
@ -57,6 +61,12 @@ func runRestart(ctx context.Context, backend api.Service, opts restartOptions, s
return err return err
} }
var timeout *time.Duration
if opts.timeChanged {
timeoutValue := time.Duration(opts.timeout) * time.Second
timeout = &timeoutValue
}
if opts.noDeps { if opts.noDeps {
err := project.ForServices(services, types.IgnoreDependencies) err := project.ForServices(services, types.IgnoreDependencies)
if err != nil { if err != nil {
@ -64,9 +74,8 @@ func runRestart(ctx context.Context, backend api.Service, opts restartOptions, s
} }
} }
timeout := time.Duration(opts.timeout) * time.Second
return backend.Restart(ctx, name, api.RestartOptions{ return backend.Restart(ctx, name, api.RestartOptions{
Timeout: &timeout, Timeout: timeout,
Services: services, Services: services,
Project: project, Project: project,
}) })

View File

@ -47,7 +47,7 @@ func stopCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
ValidArgsFunction: completeServiceNames(p), ValidArgsFunction: completeServiceNames(p),
} }
flags := cmd.Flags() flags := cmd.Flags()
flags.IntVarP(&opts.timeout, "timeout", "t", 10, "Specify a shutdown timeout in seconds") flags.IntVarP(&opts.timeout, "timeout", "t", 0, "Specify a shutdown timeout in seconds")
return cmd return cmd
} }

View File

@ -78,7 +78,7 @@ func upCommand(p *ProjectOptions, streams api.Streams, backend api.Service) *cob
Short: "Create and start containers", Short: "Create and start containers",
PreRunE: AdaptCmd(func(ctx context.Context, cmd *cobra.Command, args []string) error { PreRunE: AdaptCmd(func(ctx context.Context, cmd *cobra.Command, args []string) error {
create.pullChanged = cmd.Flags().Changed("pull") create.pullChanged = cmd.Flags().Changed("pull")
create.timeChanged = cmd.Flags().Changed("waitTimeout") create.timeChanged = cmd.Flags().Changed("timeout")
return validateFlags(&up, &create) return validateFlags(&up, &create)
}), }),
RunE: p.WithServices(func(ctx context.Context, project *types.Project, services []string) error { RunE: p.WithServices(func(ctx context.Context, project *types.Project, services []string) error {
@ -104,7 +104,7 @@ func upCommand(p *ProjectOptions, streams api.Streams, backend api.Service) *cob
flags.BoolVar(&up.noStart, "no-start", false, "Don't start the services after creating them.") flags.BoolVar(&up.noStart, "no-start", false, "Don't start the services after creating them.")
flags.BoolVar(&up.cascadeStop, "abort-on-container-exit", false, "Stops all containers if any container was stopped. Incompatible with -d") flags.BoolVar(&up.cascadeStop, "abort-on-container-exit", false, "Stops all containers if any container was stopped. Incompatible with -d")
flags.StringVar(&up.exitCodeFrom, "exit-code-from", "", "Return the exit code of the selected service container. Implies --abort-on-container-exit") flags.StringVar(&up.exitCodeFrom, "exit-code-from", "", "Return the exit code of the selected service container. Implies --abort-on-container-exit")
flags.IntVarP(&create.timeout, "timeout", "t", 10, "Use this timeout in seconds for container shutdown when attached or when containers are already running.") flags.IntVarP(&create.timeout, "timeout", "t", 0, "Use this timeout in seconds for container shutdown when attached or when containers are already running.")
flags.BoolVar(&up.timestamp, "timestamps", false, "Show timestamps.") flags.BoolVar(&up.timestamp, "timestamps", false, "Show timestamps.")
flags.BoolVar(&up.noDeps, "no-deps", false, "Don't start linked services.") flags.BoolVar(&up.noDeps, "no-deps", false, "Don't start linked services.")
flags.BoolVar(&create.recreateDeps, "always-recreate-deps", false, "Recreate dependent containers. Incompatible with --no-recreate.") flags.BoolVar(&create.recreateDeps, "always-recreate-deps", false, "Recreate dependent containers. Incompatible with --no-recreate.")

View File

@ -10,7 +10,7 @@ Stop and remove containers, networks
| `--dry-run` | | | Execute command in dry run mode | | `--dry-run` | | | Execute command in dry run mode |
| `--remove-orphans` | | | Remove containers for services not defined in the Compose file. | | `--remove-orphans` | | | Remove containers for services not defined in the Compose file. |
| `--rmi` | `string` | | Remove images used by services. "local" remove only images that don't have a custom tag ("local"\|"all") | | `--rmi` | `string` | | Remove images used by services. "local" remove only images that don't have a custom tag ("local"\|"all") |
| `-t`, `--timeout` | `int` | `10` | Specify a shutdown timeout in seconds | | `-t`, `--timeout` | `int` | `0` | Specify a shutdown timeout in seconds |
| `-v`, `--volumes` | | | Remove named volumes declared in the "volumes" section of the Compose file and anonymous volumes attached to containers. | | `-v`, `--volumes` | | | Remove named volumes declared in the "volumes" section of the Compose file and anonymous volumes attached to containers. |

View File

@ -9,7 +9,7 @@ Restart service containers
|:------------------|:------|:--------|:--------------------------------------| |:------------------|:------|:--------|:--------------------------------------|
| `--dry-run` | | | Execute command in dry run mode | | `--dry-run` | | | Execute command in dry run mode |
| `--no-deps` | | | Don't restart dependent services. | | `--no-deps` | | | Don't restart dependent services. |
| `-t`, `--timeout` | `int` | `10` | Specify a shutdown timeout in seconds | | `-t`, `--timeout` | `int` | `0` | Specify a shutdown timeout in seconds |
<!---MARKER_GEN_END--> <!---MARKER_GEN_END-->

View File

@ -8,7 +8,7 @@ Stop services
| Name | Type | Default | Description | | Name | Type | Default | Description |
|:------------------|:------|:--------|:--------------------------------------| |:------------------|:------|:--------|:--------------------------------------|
| `--dry-run` | | | Execute command in dry run mode | | `--dry-run` | | | Execute command in dry run mode |
| `-t`, `--timeout` | `int` | `10` | Specify a shutdown timeout in seconds | | `-t`, `--timeout` | `int` | `0` | Specify a shutdown timeout in seconds |
<!---MARKER_GEN_END--> <!---MARKER_GEN_END-->

View File

@ -28,7 +28,7 @@ Create and start containers
| `--remove-orphans` | | | Remove containers for services not defined in the Compose file. | | `--remove-orphans` | | | Remove containers for services not defined in the Compose file. |
| `-V`, `--renew-anon-volumes` | | | Recreate anonymous volumes instead of retrieving data from the previous containers. | | `-V`, `--renew-anon-volumes` | | | Recreate anonymous volumes instead of retrieving data from the previous containers. |
| `--scale` | `stringArray` | | Scale SERVICE to NUM instances. Overrides the `scale` setting in the Compose file if present. | | `--scale` | `stringArray` | | Scale SERVICE to NUM instances. Overrides the `scale` setting in the Compose file if present. |
| `-t`, `--timeout` | `int` | `10` | Use this timeout in seconds for container shutdown when attached or when containers are already running. | | `-t`, `--timeout` | `int` | `0` | Use this timeout in seconds for container shutdown when attached or when containers are already running. |
| `--timestamps` | | | Show timestamps. | | `--timestamps` | | | Show timestamps. |
| `--wait` | | | Wait for services to be running\|healthy. Implies detached mode. | | `--wait` | | | Wait for services to be running\|healthy. Implies detached mode. |
| `--wait-timeout` | `int` | `0` | timeout waiting for application to be running\|healthy. | | `--wait-timeout` | `int` | `0` | timeout waiting for application to be running\|healthy. |

View File

@ -41,7 +41,7 @@ options:
- option: timeout - option: timeout
shorthand: t shorthand: t
value_type: int value_type: int
default_value: "10" default_value: "0"
description: Specify a shutdown timeout in seconds description: Specify a shutdown timeout in seconds
deprecated: false deprecated: false
hidden: false hidden: false

View File

@ -28,7 +28,7 @@ options:
- option: timeout - option: timeout
shorthand: t shorthand: t
value_type: int value_type: int
default_value: "10" default_value: "0"
description: Specify a shutdown timeout in seconds description: Specify a shutdown timeout in seconds
deprecated: false deprecated: false
hidden: false hidden: false

View File

@ -9,7 +9,7 @@ options:
- option: timeout - option: timeout
shorthand: t shorthand: t
value_type: int value_type: int
default_value: "10" default_value: "0"
description: Specify a shutdown timeout in seconds description: Specify a shutdown timeout in seconds
deprecated: false deprecated: false
hidden: false hidden: false

View File

@ -234,7 +234,7 @@ options:
- option: timeout - option: timeout
shorthand: t shorthand: t
value_type: int value_type: int
default_value: "10" default_value: "0"
description: | description: |
Use this timeout in seconds for container shutdown when attached or when containers are already running. Use this timeout in seconds for container shutdown when attached or when containers are already running.
deprecated: false deprecated: false