Commit d2f21661 by Chris Dodge

move the instantiation of the metadata cache out of modulestore.py as it was…

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
parent 3211221f
from dogapi import dog_http_api, dog_stats_api from dogapi import dog_http_api, dog_stats_api
from django.conf import settings 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'): if hasattr(settings, 'DATADOG_API'):
dog_http_api.api_key = settings.DATADOG_API dog_http_api.api_key = settings.DATADOG_API
......
...@@ -423,6 +423,7 @@ class ModuleStoreBase(ModuleStore): ...@@ -423,6 +423,7 @@ class ModuleStoreBase(ModuleStore):
Set up the error-tracking logic. Set up the error-tracking logic.
''' '''
self._location_errors = {} # location -> ErrorLog self._location_errors = {} # location -> ErrorLog
self.metadata_inheritance_cache = None
def _get_errorlog(self, location): def _get_errorlog(self, location):
""" """
......
...@@ -8,8 +8,6 @@ from __future__ import absolute_import ...@@ -8,8 +8,6 @@ from __future__ import absolute_import
from importlib import import_module from importlib import import_module
from os import environ from os import environ
from django.core.cache import get_cache, InvalidCacheBackendError
from django.conf import settings from django.conf import settings
_MODULESTORES = {} _MODULESTORES = {}
...@@ -35,10 +33,6 @@ def modulestore(name='default'): ...@@ -35,10 +33,6 @@ def modulestore(name='default'):
class_ = load_function(settings.MODULESTORE[name]['ENGINE']) class_ = load_function(settings.MODULESTORE[name]['ENGINE'])
options = {} options = {}
try:
options = {'metadata_inheritance_cache': get_cache('mongo_metadata_inheritance')}
except InvalidCacheBackendError:
pass
options.update(settings.MODULESTORE[name]['OPTIONS']) options.update(settings.MODULESTORE[name]['OPTIONS'])
for key in FUNCTION_KEYS: for key in FUNCTION_KEYS:
......
...@@ -215,7 +215,7 @@ class MongoModuleStore(ModuleStoreBase): ...@@ -215,7 +215,7 @@ class MongoModuleStore(ModuleStoreBase):
def __init__(self, host, db, collection, fs_root, render_template, def __init__(self, host, db, collection, fs_root, render_template,
port=27017, default_class=None, port=27017, default_class=None,
error_tracker=null_error_tracker, error_tracker=null_error_tracker,
user=None, password=None, metadata_inheritance_cache=None, **kwargs): user=None, password=None, **kwargs):
ModuleStoreBase.__init__(self) ModuleStoreBase.__init__(self)
...@@ -246,10 +246,6 @@ class MongoModuleStore(ModuleStoreBase): ...@@ -246,10 +246,6 @@ class MongoModuleStore(ModuleStoreBase):
self.fs_root = path(fs_root) self.fs_root = path(fs_root)
self.error_tracker = error_tracker self.error_tracker = error_tracker
self.render_template = render_template 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): def get_metadata_inheritance_tree(self, location):
''' '''
...@@ -322,6 +318,9 @@ class MongoModuleStore(ModuleStoreBase): ...@@ -322,6 +318,9 @@ class MongoModuleStore(ModuleStoreBase):
tree = None tree = None
if self.metadata_inheritance_cache is not None: if self.metadata_inheritance_cache is not None:
tree = self.metadata_inheritance_cache.get(key_name) 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: if tree is None or force_refresh:
tree = self.get_metadata_inheritance_tree(location) tree = self.get_metadata_inheritance_tree(location)
......
...@@ -253,7 +253,7 @@ class XMLModuleStore(ModuleStoreBase): ...@@ -253,7 +253,7 @@ class XMLModuleStore(ModuleStoreBase):
""" """
An XML backed ModuleStore 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 Initialize an XMLModuleStore from data_dir
......
...@@ -57,6 +57,13 @@ CACHES = { ...@@ -57,6 +57,13 @@ CACHES = {
'KEY_PREFIX': 'general', 'KEY_PREFIX': 'general',
'VERSION': 4, 'VERSION': 4,
'KEY_FUNCTION': 'util.memcache.safe_key', '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',
} }
} }
......
import logging
from dogapi import dog_http_api, dog_stats_api from dogapi import dog_http_api, dog_stats_api
from django.conf import settings 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'): if hasattr(settings, 'DATADOG_API'):
dog_http_api.api_key = settings.DATADOG_API dog_http_api.api_key = settings.DATADOG_API
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment