Merge pull request #1451 from docker/down_sigterm

This commit is contained in:
Nicolas De loof 2021-03-22 17:18:03 +01:00 committed by GitHub
commit 9f81314124
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 7 deletions

View File

@ -274,21 +274,26 @@ func runCreateStart(ctx context.Context, opts upOptions, services []string) erro
queue: queue, queue: queue,
} }
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
stopFunc := func() error { stopFunc := func() error {
ctx := context.Background() ctx := context.Background()
_, err := progress.Run(ctx, func(ctx context.Context) (string, error) { _, err := progress.Run(ctx, func(ctx context.Context) (string, error) {
go func() {
<-signalChan
c.ComposeService().Kill(ctx, project, compose.KillOptions{}) // nolint:errcheck
}()
return "", c.ComposeService().Stop(ctx, project, compose.StopOptions{}) return "", c.ComposeService().Stop(ctx, project, compose.StopOptions{})
}) })
return err return err
} }
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
go func() { go func() {
<-signalChan <-signalChan
queue <- compose.ContainerEvent{ queue <- compose.ContainerEvent{
Type: compose.UserCancel, Type: compose.UserCancel,
} }
fmt.Println("Gracefully stopping...") fmt.Println("Gracefully stopping... (press Ctrl+C again to force)")
stopFunc() // nolint:errcheck stopFunc() // nolint:errcheck
}() }()

View File

@ -83,7 +83,7 @@ func (s *composeService) attach(ctx context.Context, project *types.Project, lis
s.attachContainer(ctx, container, listener, project) // nolint: errcheck s.attachContainer(ctx, container, listener, project) // nolint: errcheck
delete(crashed, event.Container) delete(crashed, event.Container)
s.waitContainer(ctx, container, listener) s.waitContainer(container, listener)
} }
return nil return nil
}, },

View File

@ -51,14 +51,14 @@ func (s *composeService) Start(ctx context.Context, project *types.Project, opti
for _, c := range containers { for _, c := range containers {
c := c c := c
go func() { go func() {
s.waitContainer(ctx, c, options.Attach) s.waitContainer(c, options.Attach)
}() }()
} }
return nil return nil
} }
func (s *composeService) waitContainer(ctx context.Context, c moby.Container, listener compose.ContainerEventListener) { func (s *composeService) waitContainer(c moby.Container, listener compose.ContainerEventListener) {
statusC, errC := s.apiClient.ContainerWait(ctx, c.ID, container.WaitConditionNotRunning) statusC, errC := s.apiClient.ContainerWait(context.Background(), c.ID, container.WaitConditionNotRunning)
name := getContainerNameWithoutProject(c) name := getContainerNameWithoutProject(c)
select { select {
case status := <-statusC: case status := <-statusC: