-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhealth.py
More file actions
49 lines (42 loc) · 1.73 KB
/
health.py
File metadata and controls
49 lines (42 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import logging
from django.http import JsonResponse
from django.views import View
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt
# from django.db import connection
from django.conf import settings
logger = logging.getLogger(f'gnana.{__name__}')
@method_decorator(csrf_exempt, name='dispatch')
class HealthCheckView(View):
def get(self, request):
"""
Health check endpoint that verifies basic application health
Returns 200 if healthy, 503 if unhealthy
"""
try:
health_status = {
"status": "healthy",
"service": "aviso-core",
"version": getattr(settings, 'APP_VERSION', '1.0.0'),
"checks": {}
}
try:
## TODO Mongo HEalth CHeck
# with connection.cursor() as cursor:
# cursor.execute("SELECT 1")
health_status["checks"]["database"] = "ok"
except Exception as db_error:
logger.warning(f"Database health check failed: {str(db_error)}")
health_status["checks"]["database"] = "failed"
health_status["status"] = "degraded"
logger.info("Health check passed")
status_code = 200 if health_status["status"] == "healthy" else 503
return JsonResponse(health_status, status=status_code)
except Exception as e:
logger.error(f"Health check endpoint error: {str(e)}", exc_info=True)
error_response = {
"status": "unhealthy",
"service": "aviso-core",
"error": str(e)
}
return JsonResponse(error_response, status=503)