Skip to content

Commit

Permalink
feat: cause on user-terminated context
Browse files Browse the repository at this point in the history
Signed-off-by: Alano Terblanche <[email protected]>
  • Loading branch information
Benehiko committed Dec 5, 2024
1 parent e7078bf commit 5f22178
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions cmd/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,43 @@ import (
"go.opentelemetry.io/otel"
)

var errCtxUserTerminated = errors.New("user terminated the process")

func main() {
err := dockerMain(context.Background())
if err != nil && !errdefs.IsCancelled(err) {
ctx := context.Background()
err := dockerMain(ctx)
if err != nil && !errdefs.IsCancelled(err) && !errors.Is(err, errCtxUserTerminated) {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(getExitCode(err))
}
}

func notifyContext(ctx context.Context, signals ...os.Signal) (context.Context, context.CancelFunc) {
ch := make(chan os.Signal, 1)
signal.Notify(ch, signals...)

ctx, cancel := context.WithCancelCause(ctx)

go func() {
select {
case <-ctx.Done():
signal.Stop(ch)
return
case <-ch:
cancel(errCtxUserTerminated)
signal.Stop(ch)
return
}
}()

return ctx, func() {
signal.Stop(ch)
cancel(nil)
}
}

func dockerMain(ctx context.Context) error {
ctx, cancelNotify := signal.NotifyContext(ctx, platformsignals.TerminationSignals...)
ctx, cancelNotify := notifyContext(ctx, platformsignals.TerminationSignals...)
defer cancelNotify()

dockerCli, err := command.NewDockerCli(command.WithBaseContext(ctx))
Expand Down

0 comments on commit 5f22178

Please sign in to comment.