diff --git a/openedx/core/djangoapps/heartbeat/default_checks.py b/openedx/core/djangoapps/heartbeat/default_checks.py index 925afe3a9b..164fb5a221 100644 --- a/openedx/core/djangoapps/heartbeat/default_checks.py +++ b/openedx/core/djangoapps/heartbeat/default_checks.py @@ -3,23 +3,27 @@ A set of built-in default checks for the platform heartbeat endpoint Other checks should be included in their respective modules/djangoapps """ +from __future__ import absolute_import + from datetime import datetime, timedelta from time import sleep, time +import six from django.conf import settings from django.core.cache import cache from django.db import connection from django.db.utils import DatabaseError -from xmodule.modulestore.django import modulestore + from xmodule.exceptions import HeartbeatFailure +from xmodule.modulestore.django import modulestore from .tasks import sample_task - # DEFAULT SYSTEM CHECKS # Modulestore + def check_modulestore(): """ Check the modulestore connection @@ -37,7 +41,7 @@ def check_modulestore(): modulestore().heartbeat() return 'modulestore', True, u'OK' except HeartbeatFailure as fail: - return 'modulestore', False, unicode(fail) + return 'modulestore', False, six.text_type(fail) def check_database(): @@ -54,7 +58,7 @@ def check_database(): cursor.fetchone() return 'sql', True, u'OK' except DatabaseError as fail: - return 'sql', False, unicode(fail) + return 'sql', False, six.text_type(fail) # Caching @@ -74,7 +78,7 @@ def check_cache_set(): cache.set(CACHE_KEY, CACHE_VALUE, 30) return 'cache_set', True, u'OK' except Exception as fail: - return 'cache_set', False, unicode(fail) + return 'cache_set', False, six.text_type(fail) def check_cache_get(): @@ -92,7 +96,7 @@ def check_cache_get(): else: return 'cache_get', False, u'value check failed' except Exception as fail: - return 'cache_get', False, unicode(fail) + return 'cache_get', False, six.text_type(fail) # Celery @@ -113,8 +117,8 @@ def check_celery(): while expires > datetime.now(): if task.ready() and task.result: finished = str(time() - now) - return 'celery', True, unicode({'time': finished}) + return 'celery', True, six.text_type({'time': finished}) sleep(0.25) return 'celery', False, "expired" except Exception as fail: - return 'celery', False, unicode(fail) + return 'celery', False, six.text_type(fail) diff --git a/openedx/core/djangoapps/heartbeat/runchecks.py b/openedx/core/djangoapps/heartbeat/runchecks.py index 82d1e88d34..7d033101a0 100644 --- a/openedx/core/djangoapps/heartbeat/runchecks.py +++ b/openedx/core/djangoapps/heartbeat/runchecks.py @@ -1,3 +1,8 @@ +""" +Code for system checks. +""" +from __future__ import absolute_import + from importlib import import_module from django.conf import settings diff --git a/openedx/core/djangoapps/heartbeat/tasks.py b/openedx/core/djangoapps/heartbeat/tasks.py index 058eea8e02..d7ba1f6dff 100644 --- a/openedx/core/djangoapps/heartbeat/tasks.py +++ b/openedx/core/djangoapps/heartbeat/tasks.py @@ -1,6 +1,8 @@ """ A trivial task for health checks """ +from __future__ import absolute_import + from celery.task import task diff --git a/openedx/core/djangoapps/heartbeat/tests/test_heartbeat.py b/openedx/core/djangoapps/heartbeat/tests/test_heartbeat.py index a1282818c9..b1a029916c 100644 --- a/openedx/core/djangoapps/heartbeat/tests/test_heartbeat.py +++ b/openedx/core/djangoapps/heartbeat/tests/test_heartbeat.py @@ -1,16 +1,17 @@ """ Test the heartbeat """ -from __future__ import print_function +from __future__ import absolute_import, print_function + import json -from django.urls import reverse from django.db.utils import DatabaseError from django.test.client import Client -from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase +from django.urls import reverse from mock import patch from xmodule.exceptions import HeartbeatFailure +from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase class HeartbeatTestCase(ModuleStoreTestCase): diff --git a/openedx/core/djangoapps/heartbeat/urls.py b/openedx/core/djangoapps/heartbeat/urls.py index 652bbdee46..5d49ace2de 100644 --- a/openedx/core/djangoapps/heartbeat/urls.py +++ b/openedx/core/djangoapps/heartbeat/urls.py @@ -1,6 +1,8 @@ """ Urls for verifying health (heartbeat) of the app. """ +from __future__ import absolute_import + from django.conf.urls import url from openedx.core.djangoapps.heartbeat.views import heartbeat diff --git a/openedx/core/djangoapps/heartbeat/views.py b/openedx/core/djangoapps/heartbeat/views.py index 0224f653d7..b61071c99c 100644 --- a/openedx/core/djangoapps/heartbeat/views.py +++ b/openedx/core/djangoapps/heartbeat/views.py @@ -1,6 +1,10 @@ """ Views for verifying the health (heartbeat) of the app. """ +from __future__ import absolute_import + +import six + from util.json_request import JsonResponse from .runchecks import runchecks @@ -22,6 +26,6 @@ def heartbeat(request): status_code = 503 # 503 on any failure except Exception as e: status_code = 503 - check_results = {'error': unicode(e)} + check_results = {'error': six.text_type(e)} return JsonResponse(check_results, status=status_code)