1
0
Fork 0
mirror of https://github.com/Luzifer/elb-instance-status.git synced 2024-12-23 03:11:20 +00:00

enforce checks to quit before they are started again

This commit is contained in:
Knut Ahlers 2016-07-22 11:35:24 +02:00
parent 4a4526d0c2
commit 4a15dc4ee9
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E

28
main.go
View file

@ -15,12 +15,12 @@ import (
"sync" "sync"
"time" "time"
"gopkg.in/yaml.v2"
"github.com/Luzifer/rconfig" "github.com/Luzifer/rconfig"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/robfig/cron" "github.com/robfig/cron"
"golang.org/x/net/context"
"gopkg.in/yaml.v2"
) )
var ( var (
@ -123,17 +123,35 @@ func main() {
} }
func spawnChecks() { func spawnChecks() {
ctx, _ := context.WithTimeout(context.Background(), 59*time.Second)
for id := range checks { for id := range checks {
go executeAndRegisterCheck(id) go executeAndRegisterCheck(ctx, id)
} }
} }
func executeAndRegisterCheck(checkID string) { func executeAndRegisterCheck(ctx context.Context, checkID string) {
check := checks[checkID] check := checks[checkID]
start := time.Now() start := time.Now()
cmd := exec.Command("/bin/bash", "-c", check.Command) cmd := exec.Command("/bin/bash", "-c", check.Command)
err := cmd.Run() err := cmd.Start()
if err == nil {
cmdDone := make(chan error)
go func(cmdDone chan error, cmd *exec.Cmd) { cmdDone <- cmd.Wait() }(cmdDone, cmd)
loop := true
for loop {
select {
case err = <-cmdDone:
loop = false
case <-ctx.Done():
log.Printf("Execution of check '%s' was killed through context timeout.", checkID)
cmd.Process.Kill()
time.Sleep(time.Millisecond)
}
}
}
success := err == nil success := err == nil