Skip to content

Commit

Permalink
compose logs to notify printer about container lifecycle events
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <[email protected]>
  • Loading branch information
ndeloof committed Dec 2, 2021
1 parent 7b84f2c commit 0d75671
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions pkg/compose/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,43 @@ func (s *composeService) Logs(ctx context.Context, projectName string, consumer
}

eg, ctx := errgroup.WithContext(ctx)
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
})
}

for _, c := range containers {
c := c
eg.Go(func() error {
return s.logContainers(ctx, consumer, c, options)
})
}
return eg.Wait()
}

Expand Down

0 comments on commit 0d75671

Please sign in to comment.