Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
implement Stop signal across platforms
  • Loading branch information
pingqixing committed Dec 27, 2019
1 parent 1f67a51 commit 6361fac
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 17 deletions.
43 changes: 26 additions & 17 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ import (
"sync"
"syscall"
"time"

"github.com/ShinyTrinkets/overseer/signals"
)

const defaultDelayStart uint = 25
Expand Down Expand Up @@ -308,7 +310,14 @@ func (c *Cmd) Stop() error {
// Signal the process group (-pid), not just the process, so that the process
// and all its children are signaled. Else, child procs can keep running and
// keep the stdout/stderr fd open and cause cmd.Wait to hang.
return sendSignal(-c.status.PID, syscall.SIGTERM)
// return sendSignal(-c.status.PID, syscall.SIGTERM)

proc, err := os.FindProcess(c.status.PID)
if err != nil {
return err
}

return signals.Kill(proc, syscall.SIGTERM, true)
}

// Signal sends OS signal to the process group.
Expand All @@ -325,24 +334,24 @@ func (c *Cmd) Signal(sig syscall.Signal) error {
}

// Signal the process group (-pid)
return sendSignal(-c.status.PID, sig)
// return sendSignal(-c.status.PID, sig)
return signals.SendSignal(-c.status.PID, sig)
}

func sendSignal(pid int, sig syscall.Signal) error {
proc, err := os.FindProcess(pid)

if err != nil {
return err
}

err = proc.Signal(sig)

if err != nil {
return err
}

return nil
}
// func sendSignal(pid int, sig syscall.Signal) error {
// proc, err := os.FindProcess(pid)
// if err != nil {
// return err
// }
//
// err = proc.Signal(sig)
//
// if err != nil {
// return err
// }
//
// return nil
// }

// Status returns the Status of the command at any time. It is safe to call
// concurrently by multiple goroutines.
Expand Down
4 changes: 4 additions & 0 deletions manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,10 @@ func (ovr *Overseer) Supervise(id string) int {
break
}

if c.State == FINISHED {
break
}

if ovr.IsStopping() {
log.Info("Overseer is stopping", Attrs{"f": "Supervise"})
break
Expand Down
32 changes: 32 additions & 0 deletions signals/signal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// +build !windows

package signals

import (
"os"
"syscall"
)

func SendSignal(pid int, sig syscall.Signal) error {
proc, err := os.FindProcess(-pid)
if err != nil {
return err
}

err = proc.Signal(sig)

if err != nil {
return err
}

return nil
}

func Kill(process *os.Process, sig os.Signal, sigChildren bool) error {
localSig := sig.(syscall.Signal)
pid := process.Pid
if sigChildren {
pid = -pid
}
return syscall.Kill(pid, localSig)
}
36 changes: 36 additions & 0 deletions signals/signal_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// +build windows

package signals

import (
"fmt"
"os"
"os/exec"
"syscall"
)

func SendSignal(pid int, sig syscall.Signal) error {
proc, err := os.FindProcess(-pid)
if err != nil {
return err
}

err = proc.Signal(sig)

if err != nil {
return err
}

return nil
}

func Kill(process *os.Process, sig os.Signal, sigChildren bool) error {
// Signal command can't kill children processes, call taskkill command to kill them
cmd := exec.Command("taskkill", "/F", "/T", "/PID", fmt.Sprintf("%d", process.Pid))
err := cmd.Start()
if err == nil {
return cmd.Wait()
}
// if fail to find taskkill, fallback to normal signal
return process.Signal(sig)
}

0 comments on commit 6361fac

Please sign in to comment.