Skip to content

Commit b6248b6

Browse files
committed
feat(compose): emit and handle container healthy events
Signed-off-by: Mark Jagyo <mark.jagyo@gmail.com>
1 parent 25bfaf5 commit b6248b6

4 files changed

Lines changed: 20 additions & 10 deletions

File tree

pkg/api/api.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,8 @@ const (
746746
ContainerEventRecreated
747747
// ContainerEventExited is a ContainerEvent of type exit. ExitCode is set
748748
ContainerEventExited
749+
// ContainerEventHealthy let consumer know container is healthy
750+
ContainerEventHealthy
749751
// UserCancel user canceled compose up, we are stopping containers
750752
HookEventLog
751753
)

pkg/compose/monitor.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@ func (c *monitor) Start(ctx context.Context) error {
164164
}
165165
containers.Remove(ctr.ID)
166166
}
167+
case events.ActionHealthStatusHealthy:
168+
logrus.Debugf("container %s healthy", ctr.Name)
169+
for _, listener := range c.listeners {
170+
listener(newContainerEvent(event.TimeNano, ctr, api.ContainerEventHealthy))
171+
}
167172
}
168173
}
169174
}

pkg/compose/start.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,6 @@ func (s *composeService) start(ctx context.Context, projectName string, options
9595
}
9696
return err
9797
}
98-
99-
if options.Log {
100-
s.Logs(ctx, projectName, options.Attach, api.LogOptions{
101-
Project: options.Project,
102-
Services: options.Services,
103-
Follow: false,
104-
Tail: "all",
105-
Timestamps: false,
106-
})
107-
}
10898
}
10999

110100
return nil

pkg/compose/up.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,11 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
198198
}
199199
monitor.withListener(printer.HandleEvent)
200200

201+
monitor.withListener(onHealthy(func(e api.ContainerEvent) {
202+
fmt.Println("Healthy:", e.Service, e.Container.Name)
203+
cancel()
204+
}))
205+
201206
var exitCode int
202207
if options.Start.OnExit != api.CascadeIgnore {
203208
once := true
@@ -301,3 +306,11 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
301306
}
302307
return err
303308
}
309+
310+
func onHealthy(fn func(api.ContainerEvent)) api.ContainerEventListener {
311+
return func(e api.ContainerEvent) {
312+
if e.Type == api.ContainerEventHealthy {
313+
fn(e)
314+
}
315+
}
316+
}

0 commit comments

Comments
 (0)