Merge pull request #1303 from docker/down_timeout

introduce timeout flag on down and stop
This commit is contained in:
Nicolas De loof 2021-02-16 10:59:02 +01:00 committed by GitHub
commit bd9248d074
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 126 additions and 29 deletions

View file

@ -18,6 +18,7 @@ package compose
import (
"context"
"time"
"github.com/compose-spec/compose-go/types"
@ -32,6 +33,8 @@ import (
type downOptions struct {
*projectOptions
removeOrphans bool
timeChanged bool
timeout int
}
func downCommand(p *projectOptions) *cobra.Command {
@ -42,11 +45,13 @@ func downCommand(p *projectOptions) *cobra.Command {
Use: "down",
Short: "Stop and remove containers, networks",
RunE: func(cmd *cobra.Command, args []string) error {
opts.timeChanged = cmd.Flags().Changed("timeout")
return runDown(cmd.Context(), opts)
},
}
flags := downCmd.Flags()
flags.BoolVar(&opts.removeOrphans, "remove-orphans", false, "Remove containers for services not defined in the Compose file.")
flags.IntVarP(&opts.timeout, "timeout", "t", 10, "Specify a shutdown timeout in seconds")
return downCmd
}
@ -69,9 +74,15 @@ func runDown(ctx context.Context, opts downOptions) error {
name = p.Name
}
var timeout *time.Duration
if opts.timeChanged {
timeoutValue := time.Duration(opts.timeout) * time.Second
timeout = &timeoutValue
}
return name, c.ComposeService().Down(ctx, name, compose.DownOptions{
RemoveOrphans: opts.removeOrphans,
Project: project,
Timeout: timeout,
})
})
return err

View file

@ -73,7 +73,7 @@ func runRemove(ctx context.Context, opts removeOptions, services []string) error
if opts.stop {
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
err := c.ComposeService().Stop(ctx, project)
err := c.ComposeService().Stop(ctx, project, compose.StopOptions{})
return "", err
})
if err != nil {

View file

@ -18,29 +18,37 @@ package compose
import (
"context"
"time"
"github.com/spf13/cobra"
"github.com/docker/compose-cli/api/client"
"github.com/docker/compose-cli/api/compose"
"github.com/docker/compose-cli/api/progress"
)
type stopOptions struct {
*projectOptions
timeChanged bool
timeout int
}
func stopCommand(p *projectOptions) *cobra.Command {
opts := stopOptions{
projectOptions: p,
}
stopCmd := &cobra.Command{
cmd := &cobra.Command{
Use: "stop [SERVICE...]",
Short: "Stop services",
RunE: func(cmd *cobra.Command, args []string) error {
opts.timeChanged = cmd.Flags().Changed("timeout")
return runStop(cmd.Context(), opts, args)
},
}
return stopCmd
flags := cmd.Flags()
flags.IntVarP(&opts.timeout, "timeout", "t", 10, "Specify a shutdown timeout in seconds")
return cmd
}
func runStop(ctx context.Context, opts stopOptions, services []string) error {
@ -54,8 +62,15 @@ func runStop(ctx context.Context, opts stopOptions, services []string) error {
return err
}
var timeout *time.Duration
if opts.timeChanged {
timeoutValue := time.Duration(opts.timeout) * time.Second
timeout = &timeoutValue
}
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
return "", c.ComposeService().Stop(ctx, project)
return "", c.ComposeService().Stop(ctx, project, compose.StopOptions{
Timeout: timeout,
})
})
return err
}

View file

@ -192,7 +192,7 @@ func runCreateStart(ctx context.Context, opts upOptions, services []string) erro
stopFunc := func() error {
ctx := context.Background()
_, err := progress.Run(ctx, func(ctx context.Context) (string, error) {
return "", c.ComposeService().Stop(ctx, project)
return "", c.ComposeService().Stop(ctx, project, compose.StopOptions{})
})
return err
}