Skip to content

Commit

Permalink
Added health checks with flask-healthz
Browse files Browse the repository at this point in the history
Resolves: #352

Signed-off-by: Riccardi Dalexis <[email protected]>
  • Loading branch information
rod7760 committed Oct 9, 2024
1 parent dda9f5b commit acfe02e
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 5 deletions.
3 changes: 3 additions & 0 deletions mirrormanager2/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import flask
from flask_admin import Admin
from flask_healthz import healthz
from flask_oidc import OpenIDConnect
from sqlalchemy.orm import configure_mappers

Expand Down Expand Up @@ -130,6 +131,8 @@ def create_app(config=None):
app.register_blueprint(api_views, url_prefix="/api")
from mirrormanager2.xml_rpc import XMLRPC

app.register_blueprint(healthz, url_prefix="/healthz")

XMLRPC.connect(app, "/xmlrpc")

return app
5 changes: 5 additions & 0 deletions mirrormanager2/default_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,8 @@
UMDL_PREFIX = ""

UMDL_MASTER_DIRECTORIES = []

HEALTHZ = {
"live": "mirrormanager2.health_checks.liveness",
"ready": "mirrormanager2.health_checks.readiness",
}
15 changes: 15 additions & 0 deletions mirrormanager2/health_checks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from flask_healthz import HealthError
from sqlalchemy.exc import OperationalError

from mirrormanager2.database import DB


def liveness():
pass


def readiness():
try:
DB.manager.get_status()
except OperationalError as err:
raise HealthError("Can't connect to the database") from err
24 changes: 19 additions & 5 deletions poetry.lock

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

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ python = "^3.9"
email-validator = ">=1.1.3"
flask = "^2.2.3 || ^3.0.0"
flask-admin = "^1.6.0"
flask-healthz = "^1.0.0"
flask-oidc = "^2.0.0"
flask-xml-rpc-re = "^0.1.4"
flask-wtf = "^1.1.1"
Expand Down
24 changes: 24 additions & 0 deletions tests/test_health_checks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import os

from mirrormanager2.app import create_app


def test_healthz_readiness_ok(client):
result = client.get("/healthz/ready")
assert result.status_code == 200


def test_healthz_readiness_not_ok(app):
here = os.path.dirname(os.path.abspath(__file__))
app = create_app(
{
"TESTING": True,
# Set fake database
"SQLALCHEMY_DATABASE_URI": "sqlite:///I/don't/exist.sqlite",
"OIDC_CLIENT_SECRETS": os.path.join(here, "client_secrets.json"),
"USE_FEDORA_MESSAGING": False,
}
)
client = app.test_client()
result = client.get("/healthz/ready")
assert result.status_code != 200

0 comments on commit acfe02e

Please sign in to comment.