Skip to content

Commit

Permalink
Merge pull request #41 from justenwalker/bug-40
Browse files Browse the repository at this point in the history
Refresh exec.Cmd for Health and OnChange
  • Loading branch information
tgross committed Dec 11, 2015
2 parents dd157e8 + f0b3bff commit f9a77ed
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/containerbuddy/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ func (b *BackendConfig) CheckForUpstreamChanges() bool {
return b.discoveryService.CheckForUpstreamChanges(b)
}

func (b *BackendConfig) OnChange() (int, error) {
exitCode, err := run(b.onChangeCmd)
// Reset command object - since it can't be reused
b.onChangeCmd = argsToCmd(b.onChangeCmd.Args)
return exitCode, err
}

func (s ServiceConfig) PollTime() int {
return s.Poll
}
Expand All @@ -80,6 +87,13 @@ func (s *ServiceConfig) Deregister() {
s.discoveryService.Deregister(s)
}

func (s *ServiceConfig) CheckHealth() (int, error) {
exitCode, err := run(s.healthCheckCmd)
// Reset command object - since it can't be reused
s.healthCheckCmd = argsToCmd(s.healthCheckCmd.Args)
return exitCode, err
}

const (
// Amount of time to wait before killing the application
defaultStopTimeout int = 5
Expand Down
28 changes: 28 additions & 0 deletions src/containerbuddy/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,34 @@ func TestGetIp(t *testing.T) {
}
}

func TestOnChangeCmd(t *testing.T) {
cmd1 := strToCmd("/root/examples/test/test.sh doStuff --debug")
backend := &BackendConfig{
onChangeCmd: cmd1,
}
if _, err := backend.OnChange(); err != nil {
t.Errorf("Unexpected error OnChange: %s", err)
}
// Ensure we can run it more than once
if _, err := backend.OnChange(); err != nil {
t.Errorf("Unexpected error OnChange (x2): %s", err)
}
}

func TestHealthCheck(t *testing.T) {
cmd1 := strToCmd("/root/examples/test/test.sh doStuff --debug")
service := &ServiceConfig{
healthCheckCmd: cmd1,
}
if _, err := service.CheckHealth(); err != nil {
t.Errorf("Unexpected error CheckHealth: %s", err)
}
// Ensure we can run it more than once
if _, err := service.CheckHealth(); err != nil {
t.Errorf("Unexpected error CheckHealth (x2): %s", err)
}
}

// ----------------------------------------------------
// test helpers

Expand Down
4 changes: 2 additions & 2 deletions src/containerbuddy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func poll(config Pollable, fn pollingFunc) chan bool {
// 0, we write a TTL health check to the health check store.
func checkHealth(pollable Pollable) {
service := pollable.(*ServiceConfig) // if we pass a bad type here we crash intentionally
if code, _ := run(service.healthCheckCmd); code == 0 {
if code, _ := service.CheckHealth(); code == 0 {
service.SendHeartbeat()
}
}
Expand All @@ -99,7 +99,7 @@ func checkHealth(pollable Pollable) {
func checkForChanges(pollable Pollable) {
backend := pollable.(*BackendConfig) // if we pass a bad type here we crash intentionally
if backend.CheckForUpstreamChanges() {
run(backend.onChangeCmd)
backend.OnChange()
}
}

Expand Down

0 comments on commit f9a77ed

Please sign in to comment.