mirror of https://github.com/docker/compose.git
Merge pull request #11292 from laurazard/update-cli-signal-handling
Up: teardown when command context is cancelled
This commit is contained in:
commit
06af729dc8
|
@ -42,6 +42,7 @@ import (
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
|
|
||||||
|
"github.com/docker/cli/cli-plugins/plugin"
|
||||||
"github.com/docker/compose/v2/cmd/formatter"
|
"github.com/docker/compose/v2/cmd/formatter"
|
||||||
"github.com/docker/compose/v2/pkg/api"
|
"github.com/docker/compose/v2/pkg/api"
|
||||||
"github.com/docker/compose/v2/pkg/compose"
|
"github.com/docker/compose/v2/pkg/compose"
|
||||||
|
@ -75,7 +76,7 @@ func AdaptCmd(fn CobraCommand) func(cmd *cobra.Command, args []string) error {
|
||||||
return func(cmd *cobra.Command, args []string) error {
|
return func(cmd *cobra.Command, args []string) error {
|
||||||
ctx := cmd.Context()
|
ctx := cmd.Context()
|
||||||
contextString := fmt.Sprintf("%s", ctx)
|
contextString := fmt.Sprintf("%s", ctx)
|
||||||
if !strings.HasSuffix(contextString, ".WithCancel") { // need to handle cancel
|
if !strings.Contains(contextString, ".WithCancel") || plugin.RunningStandalone() { // need to handle cancel
|
||||||
cancellableCtx, cancel := context.WithCancel(cmd.Context())
|
cancellableCtx, cancel := context.WithCancel(cmd.Context())
|
||||||
ctx = cancellableCtx
|
ctx = cancellableCtx
|
||||||
s := make(chan os.Signal, 1)
|
s := make(chan os.Signal, 1)
|
||||||
|
|
|
@ -31,7 +31,7 @@ import (
|
||||||
"github.com/hashicorp/go-multierror"
|
"github.com/hashicorp/go-multierror"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *composeService) Up(ctx context.Context, project *types.Project, options api.UpOptions) error {
|
func (s *composeService) Up(ctx context.Context, project *types.Project, options api.UpOptions) error { //nolint:gocyclo
|
||||||
err := progress.Run(ctx, tracing.SpanWrapFunc("project/up", tracing.ProjectOptions(project), func(ctx context.Context) error {
|
err := progress.Run(ctx, tracing.SpanWrapFunc("project/up", tracing.ProjectOptions(project), func(ctx context.Context) error {
|
||||||
err := s.create(ctx, project, options.Create)
|
err := s.create(ctx, project, options.Create)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -69,24 +69,31 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
|
||||||
doneCh := make(chan bool)
|
doneCh := make(chan bool)
|
||||||
eg.Go(func() error {
|
eg.Go(func() error {
|
||||||
first := true
|
first := true
|
||||||
|
gracefulTeardown := func() {
|
||||||
|
printer.Cancel()
|
||||||
|
fmt.Fprintln(s.stdinfo(), "Gracefully stopping... (press Ctrl+C again to force)")
|
||||||
|
eg.Go(func() error {
|
||||||
|
err := s.Stop(context.Background(), project.Name, api.StopOptions{
|
||||||
|
Services: options.Create.Services,
|
||||||
|
Project: project,
|
||||||
|
})
|
||||||
|
isTerminated = true
|
||||||
|
close(doneCh)
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
first = false
|
||||||
|
}
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-doneCh:
|
case <-doneCh:
|
||||||
return nil
|
return nil
|
||||||
|
case <-ctx.Done():
|
||||||
|
if first {
|
||||||
|
gracefulTeardown()
|
||||||
|
}
|
||||||
case <-signalChan:
|
case <-signalChan:
|
||||||
if first {
|
if first {
|
||||||
printer.Cancel()
|
gracefulTeardown()
|
||||||
fmt.Fprintln(s.stdinfo(), "Gracefully stopping... (press Ctrl+C again to force)")
|
|
||||||
eg.Go(func() error {
|
|
||||||
err := s.Stop(context.Background(), project.Name, api.StopOptions{
|
|
||||||
Services: options.Create.Services,
|
|
||||||
Project: project,
|
|
||||||
})
|
|
||||||
isTerminated = true
|
|
||||||
close(doneCh)
|
|
||||||
return err
|
|
||||||
})
|
|
||||||
first = false
|
|
||||||
} else {
|
} else {
|
||||||
eg.Go(func() error {
|
eg.Go(func() error {
|
||||||
return s.Kill(context.Background(), project.Name, api.KillOptions{
|
return s.Kill(context.Background(), project.Name, api.KillOptions{
|
||||||
|
|
Loading…
Reference in New Issue