Skip to content

Commit

Permalink
Merge pull request #15 from MZC-CSC/develop
Browse files Browse the repository at this point in the history
Update readyz API to check database connection and subsystem readiness
  • Loading branch information
MZC-CSC authored Sep 11, 2024
2 parents 4a66d0a + 32d9bc1 commit afa5847
Show file tree
Hide file tree
Showing 23 changed files with 621 additions and 423 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ meta/*.db
ant
data
conf
container-volume
container-volume
./bin/**
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ARG TARGETOS=linux
ARG TARGETARCH=amd64

RUN apt-get update && \
apt-get install -y --no-install-recommends make && \
apt-get install -y --no-install-recommends make rsync && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

Expand Down
82 changes: 1 addition & 81 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,23 +1,3 @@
###########################################################
ANT_NETWORK=cm-ant-local-net
DB_CONTAINER_NAME=ant-local-postgres
DB_NAME=cm-ant-db
DB_USER=cm-ant-user
DB_PASSWORD=cm-ant-secret

ANT_CONTAINER_NAME=cm-ant
OS := $(shell uname -s | tr '[:upper:]' '[:lower:]')
ARCH := $(shell uname -m)

ifeq ($(ARCH),x86_64)
ARCH := amd64
else ifeq ($(ARCH),arm64)
ARCH := arm64
else ifeq ($(ARCH),aarch64)
ARCH := arm64
endif
###########################################################

###########################################################
.PHONY: swag
swag:
Expand All @@ -32,66 +12,6 @@ build:

###########################################################
.PHONY: run
run: run-db
run:
@go run cmd/cm-ant/main.go
###########################################################

###########################################################
.PHONY: create-network
create-network:
@if [ -z "$$(docker network ls -q -f name=$(ANT_NETWORK))" ]; then \
echo "Creating cm-ant network..."; \
docker network create --driver bridge $(ANT_NETWORK); \
echo "cm-ant network created!"; \
else \
echo "cm-ant network already exist..."; \
fi
###########################################################

###########################################################
.PHONY: run-db
run-db: create-network
@if [ -z "$$(docker container ps -q -f name=$(DB_CONTAINER_NAME))" ]; then \
echo "Run database container...."; \
docker container run \
--name $(DB_CONTAINER_NAME) \
--network $(ANT_NETWORK) \
-p 5432:5432 \
-e POSTGRES_USER=$(DB_USER) \
-e POSTGRES_PASSWORD=$(DB_PASSWORD) \
-e POSTGRES_DB=$(DB_NAME) \
-d --rm \
timescale/timescaledb:latest-pg16; \
echo "Started Postgres database container!"; \
echo "Waiting for database to be ready..."; \
for i in $$(seq 1 10); do \
docker container exec $(DB_CONTAINER_NAME) pg_isready -U $(DB_USER) -d $(DB_NAME); \
if [ $$? -eq 0 ]; then \
echo "Database is ready!"; \
break; \
fi; \
echo "Database is not ready yet. Waiting..."; \
sleep 5; \
done; \
if [ $$i -eq 10 ]; then \
echo "Failed to start the database"; \
exit 1; \
fi; \
echo "Database $(DB_NAME) successfully started!"; \
else \
echo "Database container is already running."; \
fi
###########################################################

###########################################################
.PHONY: down
down:
@echo "Checking if the database container is running..."
@if [ -n "$$(docker container ps -q -f name=$(DB_CONTAINER_NAME))" ]; then \
echo "Stopping and removing the database container..."; \
docker container stop $(DB_CONTAINER_NAME); \
echo "Database container stopped!"; \
else \
echo "No running database container found."; \
fi
###########################################################
36 changes: 36 additions & 0 deletions api/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions api/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,42 @@
}
}
}
},
"/readyz": {
"get": {
"description": "This endpoint checks if the CB-Ant API server is ready by verifying the status of both the load service and the cost service. If either service is unavailable, it returns a 503 status indicating the server is not ready.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"[Server Health]"
],
"summary": "Check CB-Ant API server readiness",
"operationId": "AntServerReadiness",
"responses": {
"200": {
"description": "CM-Ant API server is ready",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"503": {
"description": "CB-Ant API server is not ready",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
}
}
}
},
"definitions": {
Expand Down
26 changes: 26 additions & 0 deletions api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1335,4 +1335,30 @@ paths:
$ref: '#/definitions/app.AntResponse-string'
tags:
- '[Price Management]'
/readyz:
get:
consumes:
- application/json
description: This endpoint checks if the CB-Ant API server is ready by verifying
the status of both the load service and the cost service. If either service
is unavailable, it returns a 503 status indicating the server is not ready.
operationId: AntServerReadiness
produces:
- application/json
responses:
"200":
description: CM-Ant API server is ready
schema:
additionalProperties:
type: string
type: object
"503":
description: CB-Ant API server is not ready
schema:
additionalProperties:
type: string
type: object
summary: Check CB-Ant API server readiness
tags:
- '[Server Health]'
swagger: "2.0"
14 changes: 6 additions & 8 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ services:
- ANT_TUMBLEBUG_PORT=1323
- ANT_DATABASE_HOST=ant-postgres
healthcheck:
test: [ "CMD", "curl", "-f", "http://localhost:8880/ant/api/v1/readyz" ]
test: [ "CMD", "curl", "-f", "http://localhost:8880/ant/readyz" ]
interval: 1m
timeout: 5s
timeout: 7s
retries: 5
start_period: 10s
restart: unless-stopped
Expand All @@ -50,7 +50,7 @@ services:
restart: unless-stopped

cb-tumblebug:
image: cloudbaristaorg/cb-tumblebug:0.9.7
image: cloudbaristaorg/cb-tumblebug:0.9.11
container_name: cb-tumblebug
platform: linux/amd64
ports:
Expand Down Expand Up @@ -100,8 +100,6 @@ services:
retries: 3
start_period: 10s
restart: unless-stopped


cb-tumblebug-etcd:
image: gcr.io/etcd-development/etcd:v3.5.14
container_name: cb-tumblebug-etcd
Expand Down Expand Up @@ -140,16 +138,16 @@ services:
- stderr
- --auth-token
- simple
healthcheck: # for etcd
test: [ "CMD", "/usr/local/bin/etcd", "--version"]
healthcheck:
test: [ "CMD", "/usr/local/bin/etcd", "--version"]
interval: 1m
timeout: 5s
retries: 3
start_period: 10s
restart: unless-stopped

cb-spider:
image: cloudbaristaorg/cb-spider:0.9.0
image: cloudbaristaorg/cb-spider:0.9.4
container_name: cb-spider
platform: linux/amd64
networks:
Expand Down
37 changes: 37 additions & 0 deletions internal/app/common_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package app

import (
"net/http"

"github.com/labstack/echo/v4"
)

// @Id AntServerReadiness
// @Summary Check CB-Ant API server readiness
// @Description This endpoint checks if the CB-Ant API server is ready by verifying the status of both the load service and the cost service. If either service is unavailable, it returns a 503 status indicating the server is not ready.
// @Tags [Server Health]
// @Accept json
// @Produce json
// @Success 200 {object} map[string]string "CM-Ant API server is ready"
// @Failure 503 {object} map[string]string "CB-Ant API server is not ready"
// @Router /readyz [get]
func (s *AntServer) readyz(c echo.Context) error {
err := s.services.loadService.Readyz()
if err != nil {
return echo.NewHTTPError(http.StatusServiceUnavailable, map[string]string{
"message": "CM-Ant API server is not ready",
})
}

err = s.services.costService.Readyz()

if err != nil {
return echo.NewHTTPError(http.StatusServiceUnavailable, map[string]string{
"message": "CM-Ant API server is not ready",
})
}

return c.JSON(http.StatusOK, map[string]string{
"message": "CM-Ant API server is ready",
})
}
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit afa5847

Please sign in to comment.