From 83a1b45b19658f7436504368ab87416787f2d95d Mon Sep 17 00:00:00 2001 From: Gibheer Date: Thu, 29 Nov 2018 10:45:33 +0100 Subject: [PATCH] moncheck - add timeout option Before the timeout for checks was static. With this change it is finally an option to configure. --- cmd/moncheck/main.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/cmd/moncheck/main.go b/cmd/moncheck/main.go index fbc2b57..cc05594 100644 --- a/cmd/moncheck/main.go +++ b/cmd/moncheck/main.go @@ -26,8 +26,9 @@ var ( type ( Config struct { - DB string `json:"db"` - Wait string `json:"wait"` + DB string `json:"db"` + Timeout string `json:"timeout"` + Wait string `json:"wait"` } States []int @@ -40,7 +41,7 @@ func main() { if err != nil { log.Fatalf("could not read config: %s", err) } - config := Config{} + config := Config{Timeout: "30s", Wait: "30s"} if err := json.Unmarshal(raw, &config); err != nil { log.Fatalf("could not parse config: %s", err) } @@ -49,6 +50,10 @@ func main() { if err != nil { log.Fatalf("could not parse wait duration: %s", err) } + timeout, err := time.ParseDuration(config.Timeout) + if err != nil { + log.Fatalf("could not parse timeout: %s", err) + } db, err := sql.Open("postgres", config.DB) if err != nil { @@ -56,14 +61,14 @@ func main() { } for i := 0; i < 25; i++ { - go check(i, db, waitDuration) + go check(i, db, waitDuration, timeout) } wg := sync.WaitGroup{} wg.Add(1) wg.Wait() } -func check(thread int, db *sql.DB, waitDuration time.Duration) { +func check(thread int, db *sql.DB, waitDuration, timeout time.Duration) { for { tx, err := db.Begin() if err != nil { @@ -101,20 +106,19 @@ func check(thread int, db *sql.DB, waitDuration time.Duration) { tx.Rollback() continue } - ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), timeout) cmd := exec.CommandContext(ctx, cmdLine[0], cmdLine[1:]...) output := bytes.NewBuffer([]byte{}) cmd.Stdout = output cmd.Stderr = output err = cmd.Run() if err != nil && ctx.Err() == context.DeadlineExceeded { - log.Printf("[%d] check took too long: %s", id, err) cancel() // TODO which state to choose? // TODO add notification handler // TODO all this casting should be done better states.Add(99) - output.Write([]byte(ctx.Err().Error())) + fmt.Fprintf(output, "check took longer than %s", timeout) } else if err != nil { cancel() status, ok := cmd.ProcessState.Sys().(syscall.WaitStatus)