mirror of https://github.com/docker/compose.git
capture exit code and log as ERROR
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
parent
06b033db6c
commit
17c26e81ff
|
@ -20,6 +20,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
@ -152,14 +153,15 @@ func runCreateStart(ctx context.Context, opts upOptions, services []string) erro
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(ctx)
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
listener := make(chan compose.ContainerExited)
|
listener := make(chan compose.ContainerExited)
|
||||||
|
exitCode := make(chan int)
|
||||||
go func() {
|
go func() {
|
||||||
var aborting bool
|
var aborting bool
|
||||||
for {
|
for {
|
||||||
<-listener
|
exit := <-listener
|
||||||
if opts.cascadeStop && !aborting {
|
if opts.cascadeStop && !aborting {
|
||||||
aborting = true
|
aborting = true
|
||||||
fmt.Println("Aborting on container exit...")
|
|
||||||
cancel()
|
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) {
|
if errors.Is(ctx.Err(), context.Canceled) {
|
||||||
fmt.Println("Gracefully stopping...")
|
select {
|
||||||
ctx = context.Background()
|
case exit := <-exitCode:
|
||||||
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
|
fmt.Println("Aborting on container exit...")
|
||||||
return "", c.ComposeService().Stop(ctx, project)
|
err = stop(c, project)
|
||||||
})
|
logrus.Error(exit)
|
||||||
|
// os.Exit(exit)
|
||||||
|
default:
|
||||||
|
// cancelled by user
|
||||||
|
fmt.Println("Gracefully stopping...")
|
||||||
|
err = stop(c, project)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return err
|
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) {
|
func setup(ctx context.Context, opts composeOptions, services []string) (*client.Client, *types.Project, error) {
|
||||||
c, err := client.NewWithDefaultLocalBackend(ctx)
|
c, err := client.NewWithDefaultLocalBackend(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue