From 5d87913cf3a1496de038ea184505dd0573d2b8d6 Mon Sep 17 00:00:00 2001 From: Yolanda Robla Mota Date: Fri, 16 Feb 2024 01:32:09 +0100 Subject: [PATCH] feat: add healthcheck endpoint (#427) This is useful for deploying package-feeds on any container orchestration system Signed-off-by: Yolanda Robla Signed-off-by: Yolanda Robla --- pkg/scheduler/scheduler.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/pkg/scheduler/scheduler.go b/pkg/scheduler/scheduler.go index 780848c2..4442d828 100644 --- a/pkg/scheduler/scheduler.go +++ b/pkg/scheduler/scheduler.go @@ -1,6 +1,7 @@ package scheduler import ( + "encoding/json" "fmt" "net/http" "strings" @@ -36,6 +37,38 @@ type pollResult struct { errs []error } +// healthCheckHandler is a simple health check handler for the HTTP server. +func healthCheckHandler(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + + // Define a simple JSON response structure + response := struct { + Status string `json:"status"` + Message string `json:"message"` + }{ + Status: "OK", + Message: "Service is up and running", + } + + // Serialize the response structure to JSON + jsonResponse, err := json.Marshal(response) + if err != nil { + // If there's an error, log it and return a 500 Internal Server Error + log.Errorf("Failed to marshal health check response: %v", err) + w.WriteHeader(http.StatusInternalServerError) + return + } + + // Write an HTTP status code to the response + w.WriteHeader(http.StatusOK) + + // Write the JSON response body + _, err = w.Write(jsonResponse) + if err != nil { + log.Errorf("Failed to write health check response: %v", err) + } +} + // Runs several services for the operation of scheduler, this call is blocking until application exit // or failure in the HTTP server // Services include: Cron polling via FeedGroups, HTTP serving of FeedGroupsHandler. @@ -89,6 +122,7 @@ func (s *Scheduler) Run(initialCutoff time.Duration, enableDefaultTimer bool) er pollServer := NewFeedGroupsHandler(feedGroups) log.Infof("Listening on port %v for %s", s.httpPort, strings.Join(pollFeedNames, ", ")) http.Handle("/", pollServer) + http.HandleFunc("/health", healthCheckHandler) server := &http.Server{ Addr: fmt.Sprintf(":%v", s.httpPort),