Skip to content

Commit 41b32a7

Browse files
committed
feat: ready check
This healthcheck tells if we had at least one valid build.
1 parent b20ff74 commit 41b32a7

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

internal/api/router/endpoints.go

+15-5
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@ const applicationJSON = "application/json"
1616
const hostnameKey = "hostname"
1717
const wildcard = "*"
1818

19-
func healthCheck(w http.ResponseWriter, _ *http.Request) {
20-
w.Header().Set(contentType, applicationJSON)
21-
_, _ = fmt.Fprintf(w, `{"status": "ok"}`)
22-
}
23-
2419
func getVersion(w http.ResponseWriter, _ *http.Request) {
2520
w.Header().Set(contentType, applicationJSON)
2621
_, _ = 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 {
3227
}
3328
}
3429

30+
func (m *Manager) healthCheck(w http.ResponseWriter, _ *http.Request) {
31+
w.Header().Set(contentType, applicationJSON)
32+
_, _ = fmt.Fprintf(w, `{"status": "ok"}`)
33+
}
34+
35+
func (m *Manager) readyCheck(w http.ResponseWriter, _ *http.Request) {
36+
w.Header().Set(contentType, applicationJSON)
37+
if m.reports.HasValidBuild() {
38+
_, _ = fmt.Fprintf(w, `{"status": "ok"}`)
39+
} else {
40+
w.WriteHeader(http.StatusServiceUnavailable)
41+
_, _ = fmt.Fprintf(w, `{"status": "not ready"}`)
42+
}
43+
}
44+
3545
// getAFKEnabled endpoint returns all AFK enabled devices.
3646
// They are supposed to be managed by AFK, meaning the configuration should be applied periodically.
3747
func (m *Manager) getAFKEnabled(w http.ResponseWriter, r *http.Request) {

internal/api/router/manager.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ func (m *Manager) ListenAndServe(ctx context.Context, address string, port int,
6464
// internal endpoints
6565
mux.HandleFunc("GET /metrics", prometheusMetrics(promhttp.Handler()))
6666
mux.HandleFunc("GET /api/version", getVersion)
67-
mux.HandleFunc("GET /api/health", healthCheck)
67+
mux.HandleFunc("GET /api/health", m.healthCheck)
68+
mux.HandleFunc("GET /api/ready", m.readyCheck)
6869

6970
api.Get("/metrics").
7071
HasResponseModel(http.StatusOK, rest.ModelOf[string]()).

internal/report/repository.go

+7
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ func (r *Repository) GetLastSuccessfulJSON() ([]byte, error) {
4242
return r.lastSuccessful.ToJSON()
4343
}
4444

45+
func (r *Repository) HasValidBuild() bool {
46+
r.mutex.Lock()
47+
defer r.mutex.Unlock()
48+
49+
return r.lastSuccessful != nil
50+
}
51+
4552
func (r *Repository) UpdateStatus(status jobStatus) {
4653
r.mutex.Lock()
4754
defer r.mutex.Unlock()

0 commit comments

Comments
 (0)