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

APIGOV-25722 - remove the call to execute healthchecks for the status request processing #657

Merged
merged 3 commits into from
Jun 26, 2023
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
1 change: 1 addition & 0 deletions pkg/util/healthcheck/definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type healthChecker struct {
StatusDetail string `json:"-"`
Checks map[string]*statusCheck `json:"statusChecks"`
registered bool
statusServer *Server `json:"-"`
}

// Status - the status of this healthcheck
Expand Down
6 changes: 5 additions & 1 deletion pkg/util/healthcheck/healthcheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func TestHTTPRequests(t *testing.T) {
// Register healthchecks
RegisterHealthcheck("hc1", "hc1", hcFunc)
RegisterHealthcheck("hc2", "hc2", hcFunc)

RunChecks()
var server *httptest.Server

// http Client
Expand Down Expand Up @@ -194,6 +194,7 @@ func TestHTTPRequests(t *testing.T) {

// Marshall the body to the healthChecker struct
var result healthChecker
RunChecks()
resp := getRequest("/status", &result, false)

// assert response values
Expand All @@ -207,6 +208,7 @@ func TestHTTPRequests(t *testing.T) {
hcValues["hc2"] = true

// Execute another request
RunChecks()
resp = getRequest("/status", &result, false)

// assert response values
Expand Down Expand Up @@ -238,6 +240,7 @@ func TestHTTPRequests(t *testing.T) {

// Set hc1 to fail
hcValues["hc1"] = false
RunChecks()
resp = getRequest("/status/hc1", &checkRes, false)

// assert response values
Expand All @@ -246,6 +249,7 @@ func TestHTTPRequests(t *testing.T) {

// Set hc1 to fail
hcValues["hc1"] = true
RunChecks()
resp = getRequest("/status/hc1", &checkRes, false)

// assert response values
Expand Down
23 changes: 14 additions & 9 deletions pkg/util/healthcheck/healthchecker.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ func RegisterHealthcheck(name, endpoint string, check CheckStatus) (string, erro
}

globalHealthChecker.Checks[endpoint] = newChecker

http.HandleFunc(fmt.Sprintf("/status/%s", endpoint), checkHandler)
statusServer := globalHealthChecker.statusServer
if statusServer != nil {
statusServer.registerHandler(fmt.Sprintf("/status/%s", endpoint), checkHandler)
}

if util.IsNotTest() {
newChecker.executeCheck()
Expand Down Expand Up @@ -153,16 +155,24 @@ type Server struct {
}

func NewServer(httpprof bool) *Server {
return &Server{
globalHealthChecker.statusServer = &Server{
router: http.NewServeMux(),
httpprof: httpprof,
}
return globalHealthChecker.statusServer
}

func (s *Server) registerHandler(path string, handler func(http.ResponseWriter, *http.Request)) {
vivekschauhan marked this conversation as resolved.
Show resolved Hide resolved
s.router.HandleFunc(path, handler)
}

// HandleRequests - starts the http server
func (s *Server) HandleRequests() {
if !globalHealthChecker.registered {
s.router.HandleFunc("/status", statusHandler)
s.registerHandler("/status", statusHandler)
for _, statusChecks := range globalHealthChecker.Checks {
s.registerHandler(fmt.Sprintf("/status/%s", statusChecks.Endpoint), checkHandler)
}
globalHealthChecker.registered = true
}

Expand Down Expand Up @@ -241,9 +251,6 @@ func GetHealthcheckOutput(url string) (string, error) {
}

func statusHandler(w http.ResponseWriter, r *http.Request) {
// Run the checks to get the latest results
RunChecks()

w.Header().Set("Content-Type", "application/json; charset=utf-8")

// Return the data
Expand Down Expand Up @@ -281,8 +288,6 @@ func checkHandler(w http.ResponseWriter, r *http.Request) {
return
}

thisCheck.executeCheck()

w.Header().Set("Content-Type", "application/json; charset=utf-8")
// If check failed change return code to 500
if thisCheck.Status.Result == FAIL {
Expand Down