Skip to content

Commit

Permalink
Tweak HealthCheckMiddleware.readiness to close DB cursor.
Browse files Browse the repository at this point in the history
  • Loading branch information
ropable committed Sep 17, 2024
1 parent daaa757 commit e61a32e
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions ibms_project/middleware.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from django.db import connections
from django.http import HttpResponse, HttpResponseServerError
import logging

from django.db import connections
from django.http import HttpResponse, HttpResponseServerError

LOGGER = logging.getLogger("ibms")


class HealthCheckMiddleware(object):

def __init__(self, get_response):
self.get_response = get_response

Expand All @@ -20,8 +19,7 @@ def __call__(self, request):
return self.get_response(request)

def liveness(self, request):
"""Returns that the server is alive.
"""
"""Returns that the server is alive."""
return HttpResponse("OK")

def readiness(self, request):
Expand All @@ -33,6 +31,7 @@ def readiness(self, request):
cursor = connections["default"].cursor()
cursor.execute("SELECT 1;")
row = cursor.fetchone()
cursor.close()
if row is None:
return HttpResponseServerError("Database: invalid response")
except Exception as e:
Expand Down

0 comments on commit e61a32e

Please sign in to comment.