/
exec.go
51 lines (42 loc) · 1.02 KB
/
exec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package healthcheck
/**
* exec.go - Exec healthcheck
*
* @author Yaroslav Pogrebnyak <yyyaroslav@gmail.com>
*/
import (
"time"
"github.com/yyyar/gobetween/config"
"github.com/yyyar/gobetween/core"
"github.com/yyyar/gobetween/logging"
"github.com/yyyar/gobetween/utils"
)
/**
* Exec healthcheck
*/
func exec(t core.Target, cfg config.HealthcheckConfig, result chan<- CheckResult) {
log := logging.For("healthcheck/exec")
execTimeout, _ := time.ParseDuration(cfg.Timeout)
checkResult := CheckResult{
Target: t,
}
out, err := utils.ExecTimeout(execTimeout, cfg.ExecCommand, t.Host, t.Port)
if err != nil {
// TODO: Decide better what to do in this case
checkResult.Live = false
log.Warn(err)
} else {
if out == cfg.ExecExpectedPositiveOutput {
checkResult.Live = true
} else if out == cfg.ExecExpectedNegativeOutput {
checkResult.Live = false
} else {
log.Warn("Unexpected output: ", out)
}
}
select {
case result <- checkResult:
default:
log.Warn("Channel is full. Discarding value")
}
}