Handle errors and allow to send multiple kills if one failed

Signed-off-by: Jaime Soriano Pastor <jaime.soriano@elastic.co>
This commit is contained in:
Jaime Soriano Pastor 2024-04-12 19:37:35 +02:00 committed by Nicolas De loof
parent 5daed33c6a
commit 6a3501f901
1 changed files with 25 additions and 12 deletions

View File

@ -21,6 +21,7 @@ import (
"fmt"
"os"
"os/signal"
"sync/atomic"
"syscall"
"github.com/compose-spec/compose-go/v2/types"
@ -29,6 +30,7 @@ import (
"github.com/docker/compose/v2/internal/tracing"
"github.com/docker/compose/v2/pkg/api"
"github.com/docker/compose/v2/pkg/progress"
"github.com/docker/docker/errdefs"
"github.com/eiannone/keyboard"
"github.com/hashicorp/go-multierror"
"github.com/sirupsen/logrus"
@ -106,6 +108,9 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
}
}
}
// killRunning is used to control that we don't run more than one kill if signals are spammed.
var killRunning atomic.Bool
for {
select {
case <-doneCh:
@ -117,19 +122,27 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
case <-signalChan:
if first {
gracefulTeardown()
} else {
break
}
if !killRunning.CompareAndSwap(false, true) {
break
}
eg.Go(func() error {
defer killRunning.Store(false)
// Intentionally ignore errors, for cases where some
// of the containers are already stopped.
s.kill(context.Background(), project.Name, api.KillOptions{
err := s.kill(context.Background(), project.Name, api.KillOptions{
Services: options.Create.Services,
Project: project,
All: true,
})
return nil
})
// Ignore errors indicating that some of the containers were already stopped or removed.
if errdefs.IsNotFound(err) || errdefs.IsConflict(err) {
return nil
}
return err
})
case event := <-kEvents:
formatter.KeyboardManager.HandleKeyEvents(event, ctx, project, options)
}