mirror of
https://github.com/docker/compose.git
synced 2025-07-27 07:34:10 +02:00
Merge pull request #1852 from ulyssessouza/logs-follow-scale
This commit is contained in:
commit
7a4e111794
@ -193,11 +193,11 @@ func (s *ServiceProxy) Down(ctx context.Context, project string, options DownOpt
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Logs implements Service interface
|
// 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 {
|
if s.LogsFn == nil {
|
||||||
return ErrNotImplemented
|
return ErrNotImplemented
|
||||||
}
|
}
|
||||||
return s.LogsFn(ctx, project, consumer, options)
|
return s.LogsFn(ctx, projectName, consumer, options)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ps implements Service interface
|
// Ps implements Service interface
|
||||||
|
@ -20,31 +20,47 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/docker/compose-cli/pkg/api"
|
|
||||||
"github.com/docker/docker/api/types"
|
|
||||||
"github.com/docker/docker/pkg/stdcopy"
|
"github.com/docker/docker/pkg/stdcopy"
|
||||||
"golang.org/x/sync/errgroup"
|
"golang.org/x/sync/errgroup"
|
||||||
|
|
||||||
|
"github.com/docker/compose-cli/pkg/api"
|
||||||
"github.com/docker/compose-cli/pkg/utils"
|
"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 {
|
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...)
|
containers, err := s.getContainers(ctx, projectName, oneOffExclude, true, options.Services...)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
eg, ctx := errgroup.WithContext(ctx)
|
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 {
|
for _, c := range containers {
|
||||||
service := c.Labels[api.ServiceLabel]
|
c := c
|
||||||
container, err := s.apiClient.ContainerInspect(ctx, c.ID)
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
name := getContainerNameWithoutProject(c)
|
service := c.Labels[api.ServiceLabel]
|
||||||
eg.Go(func() error {
|
r, err := s.apiClient.ContainerLogs(ctx, cnt.ID, types.ContainerLogsOptions{
|
||||||
r, err := s.apiClient.ContainerLogs(ctx, container.ID, types.ContainerLogsOptions{
|
|
||||||
ShowStdout: true,
|
ShowStdout: true,
|
||||||
ShowStderr: true,
|
ShowStderr: true,
|
||||||
Follow: options.Follow,
|
Follow: options.Follow,
|
||||||
@ -58,16 +74,14 @@ func (s *composeService) Logs(ctx context.Context, projectName string, consumer
|
|||||||
}
|
}
|
||||||
defer r.Close() // nolint errcheck
|
defer r.Close() // nolint errcheck
|
||||||
|
|
||||||
|
name := getContainerNameWithoutProject(c)
|
||||||
w := utils.GetWriter(func(line string) {
|
w := utils.GetWriter(func(line string) {
|
||||||
consumer.Log(name, service, line)
|
consumer.Log(name, service, line)
|
||||||
})
|
})
|
||||||
if container.Config.Tty {
|
if cnt.Config.Tty {
|
||||||
_, err = io.Copy(w, r)
|
_, err = io.Copy(w, r)
|
||||||
} else {
|
} else {
|
||||||
_, err = stdcopy.StdCopy(w, w, r)
|
_, err = stdcopy.StdCopy(w, w, r)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
})
|
|
||||||
}
|
|
||||||
return eg.Wait()
|
|
||||||
}
|
}
|
||||||
|
@ -47,7 +47,7 @@ func (s *composeService) start(ctx context.Context, project *types.Project, opti
|
|||||||
}
|
}
|
||||||
|
|
||||||
eg.Go(func() error {
|
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)
|
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
|
type containerWatchFn func(container moby.Container) error
|
||||||
|
|
||||||
// watchContainers uses engine events to capture container start/die and notify ContainerEventListener
|
// 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{}
|
watched := map[string]int{}
|
||||||
for _, c := range containers {
|
for _, c := range containers {
|
||||||
watched[c.ID] = 0
|
watched[c.ID] = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, stop := context.WithCancel(context.Background())
|
ctx, stop := context.WithCancel(context.Background())
|
||||||
err := s.Events(ctx, project.Name, api.EventsOptions{
|
err := s.Events(ctx, projectName, api.EventsOptions{
|
||||||
Services: services,
|
Services: services,
|
||||||
Consumer: func(event api.Event) error {
|
Consumer: func(event api.Event) error {
|
||||||
inspected, err := s.apiClient.ContainerInspect(ctx, event.Container)
|
inspected, err := s.apiClient.ContainerInspect(ctx, event.Container)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user