Skip to content

Commit

Permalink
Improve health check API
Browse files Browse the repository at this point in the history
  • Loading branch information
ish-hcc committed May 24, 2024
1 parent 2586129 commit 58e6c72
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 31 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ windows: lint swag ## Build the binary file for Windows
@echo Build finished!

run: ## Run the built binary
@sudo killall ${MODULE_NAME} | true
@git diff > .diff_current
@STATUS=`diff .diff_last_build .diff_current 2>&1 > /dev/null; echo $$?` && \
GIT_HASH_MINE=`git rev-parse HEAD` && \
Expand All @@ -113,9 +114,9 @@ run: ## Run the built binary
@@kernel_name=`uname -s` && \
cp -RpPf conf cmd/${MODULE_NAME}/ && \
if [[ $$kernel_name == "CYGWIN"* ]] || [[ $$kernel_name == "MINGW"* ]]; then \
./cmd/${MODULE_NAME}/${MODULE_NAME}.exe; \
./cmd/${MODULE_NAME}/${MODULE_NAME}.exe & \
else \
./cmd/${MODULE_NAME}/${MODULE_NAME} || echo "Trying with sudo..." && sudo ./cmd/${MODULE_NAME}/${MODULE_NAME}; \
./cmd/${MODULE_NAME}/${MODULE_NAME} || echo "Trying with sudo..." && sudo ./cmd/${MODULE_NAME}/${MODULE_NAME} & \
fi

stop: ## Stop the built binary
Expand Down
18 changes: 17 additions & 1 deletion cmd/cm-grasshopper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"github.com/cloud-barista/cm-grasshopper/common"
"github.com/cloud-barista/cm-grasshopper/db"
"github.com/cloud-barista/cm-grasshopper/lib/config"
"github.com/cloud-barista/cm-grasshopper/pkg/api/rest/controller"
"github.com/cloud-barista/cm-grasshopper/pkg/api/rest/server"
"github.com/jollaman999/utils/logger"
"github.com/jollaman999/utils/syscheck"
"log"
"os"
"os/signal"
"strings"
"sync"
"syscall"
)

Expand All @@ -30,12 +32,26 @@ func init() {
log.Panicln(err)
}

var wg sync.WaitGroup
wg.Add(1)
go func() {
defer func() {
wg.Done()
}()
controller.OkMessage.Message = "API server is not ready"
server.Init()
}()

controller.OkMessage.Message = "Database is not ready"
err = db.Open()
if err != nil {
logger.Panicln(logger.ERROR, true, err.Error())
}

server.Init()
controller.OkMessage.Message = "CM-Grasshopper API server is ready"
controller.IsReady = true

wg.Wait()
}

func end() {
Expand Down
31 changes: 19 additions & 12 deletions pkg/api/rest/controller/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,25 @@ type SimpleMsg struct {
Message string `json:"message"`
}

// GetHealth func is for checking Grasshopper server health.
// @Summary Check Grasshopper is alive
// @Description Check Grasshopper is alive
var OkMessage = SimpleMsg{}
var IsReady = false

// CheckReady func is for checking Grasshopper server health.
// @Summary Check Ready
// @Description Check Grasshopper is ready
// @Tags [Admin] System management
// @Accept json
// @Produce json
// @Success 200 {object} SimpleMsg "Successfully get heath state."
// @Failure 500 {object} common.ErrorResponse "Failed to check health."
// @Accept json
// @Produce json
// @Success 200 {object} SimpleMsg "Successfully get ready state."
// @Failure 500 {object} common.ErrorResponse "Failed to check ready state."
//
// @Router /grasshopper/health [get]
func GetHealth(c echo.Context) error {
okMessage := SimpleMsg{}
okMessage.Message = "CM-Grasshopper API server is running"
return c.JSONPretty(http.StatusOK, &okMessage, " ")
// @Router /grasshopper/readyz [get]
func CheckReady(c echo.Context) error {
status := http.StatusOK

if !IsReady {
status = http.StatusServiceUnavailable
}

return c.JSONPretty(status, &OkMessage, " ")
}
10 changes: 5 additions & 5 deletions pkg/api/rest/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ const docTemplate = `{
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {
"/grasshopper/health": {
"/grasshopper/readyz": {
"get": {
"description": "Check Grasshopper is alive",
"description": "Check Grasshopper is ready",
"consumes": [
"application/json"
],
Expand All @@ -27,16 +27,16 @@ const docTemplate = `{
"tags": [
"[Admin] System management"
],
"summary": "Check Grasshopper is alive",
"summary": "Check Ready",
"responses": {
"200": {
"description": "Successfully get heath state.",
"description": "Successfully get ready state.",
"schema": {
"$ref": "#/definitions/pkg_api_rest_controller.SimpleMsg"
}
},
"500": {
"description": "Failed to check health.",
"description": "Failed to check ready state.",
"schema": {
"$ref": "#/definitions/github_com_cloud-barista_cm-grasshopper_pkg_api_rest_common.ErrorResponse"
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/api/rest/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"contact": {}
},
"paths": {
"/grasshopper/health": {
"/grasshopper/readyz": {
"get": {
"description": "Check Grasshopper is alive",
"description": "Check Grasshopper is ready",
"consumes": [
"application/json"
],
Expand All @@ -16,16 +16,16 @@
"tags": [
"[Admin] System management"
],
"summary": "Check Grasshopper is alive",
"summary": "Check Ready",
"responses": {
"200": {
"description": "Successfully get heath state.",
"description": "Successfully get ready state.",
"schema": {
"$ref": "#/definitions/pkg_api_rest_controller.SimpleMsg"
}
},
"500": {
"description": "Failed to check health.",
"description": "Failed to check ready state.",
"schema": {
"$ref": "#/definitions/github_com_cloud-barista_cm-grasshopper_pkg_api_rest_common.ErrorResponse"
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/api/rest/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,23 @@ definitions:
info:
contact: {}
paths:
/grasshopper/health:
/grasshopper/readyz:
get:
consumes:
- application/json
description: Check Grasshopper is alive
description: Check Grasshopper is ready
produces:
- application/json
responses:
"200":
description: Successfully get heath state.
description: Successfully get ready state.
schema:
$ref: '#/definitions/pkg_api_rest_controller.SimpleMsg'
"500":
description: Failed to check health.
description: Failed to check ready state.
schema:
$ref: '#/definitions/github_com_cloud-barista_cm-grasshopper_pkg_api_rest_common.ErrorResponse'
summary: Check Grasshopper is alive
summary: Check Ready
tags:
- '[Admin] System management'
/software/install:
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/rest/route/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ import (
)

func RegisterUtility(e *echo.Echo) {
e.GET("/"+strings.ToLower(common.ShortModuleName)+"/health", controller.GetHealth)
e.GET("/"+strings.ToLower(common.ShortModuleName)+"/readyz", controller.CheckReady)
}

0 comments on commit 58e6c72

Please sign in to comment.