Skip to content

Commit 3e51402

Browse files
authored
Merge pull request #24 from Clivern/feature/health
add health check wrapper
2 parents 5f9152c + b12e675 commit 3e51402

2 files changed

Lines changed: 138 additions & 0 deletions

File tree

‎README.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,24 @@ for _, task := range p.Tasks {
154154
}
155155
````
156156
157+
**Health Checker Component**
158+
159+
```golang
160+
healthChecker := hippo.NewHealthChecker()
161+
healthChecker.AddCheck("ping_check", func() (bool, error){
162+
return true, nil
163+
})
164+
healthChecker.AddCheck("db_check", func() (bool, error){
165+
return false, fmt.Errorf("Database Down")
166+
})
167+
healthChecker.RunChecks()
168+
169+
fmt.Println(healthChecker.ChecksStatus())
170+
// Output -> DOWN
171+
fmt.Println(healthChecker.ChecksReport())
172+
// Output -> [{"id":"ping_check","status":"UP","error":"","result":true},{"id":"db_check","status":"DOWN","error":"Database Down","result":false}]
173+
```
174+
157175
## Versioning
158176

159177
For transparency into our release cycle and in striving to maintain backward compatibility, Hippo is maintained under the [Semantic Versioning guidelines](https://semver.org/) and release process is predictable and business-friendly.

‎health.go‎

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// Copyright 2019 Clivern. All rights reserved.
2+
// Use of this source code is governed by the MIT
3+
// license that can be found in the LICENSE file.
4+
5+
package hippo
6+
7+
import (
8+
"encoding/json"
9+
)
10+
11+
const (
12+
// ServiceUp const
13+
ServiceUp = "UP"
14+
// ServiceDown const
15+
ServiceDown = "DOWN"
16+
// ServiceUnknown const
17+
ServiceUnknown = "UNKNOWN"
18+
)
19+
20+
// Check struct
21+
type Check struct {
22+
ID string `json:"id"`
23+
Status string `json:"status"`
24+
Error string `json:"error"`
25+
Result bool `json:"result"`
26+
callable func() (bool, error)
27+
}
28+
29+
// Health struct
30+
type Health struct {
31+
Status string
32+
Checks []*Check
33+
}
34+
35+
// NewHealthChecker initializes a new health checker
36+
func NewHealthChecker() *Health {
37+
return &Health{}
38+
}
39+
40+
// IsUnknown returns true if Status is Unknown
41+
func (h *Health) IsUnknown() bool {
42+
return h.Status == ServiceUnknown
43+
}
44+
45+
// IsUp returns true if Status is Up
46+
func (h *Health) IsUp() bool {
47+
return h.Status == ServiceUp
48+
}
49+
50+
// IsDown returns true if Status is Down
51+
func (h *Health) IsDown() bool {
52+
return h.Status == ServiceDown
53+
}
54+
55+
// Down set the Status to Down
56+
func (h *Health) Down() *Health {
57+
h.Status = ServiceDown
58+
return h
59+
}
60+
61+
// Unknown set the Status to Unknown
62+
func (h *Health) Unknown() *Health {
63+
h.Status = ServiceUnknown
64+
return h
65+
}
66+
67+
// Up set the Status to Up
68+
func (h *Health) Up() *Health {
69+
h.Status = ServiceUp
70+
return h
71+
}
72+
73+
// ChecksStatus get checks Status
74+
func (h *Health) ChecksStatus() string {
75+
return h.Status
76+
}
77+
78+
// ChecksReport get checks Status
79+
func (h *Health) ChecksReport() (string, error) {
80+
bytes, err := json.Marshal(h.Checks)
81+
if err != nil {
82+
return "", err
83+
}
84+
return string(bytes), nil
85+
}
86+
87+
// AddCheck adds a new check
88+
func (h *Health) AddCheck(ID string, callable func() (bool, error)) {
89+
check := &Check{
90+
ID: ID,
91+
Status: ServiceUnknown,
92+
callable: callable,
93+
}
94+
h.Checks = append(h.Checks, check)
95+
}
96+
97+
// RunChecks runs all health checks
98+
func (h *Health) RunChecks() {
99+
upCount := 0
100+
downCount := 0
101+
var err error
102+
for _, check := range h.Checks {
103+
check.Result, err = check.callable()
104+
if err != nil {
105+
check.Error = err.Error()
106+
}
107+
if check.Result {
108+
check.Status = ServiceUp
109+
upCount++
110+
} else {
111+
check.Status = ServiceDown
112+
downCount++
113+
}
114+
}
115+
if downCount > 0 {
116+
h.Down()
117+
} else {
118+
h.Up()
119+
}
120+
}

0 commit comments

Comments
 (0)