interrupt printer when `compose log` is cancelled

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2021-11-15 08:48:31 +01:00 committed by Nicolas De loof
parent 413f46ade0
commit 7205d918ad
3 changed files with 47 additions and 41 deletions

View File

@ -42,7 +42,7 @@ func (s *composeService) Logs(ctx context.Context, projectName string, consumer
}) })
}) })
eg.Go(func() error { eg.Go(func() error {
_, err := printer.Run(false, "", nil) _, err := printer.Run(ctx, false, "", nil)
return err return err
}) })
} }

View File

@ -17,6 +17,7 @@
package compose package compose
import ( import (
"context"
"fmt" "fmt"
"github.com/docker/compose/v2/pkg/api" "github.com/docker/compose/v2/pkg/api"
@ -27,7 +28,7 @@ import (
// logPrinter watch application containers an collect their logs // logPrinter watch application containers an collect their logs
type logPrinter interface { type logPrinter interface {
HandleEvent(event api.ContainerEvent) HandleEvent(event api.ContainerEvent)
Run(cascadeStop bool, exitCodeFrom string, stopFn func() error) (int, error) Run(ctx context.Context, cascadeStop bool, exitCodeFrom string, stopFn func() error) (int, error)
Cancel() Cancel()
} }
@ -56,56 +57,61 @@ func (p *printer) HandleEvent(event api.ContainerEvent) {
p.queue <- event p.queue <- event
} }
func (p *printer) Run(cascadeStop bool, exitCodeFrom string, stopFn func() error) (int, error) { //nolint:gocyclo
func (p *printer) Run(ctx context.Context, cascadeStop bool, exitCodeFrom string, stopFn func() error) (int, error) {
var ( var (
aborting bool aborting bool
exitCode int exitCode int
) )
containers := map[string]struct{}{} containers := map[string]struct{}{}
for { for {
event := <-p.queue select {
container := event.Container case <-ctx.Done():
switch event.Type { return exitCode, ctx.Err()
case api.UserCancel: case event := <-p.queue:
aborting = true container := event.Container
case api.ContainerEventAttach: switch event.Type {
if _, ok := containers[container]; ok { case api.UserCancel:
continue aborting = true
} case api.ContainerEventAttach:
containers[container] = struct{}{} if _, ok := containers[container]; ok {
p.consumer.Register(container) continue
case api.ContainerEventExit: }
if !event.Restarting { containers[container] = struct{}{}
delete(containers, container) p.consumer.Register(container)
} case api.ContainerEventExit:
if !aborting { if !event.Restarting {
p.consumer.Status(container, fmt.Sprintf("exited with code %d", event.ExitCode)) delete(containers, container)
} }
if cascadeStop {
if !aborting { if !aborting {
aborting = true p.consumer.Status(container, fmt.Sprintf("exited with code %d", event.ExitCode))
fmt.Println("Aborting on container exit...") }
err := stopFn() if cascadeStop {
if err != nil { if !aborting {
return 0, err aborting = true
fmt.Println("Aborting on container exit...")
err := stopFn()
if err != nil {
return 0, err
}
}
if exitCodeFrom == "" {
exitCodeFrom = event.Service
}
if exitCodeFrom == event.Service {
logrus.Error(event.ExitCode)
exitCode = event.ExitCode
} }
} }
if exitCodeFrom == "" { if len(containers) == 0 {
exitCodeFrom = event.Service // Last container terminated, done
return exitCode, nil
} }
if exitCodeFrom == event.Service { case api.ContainerEventLog:
logrus.Error(event.ExitCode) if !aborting {
exitCode = event.ExitCode p.consumer.Log(container, event.Service, event.Line)
} }
} }
if len(containers) == 0 {
// Last container terminated, done
return exitCode, nil
}
case api.ContainerEventLog:
if !aborting {
p.consumer.Log(container, event.Service, event.Line)
}
} }
} }
} }

View File

@ -80,7 +80,7 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
var exitCode int var exitCode int
eg, ctx := errgroup.WithContext(ctx) eg, ctx := errgroup.WithContext(ctx)
eg.Go(func() error { eg.Go(func() error {
code, err := printer.Run(options.Start.CascadeStop, options.Start.ExitCodeFrom, stopFunc) code, err := printer.Run(context.Background(), options.Start.CascadeStop, options.Start.ExitCodeFrom, stopFunc)
exitCode = code exitCode = code
return err return err
}) })