Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(health): ready check #46

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions internal/api/router/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion internal/api/router/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]()).
Expand Down
7 changes: 7 additions & 0 deletions internal/report/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading