Commit a7317a7d by Clinton Blackburn

Merge pull request #59 from edx/health-page-fix

Returning HTTP 503 if service is unhealthy
parents 347cce51 c4765efa
...@@ -46,7 +46,7 @@ class OperationalEndpointsTest(TestCaseWithAuthentication): ...@@ -46,7 +46,7 @@ class OperationalEndpointsTest(TestCaseWithAuthentication):
def test_health(self): def test_health(self):
self.assert_database_health('OK') self.assert_database_health('OK')
def assert_database_health(self, status): def assert_database_health(self, status, status_code=200):
response = self.client.get('/health', follow=True) response = self.client.get('/health', follow=True)
self.assertEquals( self.assertEquals(
response.data, response.data,
...@@ -57,7 +57,7 @@ class OperationalEndpointsTest(TestCaseWithAuthentication): ...@@ -57,7 +57,7 @@ class OperationalEndpointsTest(TestCaseWithAuthentication):
} }
} }
) )
self.assertEquals(response.status_code, 200) self.assertEquals(response.status_code, status_code)
@staticmethod @staticmethod
@contextmanager @contextmanager
...@@ -71,7 +71,7 @@ class OperationalEndpointsTest(TestCaseWithAuthentication): ...@@ -71,7 +71,7 @@ class OperationalEndpointsTest(TestCaseWithAuthentication):
databases['reporting'] = {} databases['reporting'] = {}
with self.override_database_connections(databases): with self.override_database_connections(databases):
self.assert_database_health('UNAVAILABLE') self.assert_database_health('UNAVAILABLE', status_code=503)
# Workaround to remove a setting from django settings. It has to be used in override_settings and then deleted. # Workaround to remove a setting from django settings. It has to be used in override_settings and then deleted.
@override_settings(ANALYTICS_DATABASE='reporting') @override_settings(ANALYTICS_DATABASE='reporting')
......
...@@ -69,8 +69,11 @@ class HealthView(APIView): ...@@ -69,8 +69,11 @@ class HealthView(APIView):
permission_classes = (permissions.AllowAny,) permission_classes = (permissions.AllowAny,)
def get(self, request, *args, **kwargs): # pylint: disable=unused-argument def get(self, request, *args, **kwargs): # pylint: disable=unused-argument
overall_status = 'UNAVAILABLE' OK = 'OK'
db_conn_status = 'UNAVAILABLE' UNAVAILABLE = 'UNAVAILABLE'
overall_status = UNAVAILABLE
db_conn_status = UNAVAILABLE
try: try:
connection_name = getattr(settings, 'ANALYTICS_DATABASE', 'default') connection_name = getattr(settings, 'ANALYTICS_DATABASE', 'default')
...@@ -78,8 +81,9 @@ class HealthView(APIView): ...@@ -78,8 +81,9 @@ class HealthView(APIView):
try: try:
cursor.execute("SELECT 1") cursor.execute("SELECT 1")
cursor.fetchone() cursor.fetchone()
overall_status = 'OK'
db_conn_status = 'OK' overall_status = OK
db_conn_status = OK
finally: finally:
cursor.close() cursor.close()
except Exception: # pylint: disable=broad-except except Exception: # pylint: disable=broad-except
...@@ -92,4 +96,4 @@ class HealthView(APIView): ...@@ -92,4 +96,4 @@ class HealthView(APIView):
} }
} }
return Response(response) return Response(response, status=200 if overall_status == OK else 503)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment