diff --git a/app.go b/app.go index fc492b3..f4f0811 100644 --- a/app.go +++ b/app.go @@ -231,6 +231,13 @@ func (a *App) Failed() <-chan error { return a.failed } +// Ready is Readiness().Ready. Together with Live it lets an App be passed as a +// single health source, for example to dioad/net's Server.AddHealthSource, +// which then serves /health/live from Live and /health/ready from Ready. +func (a *App) Ready(ctx context.Context) (any, error) { + return a.readiness.Ready(ctx) +} + // Live returns nil while every started service that implements Liveness // reports live, and otherwise the errors of those that do not, each naming its // service. diff --git a/app_test.go b/app_test.go index 8d2c7b7..97204a9 100644 --- a/app_test.go +++ b/app_test.go @@ -274,6 +274,26 @@ func TestApp_Live_NamesTheServicesThatAreNotLive(t *testing.T) { assert.NotContains(t, err.Error(), "good") } +func TestApp_IsAHealthSourceOnItsOwn(t *testing.T) { + t.Parallel() + var _ interface { + Live(context.Context) error + Ready(context.Context) (any, error) + } = (*App)(nil) + + app := New() + svc := newFailingService("store", &eventLog{}) + svc.ready = errors.New("replica behind") + require.NoError(t, app.Add("store", svc)) + require.NoError(t, app.Start(t.Context())) + t.Cleanup(func() { _ = app.Stop(context.Background()) }) + + _, err := app.Ready(t.Context()) + + require.ErrorContains(t, err, "replica behind") + require.NoError(t, app.Live(t.Context())) +} + func TestApp_ServiceReadinessIsACheckOfThatName(t *testing.T) { t.Parallel() app := New()