Skip to content

Latest commit

 

History

History
54 lines (41 loc) · 1.27 KB

README.md

File metadata and controls

54 lines (41 loc) · 1.27 KB

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
}