From 41b32a7c19b9f23ab5ebee9da5c116936aeee47a Mon Sep 17 00:00:00 2001 From: Kevin Petremann Date: Wed, 18 Sep 2024 17:21:30 +0200 Subject: [PATCH] feat: ready check This healthcheck tells if we had at least one valid build. --- internal/api/router/endpoints.go | 20 +++++++++++++++----- internal/api/router/manager.go | 3 ++- internal/report/repository.go | 7 +++++++ 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/internal/api/router/endpoints.go b/internal/api/router/endpoints.go index d4fbc63..c872082 100644 --- a/internal/api/router/endpoints.go +++ b/internal/api/router/endpoints.go @@ -16,11 +16,6 @@ const applicationJSON = "application/json" const hostnameKey = "hostname" const wildcard = "*" -func healthCheck(w http.ResponseWriter, _ *http.Request) { - w.Header().Set(contentType, applicationJSON) - _, _ = fmt.Fprintf(w, `{"status": "ok"}`) -} - func getVersion(w http.ResponseWriter, _ *http.Request) { w.Header().Set(contentType, applicationJSON) _, _ = fmt.Fprintf(w, `{"version": "%s", "build_time": "%s", "build_user": "%s"}`, app.Info.Version, app.Info.BuildTime, app.Info.BuildUser) @@ -32,6 +27,21 @@ func prometheusMetrics(h http.Handler) http.HandlerFunc { } } +func (m *Manager) healthCheck(w http.ResponseWriter, _ *http.Request) { + w.Header().Set(contentType, applicationJSON) + _, _ = fmt.Fprintf(w, `{"status": "ok"}`) +} + +func (m *Manager) readyCheck(w http.ResponseWriter, _ *http.Request) { + w.Header().Set(contentType, applicationJSON) + if m.reports.HasValidBuild() { + _, _ = fmt.Fprintf(w, `{"status": "ok"}`) + } else { + w.WriteHeader(http.StatusServiceUnavailable) + _, _ = fmt.Fprintf(w, `{"status": "not ready"}`) + } +} + // getAFKEnabled endpoint returns all AFK enabled devices. // They are supposed to be managed by AFK, meaning the configuration should be applied periodically. func (m *Manager) getAFKEnabled(w http.ResponseWriter, r *http.Request) { diff --git a/internal/api/router/manager.go b/internal/api/router/manager.go index 801c4dd..aaa3a78 100644 --- a/internal/api/router/manager.go +++ b/internal/api/router/manager.go @@ -64,7 +64,8 @@ func (m *Manager) ListenAndServe(ctx context.Context, address string, port int, // internal endpoints mux.HandleFunc("GET /metrics", prometheusMetrics(promhttp.Handler())) mux.HandleFunc("GET /api/version", getVersion) - mux.HandleFunc("GET /api/health", healthCheck) + mux.HandleFunc("GET /api/health", m.healthCheck) + mux.HandleFunc("GET /api/ready", m.readyCheck) api.Get("/metrics"). HasResponseModel(http.StatusOK, rest.ModelOf[string]()). diff --git a/internal/report/repository.go b/internal/report/repository.go index d550ae3..66690de 100644 --- a/internal/report/repository.go +++ b/internal/report/repository.go @@ -42,6 +42,13 @@ func (r *Repository) GetLastSuccessfulJSON() ([]byte, error) { return r.lastSuccessful.ToJSON() } +func (r *Repository) HasValidBuild() bool { + r.mutex.Lock() + defer r.mutex.Unlock() + + return r.lastSuccessful != nil +} + func (r *Repository) UpdateStatus(status jobStatus) { r.mutex.Lock() defer r.mutex.Unlock()