stop as all registered containers exited || cascadestop

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2021-02-12 11:37:33 +01:00
parent 15882d3503
commit 4e5734f6e9
1 changed files with 18 additions and 9 deletions

View File

@ -315,26 +315,35 @@ type printer struct {
func (p printer) run(ctx context.Context, cascadeStop bool, exitCodeFrom string, consumer compose.LogConsumer, stopFn func() error) (int, error) { //nolint:unparam func (p printer) run(ctx context.Context, cascadeStop bool, exitCodeFrom string, consumer compose.LogConsumer, stopFn func() error) (int, error) { //nolint:unparam
var aborting bool var aborting bool
var count int
for { for {
event := <-p.queue event := <-p.queue
switch event.Type { switch event.Type {
case compose.ContainerEventAttach: case compose.ContainerEventAttach:
consumer.Register(event.Service, event.Source) consumer.Register(event.Service, event.Source)
count++
case compose.ContainerEventExit: case compose.ContainerEventExit:
if !aborting { if !aborting {
consumer.Status(event.Service, event.Source, fmt.Sprintf("exited with code %d", event.ExitCode)) consumer.Status(event.Service, event.Source, fmt.Sprintf("exited with code %d", event.ExitCode))
} }
if cascadeStop && !aborting { if cascadeStop {
aborting = true if !aborting {
fmt.Println("Aborting on container exit...") aborting = true
err := stopFn() fmt.Println("Aborting on container exit...")
if err != nil { err := stopFn()
return 0, err if err != nil {
return 0, err
}
}
if exitCodeFrom == "" || exitCodeFrom == event.Service {
logrus.Error(event.ExitCode)
return event.ExitCode, nil
} }
} }
if exitCodeFrom == "" || exitCodeFrom == event.Service { count--
logrus.Error(event.ExitCode) if count == 0 {
return event.ExitCode, nil // Last container terminated, done
return 0, nil
} }
case compose.ContainerEventLog: case compose.ContainerEventLog:
if !aborting { if !aborting {