api/pkg/health/checker.go
Peter Kurfer 108444e094 Add health API and basic CLI support
- remove plugin API due to incompatibility issues
- add Docker build to GitHub Actions
- add custom container friendly config file
2020-06-15 12:32:18 +02:00

45 lines
911 B
Go

package health
import "fmt"
type checker struct {
componentChecks map[string]Check
}
type Checker interface {
RegisterCheck(component string, check Check) error
IsHealthy() Result
}
func (c *checker) RegisterCheck(component string, check Check) error {
if _, exists := c.componentChecks[component]; exists {
return fmt.Errorf("component: %s: %w", component, CheckForComponentAlreadyRegistered)
}
c.componentChecks[component] = check
return nil
}
func (c *checker) IsHealthy() (r Result) {
r.Status = HEALTHY
r.Components = make(map[string]CheckResult)
for component, componentCheck := range c.componentChecks {
r.Components[component] = componentCheck()
r.Status = max(r.Components[component].Status, r.Status)
}
return
}
func max(s1 Status, s2 Status) Status {
var max Status
if s1 > s2 {
max = s1
} else {
max = s2
}
if max > UNHEALTHY {
return UNHEALTHY
}
return max
}