/
healthcheck.go
71 lines (60 loc) · 1.58 KB
/
healthcheck.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package traceability
import (
"fmt"
"github.com/Axway/agent-sdk/pkg/jobs"
hc "github.com/Axway/agent-sdk/pkg/util/healthcheck"
"github.com/Axway/agent-sdk/pkg/util/log"
)
// traceabilityHealthCheck -
type traceabilityHealthCheck struct {
jobs.Job
logger log.FieldLogger
ready bool
prevErr error
}
func newTraceabilityHealthCheckJob() *traceabilityHealthCheck {
return &traceabilityHealthCheck{
logger: log.NewFieldLogger().WithComponent("traceabilityHealthCheck").WithPackage(traceabilityStr),
}
}
// Ready -
func (j *traceabilityHealthCheck) Ready() bool {
j.ready = j.checkConnections() == nil
return j.ready
}
// Status -
func (j *traceabilityHealthCheck) Status() error {
return j.prevErr
}
// Execute -
func (j *traceabilityHealthCheck) Execute() error {
return j.checkConnections()
}
func (j *traceabilityHealthCheck) healthcheck(name string) *hc.Status {
// Create the default status
status := &hc.Status{
Result: hc.OK,
}
if !j.ready || j.prevErr != nil {
status.Result = hc.FAIL
status.Details = "agent not connected to traceability yet"
}
if j.prevErr != nil {
status.Details = fmt.Sprintf("connection error: %s Failed. %s", name, j.prevErr.Error())
}
return status
}
func (j *traceabilityHealthCheck) checkConnections() error {
client, err := getClient()
if err != nil {
j.logger.WithError(err).Error("could not get traceability client")
return err
}
j.prevErr = client.Connect()
if j.prevErr != nil {
j.logger.WithError(j.prevErr).Error("connection failed")
} else {
j.logger.Trace("connection to traceability succeeded")
}
return j.prevErr
}