From d2f216615f9406fd0ecea6648edaa6bae424add2 Mon Sep 17 00:00:00 2001 From: Chris Dodge Date: Thu, 14 Mar 2013 13:57:48 -0400 Subject: [PATCH] move the instantiation of the metadata cache out of modulestore.py as it was causing a circular import dependency when running on AWS. Put instantiation into one_time_startup.py which I believe is run before any Django requests are handled --- cms/one_time_startup.py | 8 ++++++++ common/lib/xmodule/xmodule/modulestore/__init__.py | 1 + common/lib/xmodule/xmodule/modulestore/django.py | 6 ------ common/lib/xmodule/xmodule/modulestore/mongo.py | 9 ++++----- common/lib/xmodule/xmodule/modulestore/xml.py | 2 +- lms/envs/dev.py | 7 +++++++ lms/one_time_startup.py | 9 +++++++++ 7 files changed, 30 insertions(+), 12 deletions(-) diff --git a/cms/one_time_startup.py b/cms/one_time_startup.py index 93428a3404..38a2fef847 100644 --- a/cms/one_time_startup.py +++ b/cms/one_time_startup.py @@ -1,5 +1,13 @@ from dogapi import dog_http_api, dog_stats_api from django.conf import settings +from xmodule.modulestore.django import modulestore + +from django.core.cache import get_cache, InvalidCacheBackendError + +cache = get_cache('mongo_metadata_inheritance') +for store_name in settings.MODULESTORE: + store = modulestore(store_name) + store.metadata_inheritance_cache = cache if hasattr(settings, 'DATADOG_API'): dog_http_api.api_key = settings.DATADOG_API diff --git a/common/lib/xmodule/xmodule/modulestore/__init__.py b/common/lib/xmodule/xmodule/modulestore/__init__.py index 525527c93f..022e016a58 100644 --- a/common/lib/xmodule/xmodule/modulestore/__init__.py +++ b/common/lib/xmodule/xmodule/modulestore/__init__.py @@ -423,6 +423,7 @@ class ModuleStoreBase(ModuleStore): Set up the error-tracking logic. ''' self._location_errors = {} # location -> ErrorLog + self.metadata_inheritance_cache = None def _get_errorlog(self, location): """ diff --git a/common/lib/xmodule/xmodule/modulestore/django.py b/common/lib/xmodule/xmodule/modulestore/django.py index 88471191d2..b0a65273c7 100644 --- a/common/lib/xmodule/xmodule/modulestore/django.py +++ b/common/lib/xmodule/xmodule/modulestore/django.py @@ -8,8 +8,6 @@ from __future__ import absolute_import from importlib import import_module from os import environ -from django.core.cache import get_cache, InvalidCacheBackendError - from django.conf import settings _MODULESTORES = {} @@ -35,10 +33,6 @@ def modulestore(name='default'): class_ = load_function(settings.MODULESTORE[name]['ENGINE']) options = {} - try: - options = {'metadata_inheritance_cache': get_cache('mongo_metadata_inheritance')} - except InvalidCacheBackendError: - pass options.update(settings.MODULESTORE[name]['OPTIONS']) for key in FUNCTION_KEYS: diff --git a/common/lib/xmodule/xmodule/modulestore/mongo.py b/common/lib/xmodule/xmodule/modulestore/mongo.py index b8f95c491c..c5e5bbfdf8 100644 --- a/common/lib/xmodule/xmodule/modulestore/mongo.py +++ b/common/lib/xmodule/xmodule/modulestore/mongo.py @@ -215,7 +215,7 @@ class MongoModuleStore(ModuleStoreBase): def __init__(self, host, db, collection, fs_root, render_template, port=27017, default_class=None, error_tracker=null_error_tracker, - user=None, password=None, metadata_inheritance_cache=None, **kwargs): + user=None, password=None, **kwargs): ModuleStoreBase.__init__(self) @@ -246,10 +246,6 @@ class MongoModuleStore(ModuleStoreBase): self.fs_root = path(fs_root) self.error_tracker = error_tracker self.render_template = render_template - if metadata_inheritance_cache is None: - logging.warning('metadata_inheritance_cache is None. Should be defined (unless running unit tests). Check config files....') - - self.metadata_inheritance_cache = metadata_inheritance_cache def get_metadata_inheritance_tree(self, location): ''' @@ -322,6 +318,9 @@ class MongoModuleStore(ModuleStoreBase): tree = None if self.metadata_inheritance_cache is not None: tree = self.metadata_inheritance_cache.get(key_name) + else: + # This is to help guard against an accident prod runtime without a cache + logging.warning('Running MongoModuleStore without metadata_inheritance_cache. This should not happen in production!') if tree is None or force_refresh: tree = self.get_metadata_inheritance_tree(location) diff --git a/common/lib/xmodule/xmodule/modulestore/xml.py b/common/lib/xmodule/xmodule/modulestore/xml.py index 029b1c38df..677f8b7d6a 100644 --- a/common/lib/xmodule/xmodule/modulestore/xml.py +++ b/common/lib/xmodule/xmodule/modulestore/xml.py @@ -253,7 +253,7 @@ class XMLModuleStore(ModuleStoreBase): """ An XML backed ModuleStore """ - def __init__(self, data_dir, default_class=None, course_dirs=None, load_error_modules=True, metadata_inheritance_cache=None): + def __init__(self, data_dir, default_class=None, course_dirs=None, load_error_modules=True): """ Initialize an XMLModuleStore from data_dir diff --git a/lms/envs/dev.py b/lms/envs/dev.py index 6ecbbb0f85..7b1d7dbc33 100644 --- a/lms/envs/dev.py +++ b/lms/envs/dev.py @@ -57,6 +57,13 @@ CACHES = { 'KEY_PREFIX': 'general', 'VERSION': 4, 'KEY_FUNCTION': 'util.memcache.safe_key', + }, + + 'mongo_metadata_inheritance': { + 'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache', + 'LOCATION': '/var/tmp/mongo_metadata_inheritance', + 'TIMEOUT': 300, + 'KEY_FUNCTION': 'util.memcache.safe_key', } } diff --git a/lms/one_time_startup.py b/lms/one_time_startup.py index 93428a3404..6b3c45d60f 100644 --- a/lms/one_time_startup.py +++ b/lms/one_time_startup.py @@ -1,5 +1,14 @@ +import logging from dogapi import dog_http_api, dog_stats_api from django.conf import settings +from xmodule.modulestore.django import modulestore + +from django.core.cache import get_cache, InvalidCacheBackendError + +cache = get_cache('mongo_metadata_inheritance') +for store_name in settings.MODULESTORE: + store = modulestore(store_name) + store.metadata_inheritance_cache = cache if hasattr(settings, 'DATADOG_API'): dog_http_api.api_key = settings.DATADOG_API