Merge pull request #1852 from ulyssessouza/logs-follow-scale

This commit is contained in:
Nicolas De loof 2021-07-12 17:23:37 +02:00 committed by GitHub
commit 7a4e111794
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 76 additions and 62 deletions

View File

@ -193,11 +193,11 @@ func (s *ServiceProxy) Down(ctx context.Context, project string, options DownOpt
}
// Logs implements Service interface
func (s *ServiceProxy) Logs(ctx context.Context, project string, consumer LogConsumer, options LogOptions) error {
func (s *ServiceProxy) Logs(ctx context.Context, projectName string, consumer LogConsumer, options LogOptions) error {
if s.LogsFn == nil {
return ErrNotImplemented
}
return s.LogsFn(ctx, project, consumer, options)
return s.LogsFn(ctx, projectName, consumer, options)
}
// Ps implements Service interface

View File

@ -20,31 +20,47 @@ import (
"context"
"io"
"github.com/docker/compose-cli/pkg/api"
"github.com/docker/docker/api/types"
"github.com/docker/docker/pkg/stdcopy"
"golang.org/x/sync/errgroup"
"github.com/docker/compose-cli/pkg/api"
"github.com/docker/compose-cli/pkg/utils"
"github.com/docker/docker/api/types"
)
func (s *composeService) Logs(ctx context.Context, projectName string, consumer api.LogConsumer, options api.LogOptions) error {
containers, err := s.getContainers(ctx, projectName, oneOffExclude, true, options.Services...)
if err != nil {
return err
}
eg, ctx := errgroup.WithContext(ctx)
if options.Follow {
eg.Go(func() error {
printer := newLogPrinter(consumer)
return s.watchContainers(projectName, options.Services, printer.HandleEvent, containers, func(c types.Container) error {
return s.logContainers(ctx, consumer, c, options)
})
})
}
for _, c := range containers {
service := c.Labels[api.ServiceLabel]
container, err := s.apiClient.ContainerInspect(ctx, c.ID)
c := c
eg.Go(func() error {
return s.logContainers(ctx, consumer, c, options)
})
}
return eg.Wait()
}
func (s *composeService) logContainers(ctx context.Context, consumer api.LogConsumer, c types.Container, options api.LogOptions) error {
cnt, err := s.apiClient.ContainerInspect(ctx, c.ID)
if err != nil {
return err
}
name := getContainerNameWithoutProject(c)
eg.Go(func() error {
r, err := s.apiClient.ContainerLogs(ctx, container.ID, types.ContainerLogsOptions{
service := c.Labels[api.ServiceLabel]
r, err := s.apiClient.ContainerLogs(ctx, cnt.ID, types.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
Follow: options.Follow,
@ -58,16 +74,14 @@ func (s *composeService) Logs(ctx context.Context, projectName string, consumer
}
defer r.Close() // nolint errcheck
name := getContainerNameWithoutProject(c)
w := utils.GetWriter(func(line string) {
consumer.Log(name, service, line)
})
if container.Config.Tty {
if cnt.Config.Tty {
_, err = io.Copy(w, r)
} else {
_, err = stdcopy.StdCopy(w, w, r)
}
return err
})
}
return eg.Wait()
}

View File

@ -47,7 +47,7 @@ func (s *composeService) start(ctx context.Context, project *types.Project, opti
}
eg.Go(func() error {
return s.watchContainers(project, options.AttachTo, listener, attached, func(container moby.Container) error {
return s.watchContainers(project.Name, options.AttachTo, listener, attached, func(container moby.Container) error {
return s.attachContainer(ctx, container, listener, project)
})
})
@ -69,14 +69,14 @@ func (s *composeService) start(ctx context.Context, project *types.Project, opti
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 api.ContainerEventListener, containers Containers, onStart containerWatchFn) error {
func (s *composeService) watchContainers(projectName string, services []string, listener api.ContainerEventListener, containers Containers, onStart containerWatchFn) error {
watched := map[string]int{}
for _, c := range containers {
watched[c.ID] = 0
}
ctx, stop := context.WithCancel(context.Background())
err := s.Events(ctx, project.Name, api.EventsOptions{
err := s.Events(ctx, projectName, api.EventsOptions{
Services: services,
Consumer: func(event api.Event) error {
inspected, err := s.apiClient.ContainerInspect(ctx, event.Container)