Merge pull request #13690 from edx/common_cleanup/heartbeat

Move heartbeat from common to openedx/core
This commit is contained in:
Nimisha Asthagiri
2016-10-07 13:59:25 -04:00
committed by GitHub
8 changed files with 18 additions and 12 deletions

View File

@@ -1,41 +0,0 @@
"""
Test the heartbeat
"""
import json
from django.core.urlresolvers import reverse
from django.db.utils import DatabaseError
from django.test.client import Client
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
from mock import patch
from xmodule.exceptions import HeartbeatFailure
class HeartbeatTestCase(ModuleStoreTestCase):
"""
Test the heartbeat
"""
def setUp(self):
self.client = Client()
self.heartbeat_url = reverse('heartbeat')
return super(HeartbeatTestCase, self).setUp()
def test_success(self):
response = self.client.get(self.heartbeat_url)
self.assertEqual(response.status_code, 200)
def test_sql_fail(self):
with patch('heartbeat.views.connection') as mock_connection:
mock_connection.cursor.return_value.execute.side_effect = DatabaseError
response = self.client.get(self.heartbeat_url)
self.assertEqual(response.status_code, 503)
response_dict = json.loads(response.content)
self.assertIn('SQL', response_dict)
def test_modulestore_fail(self):
with patch('heartbeat.views.modulestore') as mock_modulestore:
mock_modulestore.return_value.heartbeat.side_effect = HeartbeatFailure('msg', 'service')
response = self.client.get(self.heartbeat_url)
self.assertEqual(response.status_code, 503)

View File

@@ -1,7 +0,0 @@
from django.conf.urls import url, patterns
urlpatterns = patterns(
'',
url(r'^$', 'heartbeat.views.heartbeat', name='heartbeat'),
)

View File

@@ -1,33 +0,0 @@
from xmodule.modulestore.django import modulestore
from dogapi import dog_stats_api
from util.json_request import JsonResponse
from django.db import connection
from django.db.utils import DatabaseError
from xmodule.exceptions import HeartbeatFailure
@dog_stats_api.timed('edxapp.heartbeat')
def heartbeat(request):
"""
Simple view that a loadbalancer can check to verify that the app is up. Returns a json doc
of service id: status or message. If the status for any service is anything other than True,
it returns HTTP code 503 (Service Unavailable); otherwise, it returns 200.
"""
# This refactoring merely delegates to the default modulestore (which if it's mixed modulestore will
# delegate to all configured modulestores) and a quick test of sql. A later refactoring may allow
# any service to register itself as participating in the heartbeat. It's important that all implementation
# do as little as possible but give a sound determination that they are ready.
try:
output = modulestore().heartbeat()
except HeartbeatFailure as fail:
return JsonResponse({fail.service: unicode(fail)}, status=503)
cursor = connection.cursor()
try:
cursor.execute("SELECT CURRENT_DATE")
cursor.fetchone()
output['SQL'] = True
except DatabaseError as fail:
return JsonResponse({'SQL': unicode(fail)}, status=503)
return JsonResponse(output)