From 255267d42cb6103b95047a644532b5da44f76b36 Mon Sep 17 00:00:00 2001 From: David Ormsbee Date: Thu, 16 Jun 2016 11:52:45 -0400 Subject: [PATCH] Create forums config model for adjusting connection timeouts. --- .../djangoapps/django_comment_common/admin.py | 9 ++++++ .../migrations/0002_forumsconfig.py | 31 +++++++++++++++++++ .../django_comment_common/models.py | 12 +++++++ .../django_comment_client/base/tests.py | 8 ++--- .../django_comment_client/forum/tests.py | 8 ++--- lms/lib/comment_client/utils.py | 5 ++- 6 files changed, 64 insertions(+), 9 deletions(-) create mode 100644 common/djangoapps/django_comment_common/admin.py create mode 100644 common/djangoapps/django_comment_common/migrations/0002_forumsconfig.py diff --git a/common/djangoapps/django_comment_common/admin.py b/common/djangoapps/django_comment_common/admin.py new file mode 100644 index 0000000000..d2187e77e2 --- /dev/null +++ b/common/djangoapps/django_comment_common/admin.py @@ -0,0 +1,9 @@ +""" +Admin for managing the connection to the Forums backend service. +""" +from django.contrib import admin + +from .models import ForumsConfig + + +admin.site.register(ForumsConfig) diff --git a/common/djangoapps/django_comment_common/migrations/0002_forumsconfig.py b/common/djangoapps/django_comment_common/migrations/0002_forumsconfig.py new file mode 100644 index 0000000000..8694e91018 --- /dev/null +++ b/common/djangoapps/django_comment_common/migrations/0002_forumsconfig.py @@ -0,0 +1,31 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion +from django.conf import settings + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('django_comment_common', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='ForumsConfig', + fields=[ + ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), + ('change_date', models.DateTimeField(auto_now_add=True, verbose_name='Change date')), + ('enabled', models.BooleanField(default=False, verbose_name='Enabled')), + ('connection_timeout', models.FloatField(default=5.0)), + ('changed_by', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, editable=False, to=settings.AUTH_USER_MODEL, null=True, verbose_name='Changed by')), + ], + options={ + 'ordering': ('-change_date',), + 'abstract': False, + }, + ), + ] diff --git a/common/djangoapps/django_comment_common/models.py b/common/djangoapps/django_comment_common/models.py index 0ec8b96081..c831bf88a9 100644 --- a/common/djangoapps/django_comment_common/models.py +++ b/common/djangoapps/django_comment_common/models.py @@ -7,6 +7,7 @@ from django.dispatch import receiver from django.db.models.signals import post_save from django.utils.translation import ugettext_noop +from config_models.models import ConfigurationModel from student.models import CourseEnrollment from xmodule.modulestore.django import modulestore @@ -137,3 +138,14 @@ def all_permissions_for_user_in_course(user, course_id): # pylint: disable=inva if not permission_blacked_out(course, all_roles, permission.name) } return permissions + + +class ForumsConfig(ConfigurationModel): + """Config for the connection to the cs_comments_service forums backend.""" + + # For now, just tweak the connection timeout settings. We can add more later. + connection_timeout = models.FloatField(default=5.0) + + def __unicode__(self): + """Simple representation so the admin screen looks less ugly.""" + return u"ForumsConfig: timeout={}".format(self.connection_timeout) diff --git a/lms/djangoapps/django_comment_client/base/tests.py b/lms/djangoapps/django_comment_client/base/tests.py index e94573de7f..bbce7dc065 100644 --- a/lms/djangoapps/django_comment_client/base/tests.py +++ b/lms/djangoapps/django_comment_client/base/tests.py @@ -372,8 +372,8 @@ class ViewsQueryCountTestCase(UrlResetMixin, ModuleStoreTestCase, MockRequestSet return inner @ddt.data( - (ModuleStoreEnum.Type.mongo, 3, 4, 29), - (ModuleStoreEnum.Type.split, 3, 13, 29), + (ModuleStoreEnum.Type.mongo, 3, 4, 30), + (ModuleStoreEnum.Type.split, 3, 13, 30), ) @ddt.unpack @count_queries @@ -381,8 +381,8 @@ class ViewsQueryCountTestCase(UrlResetMixin, ModuleStoreTestCase, MockRequestSet self.create_thread_helper(mock_request) @ddt.data( - (ModuleStoreEnum.Type.mongo, 3, 3, 23), - (ModuleStoreEnum.Type.split, 3, 10, 23), + (ModuleStoreEnum.Type.mongo, 3, 3, 24), + (ModuleStoreEnum.Type.split, 3, 10, 24), ) @ddt.unpack @count_queries diff --git a/lms/djangoapps/django_comment_client/forum/tests.py b/lms/djangoapps/django_comment_client/forum/tests.py index dfeb5d9e4c..9081066c04 100644 --- a/lms/djangoapps/django_comment_client/forum/tests.py +++ b/lms/djangoapps/django_comment_client/forum/tests.py @@ -342,11 +342,11 @@ class SingleThreadQueryCountTestCase(ModuleStoreTestCase): @ddt.data( # old mongo with cache - (ModuleStoreEnum.Type.mongo, 1, 6, 4, 16, 8), - (ModuleStoreEnum.Type.mongo, 50, 6, 4, 16, 8), + (ModuleStoreEnum.Type.mongo, 1, 6, 4, 18, 10), + (ModuleStoreEnum.Type.mongo, 50, 6, 4, 18, 10), # split mongo: 3 queries, regardless of thread response size. - (ModuleStoreEnum.Type.split, 1, 3, 3, 16, 8), - (ModuleStoreEnum.Type.split, 50, 3, 3, 16, 8), + (ModuleStoreEnum.Type.split, 1, 3, 3, 18, 10), + (ModuleStoreEnum.Type.split, 50, 3, 3, 18, 10), ) @ddt.unpack def test_number_of_mongo_queries( diff --git a/lms/lib/comment_client/utils.py b/lms/lib/comment_client/utils.py index 5c133fed8a..25eaaf3ef6 100644 --- a/lms/lib/comment_client/utils.py +++ b/lms/lib/comment_client/utils.py @@ -53,6 +53,8 @@ def request_timer(request_id, method, url, tags=None): def perform_request(method, url, data_or_params=None, raw=False, metric_action=None, metric_tags=None, paged_results=False): + # To avoid dependency conflict + from django_comment_common.models import ForumsConfig if metric_tags is None: metric_tags = [] @@ -77,13 +79,14 @@ def perform_request(method, url, data_or_params=None, raw=False, data = None params = merge_dict(data_or_params, request_id_dict) with request_timer(request_id, method, url, metric_tags): + config = ForumsConfig.current() response = requests.request( method, url, data=data, params=params, headers=headers, - timeout=5 + timeout=config.connection_timeout ) metric_tags.append(u'status_code:{}'.format(response.status_code))