|
| 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