-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
368 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
) | ||
|
||
type supervisor struct { | ||
prog string | ||
argv []string | ||
|
||
cmd *exec.Cmd | ||
startAt time.Time | ||
signaled bool | ||
hupped bool | ||
} | ||
|
||
var spawnInterval = 30 * time.Second | ||
|
||
func (sv *supervisor) launched() bool { | ||
return sv.cmd.Process != nil && time.Now().After(sv.startAt.Add(spawnInterval)) | ||
} | ||
|
||
func (sv *supervisor) buildCmd() *exec.Cmd { | ||
argv := append(sv.argv, "-child") | ||
cmd := exec.Command(sv.prog, argv...) | ||
cmd.Stderr = os.Stderr | ||
cmd.Stdout = os.Stdout | ||
return cmd | ||
} | ||
|
||
func (sv *supervisor) start() error { | ||
sv.hupped = false | ||
sv.cmd = sv.buildCmd() | ||
sv.startAt = time.Now() | ||
return sv.cmd.Start() | ||
} | ||
|
||
func (sv *supervisor) stop(sig os.Signal) error { | ||
sv.signaled = true | ||
return sv.cmd.Process.Signal(sig) | ||
} | ||
|
||
func (sv *supervisor) configtest() error { | ||
argv := append([]string{"configtest"}, sv.argv...) | ||
cmd := exec.Command(sv.prog, argv...) | ||
buf := &bytes.Buffer{} | ||
cmd.Stderr = buf | ||
err := cmd.Run() | ||
if err != nil { | ||
return fmt.Errorf("configtest failed: %s", buf.String()) | ||
} | ||
return nil | ||
} | ||
|
||
func (sv *supervisor) reload() error { | ||
err := sv.configtest() | ||
if err != nil { | ||
return err | ||
} | ||
sv.hupped = true | ||
return sv.cmd.Process.Signal(syscall.SIGTERM) | ||
} | ||
|
||
func (sv *supervisor) wait() (err error) { | ||
for { | ||
err = sv.cmd.Wait() | ||
if sv.signaled || (!sv.hupped && !sv.launched()) { | ||
break | ||
} | ||
sv.start() | ||
} | ||
return | ||
} | ||
|
||
func (sv *supervisor) handleSignal(ch <-chan os.Signal) { | ||
for sig := range ch { | ||
if sig == syscall.SIGHUP { | ||
err := sv.reload() | ||
if err != nil { | ||
logger.Warningf("failed to reload: %s", err.Error()) | ||
} | ||
} else { | ||
sv.stop(sig) | ||
} | ||
} | ||
} | ||
|
||
func (sv *supervisor) supervise() error { | ||
sv.start() | ||
c := make(chan os.Signal) | ||
signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGHUP) | ||
go sv.handleSignal(c) | ||
return sv.wait() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
// +build !windows | ||
|
||
package main | ||
|
||
import ( | ||
"os" | ||
"os/exec" | ||
"syscall" | ||
"testing" | ||
"time" | ||
) | ||
|
||
const stubAgent = "testdata/stub-agent" | ||
|
||
func init() { | ||
err := exec.Command("go", "build", "-o", stubAgent, "testdata/stub-agent.go").Run() | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func TestSupervisor(t *testing.T) { | ||
sv := &supervisor{ | ||
prog: stubAgent, | ||
argv: []string{"dummy"}, | ||
} | ||
sv.start() | ||
ch := make(chan os.Signal) | ||
go sv.handleSignal(ch) | ||
done := make(chan error) | ||
go func() { | ||
done <- sv.wait() | ||
}() | ||
pid := sv.cmd.Process.Pid | ||
if !existsPid(pid) { | ||
t.Errorf("process doesn't exist") | ||
} | ||
ch <- os.Interrupt | ||
|
||
err := <-done | ||
if err != nil { | ||
t.Errorf("error should be nil but: %s", err) | ||
} | ||
if existsPid(pid) { | ||
t.Errorf("child process isn't terminated") | ||
} | ||
} | ||
|
||
func TestSupervisor_reload(t *testing.T) { | ||
sv := &supervisor{ | ||
prog: stubAgent, | ||
argv: []string{"dummy"}, | ||
} | ||
sv.start() | ||
ch := make(chan os.Signal) | ||
go sv.handleSignal(ch) | ||
done := make(chan error) | ||
go func() { | ||
done <- sv.wait() | ||
}() | ||
oldPid := sv.cmd.Process.Pid | ||
if !existsPid(oldPid) { | ||
t.Errorf("process doesn't exist") | ||
} | ||
ch <- syscall.SIGHUP | ||
time.Sleep(200 * time.Millisecond) | ||
newPid := sv.cmd.Process.Pid | ||
if oldPid == newPid { | ||
t.Errorf("reload failed") | ||
} | ||
if existsPid(oldPid) { | ||
t.Errorf("old process isn't terminated") | ||
} | ||
if !existsPid(newPid) { | ||
t.Errorf("new process doesn't exist") | ||
} | ||
ch <- syscall.SIGTERM | ||
err := <-done | ||
if err != nil { | ||
t.Errorf("something went wrong") | ||
} | ||
if newPid != sv.cmd.Process.Pid { | ||
t.Errorf("something went wrong") | ||
} | ||
if existsPid(newPid) { | ||
t.Errorf("child process isn't terminated") | ||
} | ||
} | ||
|
||
func TestSupervisor_reloadFail(t *testing.T) { | ||
sv := &supervisor{ | ||
prog: stubAgent, | ||
argv: []string{"failed"}, | ||
} | ||
sv.start() | ||
ch := make(chan os.Signal) | ||
go sv.handleSignal(ch) | ||
done := make(chan error) | ||
go func() { | ||
done <- sv.wait() | ||
}() | ||
oldPid := sv.cmd.Process.Pid | ||
if !existsPid(oldPid) { | ||
t.Errorf("process doesn't exist") | ||
} | ||
ch <- syscall.SIGHUP | ||
time.Sleep(time.Second) | ||
newPid := sv.cmd.Process.Pid | ||
if oldPid != newPid { | ||
t.Errorf("reload should be failed, but unintentionally reloaded") | ||
} | ||
|
||
ch <- syscall.SIGTERM | ||
<-done | ||
} | ||
|
||
func TestSupervisor_launchFailed(t *testing.T) { | ||
sv := &supervisor{ | ||
prog: stubAgent, | ||
argv: []string{"launch failure"}, | ||
} | ||
sv.start() | ||
ch := make(chan os.Signal) | ||
go sv.handleSignal(ch) | ||
done := make(chan error) | ||
go func() { | ||
done <- sv.wait() | ||
}() | ||
pid := sv.cmd.Process.Pid | ||
if !existsPid(pid) { | ||
t.Errorf("process doesn't exist") | ||
} | ||
err := <-done | ||
if err == nil { | ||
t.Errorf("something went wrong") | ||
} | ||
if existsPid(sv.cmd.Process.Pid) { | ||
t.Errorf("child process isn't terminated") | ||
} | ||
} | ||
|
||
func TestSupervisor_crashRecovery(t *testing.T) { | ||
origSpawnInterval := spawnInterval | ||
spawnInterval = 300 * time.Millisecond | ||
defer func() { spawnInterval = origSpawnInterval }() | ||
|
||
sv := &supervisor{ | ||
prog: stubAgent, | ||
argv: []string{"blah blah blah"}, | ||
} | ||
sv.start() | ||
ch := make(chan os.Signal) | ||
go sv.handleSignal(ch) | ||
done := make(chan error) | ||
go func() { | ||
done <- sv.wait() | ||
}() | ||
oldPid := sv.cmd.Process.Pid | ||
if !existsPid(oldPid) { | ||
t.Errorf("process doesn't exist") | ||
} | ||
time.Sleep(spawnInterval) | ||
|
||
// let it crash | ||
sv.cmd.Process.Signal(syscall.SIGUSR1) | ||
|
||
time.Sleep(spawnInterval) | ||
newPid := sv.cmd.Process.Pid | ||
if oldPid == newPid { | ||
t.Errorf("crash recovery failed") | ||
} | ||
if existsPid(oldPid) { | ||
t.Errorf("old process isn't terminated") | ||
} | ||
if !existsPid(newPid) { | ||
t.Errorf("new process doesn't exist") | ||
} | ||
ch <- syscall.SIGTERM | ||
<-done | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
stub-agent |
Oops, something went wrong.