moncheck - add timeout option
Before the timeout for checks was static. With this change it is finally an option to configure.
This commit is contained in:
parent
3b8e27706b
commit
83a1b45b19
@ -27,6 +27,7 @@ var (
|
||||
type (
|
||||
Config struct {
|
||||
DB string `json:"db"`
|
||||
Timeout string `json:"timeout"`
|
||||
Wait string `json:"wait"`
|
||||
}
|
||||
|
||||
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user