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):
def test_health(self):
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)
self.assertEquals(
response.data,
......@@ -57,7 +57,7 @@ class OperationalEndpointsTest(TestCaseWithAuthentication):
}
}
)
self.assertEquals(response.status_code, 200)
self.assertEquals(response.status_code, status_code)
@staticmethod
@contextmanager
......@@ -71,7 +71,7 @@ class OperationalEndpointsTest(TestCaseWithAuthentication):
databases['reporting'] = {}
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.
@override_settings(ANALYTICS_DATABASE='reporting')
......
......@@ -69,8 +69,11 @@ class HealthView(APIView):
permission_classes = (permissions.AllowAny,)
def get(self, request, *args, **kwargs): # pylint: disable=unused-argument
overall_status = 'UNAVAILABLE'
db_conn_status = 'UNAVAILABLE'
OK = 'OK'
UNAVAILABLE = 'UNAVAILABLE'
overall_status = UNAVAILABLE
db_conn_status = UNAVAILABLE
try:
connection_name = getattr(settings, 'ANALYTICS_DATABASE', 'default')
......@@ -78,8 +81,9 @@ class HealthView(APIView):
try:
cursor.execute("SELECT 1")
cursor.fetchone()
overall_status = 'OK'
db_conn_status = 'OK'
overall_status = OK
db_conn_status = OK
finally:
cursor.close()
except Exception: # pylint: disable=broad-except
......@@ -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