Skip to content

azazeal/health

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status Coverage Report Go Reference

health

Package health implements a reusable, concurrent health check for Go apps.

Usage

package main

import (
	"context"
	"log"
	"net/http"
	"time"

	"github.com/azazeal/health"
	"github.com/azazeal/pause"
)

func main() {
	var hc health.Check
	go longRunningTaskThatMightFail(context.TODO(), &hc)

	http.Handle("/health", &hc) // /health returns 204 if hc.Healthy(), or 503
	if err := http.ListenAndServe(":8080", nil); err != http.ErrServerClosed {
		log.Fatal(err)
	}
}

func longRunningTaskThatMightFail(ctx context.Context, hc *health.Check) {
	for ctx.Err() == nil {
		if err := mightFail(ctx); err != nil {
			// the component failed
			hc.Fail("component") 

			pause.For(ctx, time.Second)

			continue
		}
		hc.Pass("component") // the component did not fail; carry on

		// ...
	}
}

func mightFail(context.Context) (err error) {
	// ...
	return
}