Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add --detach to docker scale #243

Merged
merged 2 commits into from
Jul 4, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions cli/command/service/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,28 @@ import (
"github.com/docker/docker/api/types"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

type scaleOptions struct {
detach bool
}

func newScaleCommand(dockerCli *command.DockerCli) *cobra.Command {
return &cobra.Command{
options := &scaleOptions{}

cmd := &cobra.Command{
Use: "scale SERVICE=REPLICAS [SERVICE=REPLICAS...]",
Short: "Scale one or multiple replicated services",
Args: scaleArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return runScale(dockerCli, args)
return runScale(dockerCli, cmd.Flags(), options, args)
},
}

flags := cmd.Flags()
addDetachFlag(flags, &options.detach)
return cmd
}

func scaleArgs(cmd *cobra.Command, args []string) error {
Expand All @@ -43,8 +54,10 @@ func scaleArgs(cmd *cobra.Command, args []string) error {
return nil
}

func runScale(dockerCli *command.DockerCli, args []string) error {
func runScale(dockerCli *command.DockerCli, flags *pflag.FlagSet, options *scaleOptions, args []string) error {
var errs []string
ctx := context.Background()

for _, arg := range args {
parts := strings.SplitN(arg, "=", 2)
serviceID, scaleStr := parts[0], parts[1]
Expand All @@ -56,20 +69,31 @@ func runScale(dockerCli *command.DockerCli, args []string) error {
continue
}

if err := runServiceScale(dockerCli, serviceID, scale); err != nil {
if err := runServiceScale(ctx, dockerCli, serviceID, scale); err != nil {
errs = append(errs, fmt.Sprintf("%s: %v", serviceID, err))
}

if options.detach {
continue
}

if err := waitOnService(ctx, dockerCli, serviceID, false); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will change the behavior to wait for the first service to scale before scaling the next one. Previously, they would all be scaled at the same time. I wonder if it's better to continue scaling them simultaneously, and show the progress for all services at the same time. On the other hand, this is a lot more work, and I'm not sure enough people try to scale multiple services at once for this to matter. Any thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I missed this. I think we should scale them all at once.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I agree it would be best to not change the existing behaviour

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vieux you'll be working on this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

errs = append(errs, fmt.Sprintf("%s: %v", serviceID, err))
}
}

if options.detach {
warnDetachDefault(dockerCli.Err(), dockerCli.Client().ClientVersion(), flags, "scaled")
}

if len(errs) == 0 {
return nil
}
return errors.Errorf(strings.Join(errs, "\n"))
}

func runServiceScale(dockerCli *command.DockerCli, serviceID string, scale uint64) error {
func runServiceScale(ctx context.Context, dockerCli *command.DockerCli, serviceID string, scale uint64) error {
client := dockerCli.Client()
ctx := context.Background()

service, _, err := client.ServiceInspectWithRaw(ctx, serviceID, types.ServiceInspectOptions{})
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion docs/reference/commandline/service_scale.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ keywords: "service, scale"
# service scale

```markdown
Usage: docker service scale SERVICE=REPLICAS [SERVICE=REPLICAS...]
Usage: docker service scale [OPTIONS] SERVICE=REPLICAS [SERVICE=REPLICAS...]

Scale one or multiple replicated services

Options:
-d, --detach Exit immediately instead of waiting for the service to converge (default true)
--help Print usage
```

Expand Down