Merge pull request #22566 from yongtang/22471-daemon-shutdown-timeout

Add config parameter to change per-container stop timeout during daemon shutdown
This commit is contained in:
Vincent Demeester 2016-10-18 14:33:09 +02:00 committed by GitHub
commit 3eeb334ccc
2 changed files with 20 additions and 6 deletions

View File

@ -13,7 +13,8 @@ import (
) )
type restartOptions struct { type restartOptions struct {
nSeconds int nSeconds int
nSecondsChanged bool
containers []string containers []string
} }
@ -28,6 +29,7 @@ func NewRestartCommand(dockerCli *command.DockerCli) *cobra.Command {
Args: cli.RequiresMinArgs(1), Args: cli.RequiresMinArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
opts.containers = args opts.containers = args
opts.nSecondsChanged = cmd.Flags().Changed("time")
return runRestart(dockerCli, &opts) return runRestart(dockerCli, &opts)
}, },
} }
@ -40,9 +42,14 @@ func NewRestartCommand(dockerCli *command.DockerCli) *cobra.Command {
func runRestart(dockerCli *command.DockerCli, opts *restartOptions) error { func runRestart(dockerCli *command.DockerCli, opts *restartOptions) error {
ctx := context.Background() ctx := context.Background()
var errs []string var errs []string
var timeout *time.Duration
if opts.nSecondsChanged {
timeoutValue := time.Duration(opts.nSeconds) * time.Second
timeout = &timeoutValue
}
for _, name := range opts.containers { for _, name := range opts.containers {
timeout := time.Duration(opts.nSeconds) * time.Second if err := dockerCli.Client().ContainerRestart(ctx, name, timeout); err != nil {
if err := dockerCli.Client().ContainerRestart(ctx, name, &timeout); err != nil {
errs = append(errs, err.Error()) errs = append(errs, err.Error())
} else { } else {
fmt.Fprintf(dockerCli.Out(), "%s\n", name) fmt.Fprintf(dockerCli.Out(), "%s\n", name)

View File

@ -13,7 +13,8 @@ import (
) )
type stopOptions struct { type stopOptions struct {
time int time int
timeChanged bool
containers []string containers []string
} }
@ -28,6 +29,7 @@ func NewStopCommand(dockerCli *command.DockerCli) *cobra.Command {
Args: cli.RequiresMinArgs(1), Args: cli.RequiresMinArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
opts.containers = args opts.containers = args
opts.timeChanged = cmd.Flags().Changed("time")
return runStop(dockerCli, &opts) return runStop(dockerCli, &opts)
}, },
} }
@ -39,12 +41,17 @@ func NewStopCommand(dockerCli *command.DockerCli) *cobra.Command {
func runStop(dockerCli *command.DockerCli, opts *stopOptions) error { func runStop(dockerCli *command.DockerCli, opts *stopOptions) error {
ctx := context.Background() ctx := context.Background()
timeout := time.Duration(opts.time) * time.Second
var timeout *time.Duration
if opts.timeChanged {
timeoutValue := time.Duration(opts.time) * time.Second
timeout = &timeoutValue
}
var errs []string var errs []string
errChan := parallelOperation(ctx, opts.containers, func(ctx context.Context, id string) error { errChan := parallelOperation(ctx, opts.containers, func(ctx context.Context, id string) error {
return dockerCli.Client().ContainerStop(ctx, id, &timeout) return dockerCli.Client().ContainerStop(ctx, id, timeout)
}) })
for _, container := range opts.containers { for _, container := range opts.containers {
if err := <-errChan; err != nil { if err := <-errChan; err != nil {