44 lines
1.3 KiB
Python
44 lines
1.3 KiB
Python
"""
|
|
Core views - Health check and utility views
|
|
"""
|
|
from django.db import connection
|
|
from django.http import JsonResponse
|
|
from django.views.decorators.cache import never_cache
|
|
from django.views.decorators.http import require_GET
|
|
|
|
|
|
@never_cache
|
|
@require_GET
|
|
def health_check(request):
|
|
"""
|
|
Health check endpoint for monitoring and load balancers.
|
|
Returns JSON with status of various services.
|
|
"""
|
|
health_status = {
|
|
'status': 'ok',
|
|
'services': {}
|
|
}
|
|
|
|
# Check database connection
|
|
try:
|
|
with connection.cursor() as cursor:
|
|
cursor.execute("SELECT 1")
|
|
health_status['services']['database'] = 'ok'
|
|
except Exception as e:
|
|
health_status['status'] = 'error'
|
|
health_status['services']['database'] = f'error: {str(e)}'
|
|
|
|
# Check Redis/Celery (optional - don't fail if not available)
|
|
try:
|
|
from django_celery_beat.models import PeriodicTask
|
|
PeriodicTask.objects.count()
|
|
health_status['services']['celery_beat'] = 'ok'
|
|
except Exception:
|
|
health_status['services']['celery_beat'] = 'not_configured'
|
|
|
|
# Return appropriate status code
|
|
status_code = 200 if health_status['status'] == 'ok' else 503
|
|
|
|
return JsonResponse(health_status, status=status_code)
|
|
|