diff --git a/README.md b/README.md index 7ad2b54..1438ed9 100644 --- a/README.md +++ b/README.md @@ -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"} diff --git a/cmd/cm-cicada/main.go b/cmd/cm-cicada/main.go index f50a413..fc0ff8f 100644 --- a/cmd/cm-cicada/main.go +++ b/cmd/cm-cicada/main.go @@ -5,6 +5,7 @@ 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" @@ -12,6 +13,7 @@ import ( "os" "os/signal" "strings" + "sync" "syscall" ) @@ -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() { diff --git a/pkg/api/rest/controller/health.go b/pkg/api/rest/controller/health.go index 3eb4249..4b7e1d4 100644 --- a/pkg/api/rest/controller/health.go +++ b/pkg/api/rest/controller/health.go @@ -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, " ") } diff --git a/pkg/api/rest/docs/docs.go b/pkg/api/rest/docs/docs.go index 5188841..f754be9 100644 --- a/pkg/api/rest/docs/docs.go +++ b/pkg/api/rest/docs/docs.go @@ -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" ], @@ -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" } diff --git a/pkg/api/rest/docs/swagger.json b/pkg/api/rest/docs/swagger.json index 1deb750..ad55e3b 100644 --- a/pkg/api/rest/docs/swagger.json +++ b/pkg/api/rest/docs/swagger.json @@ -4,9 +4,9 @@ "contact": {} }, "paths": { - "/cicada/health": { + "/cicada/readyz": { "get": { - "description": "Check Cicada is alive", + "description": "Check Cicada is ready", "consumes": [ "application/json" ], @@ -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" } diff --git a/pkg/api/rest/docs/swagger.yaml b/pkg/api/rest/docs/swagger.yaml index 55685a2..1c6572b 100644 --- a/pkg/api/rest/docs/swagger.yaml +++ b/pkg/api/rest/docs/swagger.yaml @@ -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: diff --git a/pkg/api/rest/route/utility.go b/pkg/api/rest/route/utility.go index f2b36e8..36c3f44 100644 --- a/pkg/api/rest/route/utility.go +++ b/pkg/api/rest/route/utility.go @@ -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) }