aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGibheer <gibheer+git@zero-knowledge.org>2018-11-29 10:45:33 +0100
committerGibheer <gibheer+git@zero-knowledge.org>2018-11-29 10:45:33 +0100
commit83a1b45b19658f7436504368ab87416787f2d95d (patch)
tree3172d54af7cc2ec7d343ed334fa852c5abf30dd8
parent3b8e27706b2b0d623d2c162c1cd24f5f5806140a (diff)
moncheck - add timeout option
Before the timeout for checks was static. With this change it is finally an option to configure.
-rw-r--r--cmd/moncheck/main.go20
1 files 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)