mirror of
https://github.com/docker/compose.git
synced 2025-07-25 14:44:29 +02:00
Introduce ergonomic API for handling multiple container events
Signed-off-by: Nikhil Benesch <nikhil.benesch@gmail.com>
This commit is contained in:
parent
5eb314a4ca
commit
ee586e7f1e
@ -261,6 +261,14 @@ func getContainerProgressName(container moby.Container) string {
|
|||||||
return "Container " + getCanonicalContainerName(container)
|
return "Container " + getCanonicalContainerName(container)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func containerEvents(containers Containers, eventFunc func(string) progress.Event) []progress.Event {
|
||||||
|
events := []progress.Event{}
|
||||||
|
for _, container := range containers {
|
||||||
|
events = append(events, eventFunc(getContainerProgressName(container)))
|
||||||
|
}
|
||||||
|
return events
|
||||||
|
}
|
||||||
|
|
||||||
// ServiceConditionRunningOrHealthy is a service condition on statys running or healthy
|
// ServiceConditionRunningOrHealthy is a service condition on statys running or healthy
|
||||||
const ServiceConditionRunningOrHealthy = "running_or_healthy"
|
const ServiceConditionRunningOrHealthy = "running_or_healthy"
|
||||||
|
|
||||||
@ -277,9 +285,7 @@ func (s *composeService) waitDependencies(ctx context.Context, project *types.Pr
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, container := range containers {
|
w.Events(containerEvents(containers, progress.Waiting))
|
||||||
w.Event(progress.Waiting(getContainerProgressName(container)))
|
|
||||||
}
|
|
||||||
|
|
||||||
dep, config := dep, config
|
dep, config := dep, config
|
||||||
eg.Go(func() error {
|
eg.Go(func() error {
|
||||||
@ -294,9 +300,7 @@ func (s *composeService) waitDependencies(ctx context.Context, project *types.Pr
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if healthy {
|
if healthy {
|
||||||
for _, container := range containers {
|
w.Events(containerEvents(containers, progress.Healthy))
|
||||||
w.Event(progress.Healthy(getContainerProgressName(container)))
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
case types.ServiceConditionHealthy:
|
case types.ServiceConditionHealthy:
|
||||||
@ -305,9 +309,7 @@ func (s *composeService) waitDependencies(ctx context.Context, project *types.Pr
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if healthy {
|
if healthy {
|
||||||
for _, container := range containers {
|
w.Events(containerEvents(containers, progress.Healthy))
|
||||||
w.Event(progress.Healthy(getContainerProgressName(container)))
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
case types.ServiceConditionCompletedSuccessfully:
|
case types.ServiceConditionCompletedSuccessfully:
|
||||||
@ -316,9 +318,7 @@ func (s *composeService) waitDependencies(ctx context.Context, project *types.Pr
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if exited {
|
if exited {
|
||||||
for _, container := range containers {
|
w.Events(containerEvents(containers, progress.Exited))
|
||||||
w.Event(progress.Exited(getContainerProgressName(container)))
|
|
||||||
}
|
|
||||||
if code != 0 {
|
if code != 0 {
|
||||||
return fmt.Errorf("service %q didn't completed successfully: exit %d", dep, code)
|
return fmt.Errorf("service %q didn't completed successfully: exit %d", dep, code)
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,10 @@ func (p *noopWriter) Start(ctx context.Context) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *noopWriter) Event(e Event) {
|
func (p *noopWriter) Event(Event) {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *noopWriter) Events([]Event) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *noopWriter) TailMsgf(_ string, _ ...interface{}) {
|
func (p *noopWriter) TailMsgf(_ string, _ ...interface{}) {
|
||||||
|
@ -40,6 +40,12 @@ func (p *plainWriter) Event(e Event) {
|
|||||||
fmt.Fprintln(p.out, e.ID, e.Text, e.StatusText)
|
fmt.Fprintln(p.out, e.ID, e.Text, e.StatusText)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *plainWriter) Events(events []Event) {
|
||||||
|
for _, e := range events {
|
||||||
|
p.Event(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (p *plainWriter) TailMsgf(m string, args ...interface{}) {
|
func (p *plainWriter) TailMsgf(m string, args ...interface{}) {
|
||||||
fmt.Fprintln(p.out, append([]interface{}{m}, args...)...)
|
fmt.Fprintln(p.out, append([]interface{}{m}, args...)...)
|
||||||
}
|
}
|
||||||
|
@ -95,6 +95,12 @@ func (w *ttyWriter) Event(e Event) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w *ttyWriter) Events(events []Event) {
|
||||||
|
for _, e := range events {
|
||||||
|
w.Event(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (w *ttyWriter) TailMsgf(msg string, args ...interface{}) {
|
func (w *ttyWriter) TailMsgf(msg string, args ...interface{}) {
|
||||||
w.mtx.Lock()
|
w.mtx.Lock()
|
||||||
defer w.mtx.Unlock()
|
defer w.mtx.Unlock()
|
||||||
|
@ -31,6 +31,7 @@ type Writer interface {
|
|||||||
Start(context.Context) error
|
Start(context.Context) error
|
||||||
Stop()
|
Stop()
|
||||||
Event(Event)
|
Event(Event)
|
||||||
|
Events([]Event)
|
||||||
TailMsgf(string, ...interface{})
|
TailMsgf(string, ...interface{})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user