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 03f6541 commit 2074875
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 30 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ git clone https://github.com/cloud-barista/cm-cicada.git
Check if CM-Cicada is running

```bash
curl http://127.0.0.1:8083/cicada/health
curl http://127.0.0.1:8083/cicada/readyz

# Output if it's running successfully
# {"message":"CM-Cicada API server is running"}
Expand Down
19 changes: 18 additions & 1 deletion cmd/cm-cicada/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import (
"github.com/cloud-barista/cm-cicada/db"
"github.com/cloud-barista/cm-cicada/lib/airflow"
"github.com/cloud-barista/cm-cicada/lib/config"
"github.com/cloud-barista/cm-cicada/pkg/api/rest/controller"
"github.com/cloud-barista/cm-cicada/pkg/api/rest/server"
"github.com/jollaman999/utils/logger"
"github.com/jollaman999/utils/syscheck"
"log"
"os"
"os/signal"
"strings"
"sync"
"syscall"
)

Expand All @@ -31,14 +33,29 @@ 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())
}

controller.OkMessage.Message = "Airflow connection is not ready"
airflow.Init()

server.Init()
controller.OkMessage.Message = "CM-Cicada 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 Cicada server health.
// @Summary Check Cicada is alive
// @Description Check Cicada is alive
var OkMessage = SimpleMsg{}
var IsReady = false

// CheckReady func is for checking Cicada server health.
// @Summary Check Ready
// @Description Check Cicada 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 /cicada/health [get]
func GetHealth(c echo.Context) error {
okMessage := SimpleMsg{}
okMessage.Message = "CM-Cicada API server is running"
return c.JSONPretty(http.StatusOK, &okMessage, " ")
// @Router /cicada/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": {
"/cicada/health": {
"/cicada/readyz": {
"get": {
"description": "Check Cicada is alive",
"description": "Check Cicada is ready",
"consumes": [
"application/json"
],
Expand All @@ -27,16 +27,16 @@ const docTemplate = `{
"tags": [
"[Admin] System management"
],
"summary": "Check Cicada 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-cicada_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": {
"/cicada/health": {
"/cicada/readyz": {
"get": {
"description": "Check Cicada is alive",
"description": "Check Cicada is ready",
"consumes": [
"application/json"
],
Expand All @@ -16,16 +16,16 @@
"tags": [
"[Admin] System management"
],
"summary": "Check Cicada 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-cicada_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 @@ -182,23 +182,23 @@ definitions:
info:
contact: {}
paths:
/cicada/health:
/cicada/readyz:
get:
consumes:
- application/json
description: Check Cicada is alive
description: Check Cicada 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-cicada_pkg_api_rest_common.ErrorResponse'
summary: Check Cicada is alive
summary: Check Ready
tags:
- '[Admin] System management'
/task_component:
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 2074875

Please sign in to comment.