Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: healthz return status json #111

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
10 changes: 9 additions & 1 deletion atv/settings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import subprocess
from datetime import datetime

import environ
import sentry_sdk
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")
66 changes: 64 additions & 2 deletions atv/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import threading
import time

import pyclamd
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 All @@ -9,6 +14,7 @@
)
from rest_framework_extensions.routers import ExtendedSimpleRouter

from atv import __version__
from documents.api import AttachmentViewSet, DocumentViewSet
from documents.api.viewsets import (
DocumentMetadataViewSet,
Expand Down Expand Up @@ -68,9 +74,65 @@
# Kubernetes liveness & readiness probes
#

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


def check_db_connection():
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(300) # Sleep for 5 minutes


def check_clamav_connection():
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(300) # Sleep for 5 minutes


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


def healthz(*args, **kwargs):
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 200
)
return JsonResponse(response_data, status=status_code)


def readiness(*args, **kwargs):
Expand Down
Loading