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 (
|
type (
|
||||||
Config struct {
|
Config struct {
|
||||||
DB string `json:"db"`
|
DB string `json:"db"`
|
||||||
|
Timeout string `json:"timeout"`
|
||||||
Wait string `json:"wait"`
|
Wait string `json:"wait"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,7 +41,7 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("could not read config: %s", err)
|
log.Fatalf("could not read config: %s", err)
|
||||||
}
|
}
|
||||||
config := Config{}
|
config := Config{Timeout: "30s", Wait: "30s"}
|
||||||
if err := json.Unmarshal(raw, &config); err != nil {
|
if err := json.Unmarshal(raw, &config); err != nil {
|
||||||
log.Fatalf("could not parse config: %s", err)
|
log.Fatalf("could not parse config: %s", err)
|
||||||
}
|
}
|
||||||
@ -49,6 +50,10 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("could not parse wait duration: %s", err)
|
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)
|
db, err := sql.Open("postgres", config.DB)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -56,14 +61,14 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < 25; i++ {
|
for i := 0; i < 25; i++ {
|
||||||
go check(i, db, waitDuration)
|
go check(i, db, waitDuration, timeout)
|
||||||
}
|
}
|
||||||
wg := sync.WaitGroup{}
|
wg := sync.WaitGroup{}
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
func check(thread int, db *sql.DB, waitDuration time.Duration) {
|
func check(thread int, db *sql.DB, waitDuration, timeout time.Duration) {
|
||||||
for {
|
for {
|
||||||
tx, err := db.Begin()
|
tx, err := db.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -101,20 +106,19 @@ func check(thread int, db *sql.DB, waitDuration time.Duration) {
|
|||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
continue
|
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:]...)
|
cmd := exec.CommandContext(ctx, cmdLine[0], cmdLine[1:]...)
|
||||||
output := bytes.NewBuffer([]byte{})
|
output := bytes.NewBuffer([]byte{})
|
||||||
cmd.Stdout = output
|
cmd.Stdout = output
|
||||||
cmd.Stderr = output
|
cmd.Stderr = output
|
||||||
err = cmd.Run()
|
err = cmd.Run()
|
||||||
if err != nil && ctx.Err() == context.DeadlineExceeded {
|
if err != nil && ctx.Err() == context.DeadlineExceeded {
|
||||||
log.Printf("[%d] check took too long: %s", id, err)
|
|
||||||
cancel()
|
cancel()
|
||||||
// TODO which state to choose?
|
// TODO which state to choose?
|
||||||
// TODO add notification handler
|
// TODO add notification handler
|
||||||
// TODO all this casting should be done better
|
// TODO all this casting should be done better
|
||||||
states.Add(99)
|
states.Add(99)
|
||||||
output.Write([]byte(ctx.Err().Error()))
|
fmt.Fprintf(output, "check took longer than %s", timeout)
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
cancel()
|
cancel()
|
||||||
status, ok := cmd.ProcessState.Sys().(syscall.WaitStatus)
|
status, ok := cmd.ProcessState.Sys().(syscall.WaitStatus)
|
||||||
|
Loading…
Reference in New Issue
Block a user