From ce461b1d6e17b76a3ef4ccf63e3673f679ae7f61 Mon Sep 17 00:00:00 2001 From: Daniel Clemente Laboreo Date: Tue, 9 Jan 2018 18:56:52 +0200 Subject: [PATCH] Include forum status in LMS heartbeat service. --- lms/lib/comment_client/utils.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/lms/lib/comment_client/utils.py b/lms/lib/comment_client/utils.py index 5acdc1fbdd..36fd1cf98f 100644 --- a/lms/lib/comment_client/utils.py +++ b/lms/lib/comment_client/utils.py @@ -8,6 +8,7 @@ import requests from django.utils.translation import get_language import dogstats_wrapper as dog_stats_api +from .settings import SERVICE_HOST as COMMENTS_SERVICE log = logging.getLogger(__name__) @@ -171,3 +172,30 @@ class CommentClientPaginatedResult(object): self.num_pages = num_pages self.thread_count = thread_count self.corrected_text = corrected_text + + +def check_forum_heartbeat(): + """ + Check the forum connection via its built-in heartbeat service and create an answer which can be used in the LMS + heartbeat django application. + This function can be connected to the LMS heartbeat checker through the HEARTBEAT_CHECKS variable. + """ + # To avoid dependency conflict + from django_comment_common.models import ForumsConfig + config = ForumsConfig.current() + + if not config.enabled: + # If this check is enabled but forums disabled, don't connect, just report no error + return 'forum', True, 'OK' + + try: + res = requests.get( + '%s/heartbeat' % COMMENTS_SERVICE, + timeout=config.connection_timeout + ).json() + if res['OK']: + return 'forum', True, 'OK' + else: + return 'forum', False, res.get('check', 'Forum heartbeat failed') + except Exception as fail: + return 'forum', False, unicode(fail)