Skip to content

Commit

Permalink
chore: heatlhz return status json
Browse files Browse the repository at this point in the history
  • Loading branch information
terovirtanen committed Sep 30, 2024
1 parent ce8e95d commit 03524be
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
1 change: 1 addition & 0 deletions atv/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "1.0.1"

Check warning on line 1 in atv/__init__.py

View workflow job for this annotation

GitHub Actions / Coding style - flake8

no newline at end of file
10 changes: 9 additions & 1 deletion atv/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import environ
import sentry_sdk
from corsheaders.defaults import default_headers
from datetime import datetime
from django.core.exceptions import ImproperlyConfigured
from sentry_sdk.integrations.django import DjangoIntegration

Expand Down Expand Up @@ -65,11 +66,11 @@
TOKEN_AUTH_REQUIRE_SCOPE=(bool, False),
TOKEN_AUTH_AUTHSERVER_URL=(str, ""),
CLAMAV_HOST=(str, "atv-clamav"),
OPENSHIFT_BUILD_COMMIT=(str, ""),
)
if os.path.exists(env_file):
env.read_env(env_file)


VERSION = env("VERSION")
if VERSION is None:
try:
Expand Down Expand Up @@ -282,3 +283,10 @@

# Malware Protection
CLAMAV_HOST = env("CLAMAV_HOST")


# get build time from a file in docker image
APP_BUILDTIME = datetime.fromtimestamp(os.path.getmtime(__file__))

# get build commit from Openshift variable
BUILD_COMMIT = env("OPENSHIFT_BUILD_COMMIT")
56 changes: 54 additions & 2 deletions atv/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import pyclamd

Check failure on line 1 in atv/urls.py

View workflow job for this annotation

GitHub Actions / Coding style - isort

Imports are incorrectly sorted and/or formatted.
import threading
import time
from atv import __version__
from django.conf import settings
from django.contrib import admin
from django.http import HttpResponse
from django.db import connection
from django.http import HttpResponse, JsonResponse
from django.urls import include, path
from drf_spectacular.views import (
SpectacularAPIView,
Expand Down Expand Up @@ -68,10 +73,57 @@
# Kubernetes liveness & readiness probes
#

# Global variable to store the health check results
health_status = {
"db": {"message": "Initializing"},
"clamav": {"message": "Initializing"}
}

def check_db_connection():

Check failure on line 82 in atv/urls.py

View workflow job for this annotation

GitHub Actions / Coding style - flake8

expected 2 blank lines, found 1
global health_status
while True:
try:
with connection.cursor() as cursor:
cursor.execute("SELECT 1")
health_status["db"] = {"message": "OK"}
except Exception as ex:
health_status["db"] = {"error": str(ex)}
time.sleep(30) # Sleep for 0.5 minutes

def check_clamav_connection():

Check failure on line 93 in atv/urls.py

View workflow job for this annotation

GitHub Actions / Coding style - flake8

expected 2 blank lines, found 1
global health_status
while True:
try:
cd = pyclamd.ClamdNetworkSocket(host=settings.CLAMAV_HOST)
cd.ping()
health_status["clamav"] = {"message": "OK"}
except Exception as ex:
health_status["clamav"] = {"error": str(ex)}
time.sleep(30) # Sleep for 0.5 minutes

# Start the health check threads
threading.Thread(target=check_db_connection, daemon=True).start()

Check failure on line 105 in atv/urls.py

View workflow job for this annotation

GitHub Actions / Coding style - flake8

expected 2 blank lines after class or function definition, found 1
threading.Thread(target=check_clamav_connection, daemon=True).start()

def healthz(*args, **kwargs):

Check failure on line 108 in atv/urls.py

View workflow job for this annotation

GitHub Actions / Coding style - flake8

expected 2 blank lines, found 1
return HttpResponse(status=200)
response_data = {
"packageVersion": __version__,
"commitHash": settings.BUILD_COMMIT,
"buildTime": settings.APP_BUILDTIME,
"status": {
"message": {},
"error": {}
}
}

for key, status in health_status.items():
if "message" in status and status["message"] == "OK":
response_data["status"]["message"][key] = status["message"]
else:
response_data["status"]["error"][key] = status.get("error", "Unknown error")

status_code = 200 if all("message" in status and status["message"] == "OK" for status in health_status.values()) else 250

Check failure on line 125 in atv/urls.py

View workflow job for this annotation

GitHub Actions / Coding style - flake8

line too long (125 > 120 characters)
return JsonResponse(response_data, status=status_code)

def readiness(*args, **kwargs):

Check failure on line 128 in atv/urls.py

View workflow job for this annotation

GitHub Actions / Coding style - flake8

expected 2 blank lines, found 1
return HttpResponse(status=200)
Expand Down

0 comments on commit 03524be

Please sign in to comment.