Merge pull request #1765 from ndeloof/log_printer

This commit is contained in:
Nicolas De loof 2021-06-09 14:24:40 +02:00 committed by GitHub
commit 3202ab93bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 20 deletions

View File

@ -19,19 +19,21 @@ package compose
import (
"fmt"
"github.com/docker/compose-cli/api/compose"
"github.com/sirupsen/logrus"
)
// LogPrinter watch application containers an collect their logs
type LogPrinter interface {
HandleEvent(event ContainerEvent)
// logPrinter watch application containers an collect their logs
type logPrinter interface {
HandleEvent(event compose.ContainerEvent)
Run(cascadeStop bool, exitCodeFrom string, stopFn func() error) (int, error)
Cancel()
}
// NewLogPrinter builds a LogPrinter passing containers logs to LogConsumer
func NewLogPrinter(consumer LogConsumer) LogPrinter {
queue := make(chan ContainerEvent)
// newLogPrinter builds a LogPrinter passing containers logs to LogConsumer
func newLogPrinter(consumer compose.LogConsumer) logPrinter {
queue := make(chan compose.ContainerEvent)
printer := printer{
consumer: consumer,
queue: queue,
@ -40,17 +42,17 @@ func NewLogPrinter(consumer LogConsumer) LogPrinter {
}
func (p *printer) Cancel() {
p.queue <- ContainerEvent{
Type: UserCancel,
p.queue <- compose.ContainerEvent{
Type: compose.UserCancel,
}
}
type printer struct {
queue chan ContainerEvent
consumer LogConsumer
queue chan compose.ContainerEvent
consumer compose.LogConsumer
}
func (p *printer) HandleEvent(event ContainerEvent) {
func (p *printer) HandleEvent(event compose.ContainerEvent) {
p.queue <- event
}
@ -64,15 +66,15 @@ func (p *printer) Run(cascadeStop bool, exitCodeFrom string, stopFn func() error
event := <-p.queue
container := event.Container
switch event.Type {
case UserCancel:
case compose.UserCancel:
aborting = true
case ContainerEventAttach:
case compose.ContainerEventAttach:
if _, ok := containers[container]; ok {
continue
}
containers[container] = struct{}{}
p.consumer.Register(container)
case ContainerEventExit:
case compose.ContainerEventExit:
if !event.Restarting {
delete(containers, container)
}
@ -100,7 +102,7 @@ func (p *printer) Run(cascadeStop bool, exitCodeFrom string, stopFn func() error
// Last container terminated, done
return exitCode, nil
}
case ContainerEventLog:
case compose.ContainerEventLog:
if !aborting {
p.consumer.Log(container, event.Service, event.Line)
}

View File

@ -34,7 +34,7 @@ func (s *composeService) Start(ctx context.Context, project *types.Project, opti
})
}
func (s *composeService) start(ctx context.Context, project *types.Project, options compose.StartOptions, listener func(event compose.ContainerEvent)) error {
func (s *composeService) start(ctx context.Context, project *types.Project, options compose.StartOptions, listener compose.ContainerEventListener) error {
if len(options.AttachTo) == 0 {
options.AttachTo = project.ServiceNames()
}
@ -47,7 +47,9 @@ func (s *composeService) start(ctx context.Context, project *types.Project, opti
}
eg.Go(func() error {
return s.watchContainers(project, options.AttachTo, listener, attached)
return s.watchContainers(project, options.AttachTo, listener, attached, func(container moby.Container) error {
return s.attachContainer(ctx, container, listener, project)
})
})
}
@ -60,8 +62,10 @@ func (s *composeService) start(ctx context.Context, project *types.Project, opti
return eg.Wait()
}
type containerWatchFn func(container moby.Container) error
// watchContainers uses engine events to capture container start/die and notify ContainerEventListener
func (s *composeService) watchContainers(project *types.Project, services []string, listener compose.ContainerEventListener, containers Containers) error {
func (s *composeService) watchContainers(project *types.Project, services []string, listener compose.ContainerEventListener, containers Containers, onStart containerWatchFn) error {
watched := map[string]int{}
for _, c := range containers {
watched[c.ID] = 0
@ -118,7 +122,7 @@ func (s *composeService) watchContainers(project *types.Project, services []stri
}
if mustAttach {
// Container restarted, need to re-attach
err := s.attachContainer(ctx, container, listener, project)
err := onStart(container)
if err != nil {
return err
}

View File

@ -47,7 +47,7 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
return err
}
printer := compose.NewLogPrinter(options.Start.Attach)
printer := newLogPrinter(options.Start.Attach)
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)