capture exit code and log as ERROR

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2021-02-09 12:40:31 +01:00
parent 06b033db6c
commit 17c26e81ff
1 changed files with 23 additions and 7 deletions

View File

@ -20,6 +20,7 @@ import (
"context"
"errors"
"fmt"
"github.com/sirupsen/logrus"
"os"
"path/filepath"
@ -152,14 +153,15 @@ func runCreateStart(ctx context.Context, opts upOptions, services []string) erro
ctx, cancel := context.WithCancel(ctx)
listener := make(chan compose.ContainerExited)
exitCode := make(chan int)
go func() {
var aborting bool
for {
<-listener
exit := <-listener
if opts.cascadeStop && !aborting {
aborting = true
fmt.Println("Aborting on container exit...")
cancel()
exitCode <- exit.Status
}
}
}()
@ -170,15 +172,29 @@ func runCreateStart(ctx context.Context, opts upOptions, services []string) erro
})
if errors.Is(ctx.Err(), context.Canceled) {
fmt.Println("Gracefully stopping...")
ctx = context.Background()
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
return "", c.ComposeService().Stop(ctx, project)
})
select {
case exit := <-exitCode:
fmt.Println("Aborting on container exit...")
err = stop(c, project)
logrus.Error(exit)
// os.Exit(exit)
default:
// cancelled by user
fmt.Println("Gracefully stopping...")
err = stop(c, project)
}
}
return err
}
func stop(c *client.Client, project *types.Project) error {
ctx := context.Background()
_, err := progress.Run(ctx, func(ctx context.Context) (string, error) {
return "", c.ComposeService().Stop(ctx, project)
})
return err
}
func setup(ctx context.Context, opts composeOptions, services []string) (*client.Client, *types.Project, error) {
c, err := client.NewWithDefaultLocalBackend(ctx)
if err != nil {