1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- package toolbox
- import (
- "bytes"
- )
- type HealthChecker interface {
- Desc() string
- Check() error
- }
- type HealthCheckFunc func() error
- type HealthCheckFuncDesc struct {
- Desc string
- Func HealthCheckFunc
- }
- type healthCheck struct {
- desc string
- HealthChecker
- check HealthCheckFunc
- }
- func (t *toolbox) AddHealthCheck(hc HealthChecker) {
- t.healthCheckJobs = append(t.healthCheckJobs, &healthCheck{
- HealthChecker: hc,
- })
- }
- func (t *toolbox) AddHealthCheckFunc(desc string, fn HealthCheckFunc) {
- t.healthCheckJobs = append(t.healthCheckJobs, &healthCheck{
- desc: desc,
- check: fn,
- })
- }
- func (t *toolbox) handleHealthCheck() string {
- if len(t.healthCheckJobs) == 0 {
- return "no health check jobs"
- }
- var buf bytes.Buffer
- var err error
- for _, job := range t.healthCheckJobs {
- buf.WriteString("* ")
- if job.check != nil {
- buf.WriteString(job.desc)
- err = job.check()
- } else {
- buf.WriteString(job.Desc())
- err = job.Check()
- }
- buf.WriteString(": ")
- if err == nil {
- buf.WriteString("OK")
- } else {
- buf.WriteString(err.Error())
- }
- buf.WriteString("\n")
- }
- return buf.String()
- }
|