Skip to content

Commit

Permalink
fix: handles SIGINT to terminate process on KeyboardInterrupt
Browse files Browse the repository at this point in the history
  • Loading branch information
nxtcoder17 committed Dec 10, 2024
1 parent a3df5e2 commit 8465e39
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ func main() {

ex := executor.NewExecutor(executor.ExecutorArgs{
Logger: logger,
Command: func(context.Context) *exec.Cmd {
cmd := exec.Command(execCmd, execArgs...)
Command: func(ctx context.Context) *exec.Cmd {
cmd := exec.CommandContext(ctx, execCmd, execArgs...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
Expand Down
5 changes: 2 additions & 3 deletions pkg/executor/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type ExecutorArgs struct {

func NewExecutor(args ExecutorArgs) *Executor {
done := make(chan os.Signal, 1)
signal.Notify(done, syscall.SIGTERM)
signal.Notify(done, syscall.SIGINT, syscall.SIGTERM)

if args.Logger == nil {
args.Logger = slog.Default()
Expand All @@ -51,8 +51,6 @@ func (ex *Executor) Exec() error {
ex.mu.Unlock()
}()

ex.logger.Debug("[exec] starting process")

ctx, cf := context.WithCancel(context.TODO())
defer cf()

Expand Down Expand Up @@ -82,6 +80,7 @@ func (ex *Executor) Exec() error {
}
}()

ex.logger.Debug("[exec:post] process running")
if err := cmd.Wait(); err != nil {
if strings.HasPrefix(err.Error(), "signal:") {
ex.logger.Debug("wait terminated, received", "signal", err.Error())
Expand Down

0 comments on commit 8465e39

Please sign in to comment.