From 0d7567131acb68c5d09da9c29ee81a92b7ea7ae6 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Thu, 2 Dec 2021 09:12:49 +0100 Subject: [PATCH] compose logs to notify printer about container lifecycle events Signed-off-by: Nicolas De Loof --- pkg/compose/logs.go | 44 +++++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/pkg/compose/logs.go b/pkg/compose/logs.go index 1b143a6b6..15aef3ec0 100644 --- a/pkg/compose/logs.go +++ b/pkg/compose/logs.go @@ -34,25 +34,43 @@ func (s *composeService) Logs(ctx context.Context, projectName string, consumer } eg, ctx := errgroup.WithContext(ctx) - if options.Follow { - printer := newLogPrinter(consumer) - eg.Go(func() error { - return s.watchContainers(ctx, projectName, options.Services, printer.HandleEvent, containers, func(c types.Container) error { - return s.logContainers(ctx, consumer, c, options) - }) - }) - eg.Go(func() error { - _, err := printer.Run(ctx, false, "", nil) - return err - }) - } - for _, c := range containers { c := c eg.Go(func() error { return s.logContainers(ctx, consumer, c, options) }) } + + if options.Follow { + printer := newLogPrinter(consumer) + eg.Go(func() error { + for _, c := range containers { + printer.HandleEvent(api.ContainerEvent{ + Type: api.ContainerEventAttach, + Container: getContainerNameWithoutProject(c), + Service: c.Labels[api.ServiceLabel], + }) + } + return nil + }) + + eg.Go(func() error { + return s.watchContainers(ctx, projectName, options.Services, printer.HandleEvent, containers, func(c types.Container) error { + printer.HandleEvent(api.ContainerEvent{ + Type: api.ContainerEventAttach, + Container: getContainerNameWithoutProject(c), + Service: c.Labels[api.ServiceLabel], + }) + return s.logContainers(ctx, consumer, c, options) + }) + }) + + eg.Go(func() error { + _, err := printer.Run(ctx, false, "", nil) + return err + }) + } + return eg.Wait() }