exec: interrupt with delayed kill

This gives nix a chance to gracefully exit.
This commit is contained in:
Ophestra 2025-07-21 00:33:04 +09:00
parent e2299a57d2
commit 4787f51d84
Signed by: cat
SSH Key Fingerprint: SHA256:gQ67O0enBZ7UdZypgtspB2FDM1g3GVw8nX0XSdcFw8Q

11
exec.go
View File

@ -5,7 +5,13 @@ import (
"errors" "errors"
"io" "io"
"iter" "iter"
"os"
"os/exec" "os/exec"
"time"
)
const (
defaultWaitDelay = 15 * time.Second
) )
// Nix is the name of the nix program. // Nix is the name of the nix program.
@ -47,7 +53,10 @@ func New(ctx context.Context, extraArgs []string, stdout, stderr io.Writer) Cont
} }
func (n *nix) Nix(ctx context.Context, arg ...string) *exec.Cmd { func (n *nix) Nix(ctx context.Context, arg ...string) *exec.Cmd {
return exec.CommandContext(ctx, n.name, append(n.extra, arg...)...) cmd := exec.CommandContext(ctx, n.name, append(n.extra, arg...)...)
cmd.Cancel = func() error { return cmd.Process.Signal(os.Interrupt) }
cmd.WaitDelay = defaultWaitDelay
return cmd
} }
func (n *nix) WriteStdin(cmd *exec.Cmd, installables iter.Seq[string], f func() error) (int, error) { func (n *nix) WriteStdin(cmd *exec.Cmd, installables iter.Seq[string], f func() error) (int, error) {