Skip to content

Commit

Permalink
feat(conf): SIGINT (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
wsnotify authored Mar 21, 2020
1 parent f5010ec commit 8efd558
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 21 deletions.
6 changes: 5 additions & 1 deletion air_example.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Working directory
# . or absolute path, please note that the directories following must be under root.
root = "."
root = "."
tmp_dir = "tmp"

[build]
Expand All @@ -26,6 +26,10 @@ delay = 1000 # ms
stop_on_error = true
# This log file places in your tmp_dir.
log = "air_errors.log"
# Send Interrupt signal before killing process (windows does not support this feature)
send_interrupt = false
# Delay after sending Interrupt signal
kill_delay = 0

[log]
# Show log time
Expand Down
22 changes: 12 additions & 10 deletions runner/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,18 @@ type config struct {
}

type cfgBuild struct {
Cmd string `toml:"cmd"`
Bin string `toml:"bin"`
FullBin string `toml:"full_bin"`
Log string `toml:"log"`
IncludeExt []string `toml:"include_ext"`
ExcludeDir []string `toml:"exclude_dir"`
IncludeDir []string `toml:"include_dir"`
ExcludeFile []string `toml:"exclude_file"`
Delay int `toml:"delay"`
StopOnError bool `toml:"stop_on_error"`
Cmd string `toml:"cmd"`
Bin string `toml:"bin"`
FullBin string `toml:"full_bin"`
Log string `toml:"log"`
IncludeExt []string `toml:"include_ext"`
ExcludeDir []string `toml:"exclude_dir"`
IncludeDir []string `toml:"include_dir"`
ExcludeFile []string `toml:"exclude_file"`
Delay int `toml:"delay"`
StopOnError bool `toml:"stop_on_error"`
SendInterrupt bool `toml:"send_interrupt"`
KillDelay time.Duration `toml:"kill_delay"`
}

type cfgLog struct {
Expand Down
2 changes: 1 addition & 1 deletion runner/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ func (e *Engine) runBin() error {
}()

var err error
pid, err := killCmd(cmd)
pid, err := e.killCmd(cmd)
if err != nil {
e.mainDebug("failed to kill PID %d, error: %s", pid, err.Error())
if cmd.ProcessState != nil && !cmd.ProcessState.Exited() {
Expand Down
21 changes: 18 additions & 3 deletions runner/util_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,29 @@ import (
"io"
"os/exec"
"syscall"
"time"

"github.com/creack/pty"
)

func killCmd(cmd *exec.Cmd) (int, error) {
pid := cmd.Process.Pid
func (e *Engine) killCmd(cmd *exec.Cmd) (pid int, err error) {
pid = cmd.Process.Pid

if e.config.Build.SendInterrupt {
// Sending a signal to make it clear to the process that it is time to turn off
if err = syscall.Kill(-pid, syscall.SIGINT); err != nil {
return
}

time.Sleep(e.config.Build.KillDelay * time.Second)
}

// https://stackoverflow.com/questions/22470193/why-wont-go-kill-a-child-process-correctly
err := syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
err = syscall.Kill(-pid, syscall.SIGKILL)

// Wait releases any resources associated with the Process.
_, _ = cmd.Process.Wait()

return pid, err
}

Expand Down
19 changes: 15 additions & 4 deletions runner/util_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,30 @@ import (
"io"
"os/exec"
"syscall"
"time"

"github.com/creack/pty"
)

func killCmd(cmd *exec.Cmd) (int, error) {
pid := cmd.Process.Pid
func (e *Engine) killCmd(cmd *exec.Cmd) (pid int, err error) {
pid = cmd.Process.Pid

if e.config.Build.SendInterrupt {
// Sending a signal to make it clear to the process that it is time to turn off
if err = syscall.Kill(-pid, syscall.SIGINT); err != nil {
return
}

time.Sleep(e.config.Build.KillDelay * time.Second)
}

// https://stackoverflow.com/questions/22470193/why-wont-go-kill-a-child-process-correctly
err := syscall.Kill(-pid, syscall.SIGKILL)
err = syscall.Kill(-pid, syscall.SIGKILL)

// Wait releases any resources associated with the Process.
_, _ = cmd.Process.Wait()

return pid, err
return
}

func (e *Engine) startCmd(cmd string) (*exec.Cmd, io.ReadCloser, io.ReadCloser, error) {
Expand Down
5 changes: 3 additions & 2 deletions runner/util_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
"strings"
)

func killCmd(cmd *exec.Cmd) (int, error) {
pid := cmd.Process.Pid
func (e *Engine) killCmd(cmd *exec.Cmd) (pid int, err error) {
pid = cmd.Process.Pid

// https://stackoverflow.com/a/44551450
kill := exec.Command("TASKKILL", "/T", "/F", "/PID", strconv.Itoa(pid))
return pid, kill.Run()
Expand Down

0 comments on commit 8efd558

Please sign in to comment.