Merge pull request #13069 from edx/christina/cache-disable-state
Use request cache for disabled_xblock_types
This commit is contained in:
@@ -1170,7 +1170,7 @@ class ModuleStoreReadBase(BulkOperationsMixin, ModuleStoreRead):
|
||||
contentstore=None,
|
||||
doc_store_config=None, # ignore if passed up
|
||||
metadata_inheritance_cache_subsystem=None, request_cache=None,
|
||||
xblock_mixins=(), xblock_select=None, xblock_field_data_wrappers=(), disabled_xblock_types=(), # pylint: disable=bad-continuation
|
||||
xblock_mixins=(), xblock_select=None, xblock_field_data_wrappers=(), disabled_xblock_types=lambda: [], # pylint: disable=bad-continuation
|
||||
# temporary parms to enable backward compatibility. remove once all envs migrated
|
||||
db=None, collection=None, host=None, port=None, tz_aware=True, user=None, password=None,
|
||||
# allow lower level init args to pass harmlessly
|
||||
|
||||
@@ -19,7 +19,6 @@ from django.conf import settings
|
||||
if not settings.configured:
|
||||
settings.configure()
|
||||
|
||||
from django.db.models.signals import post_save
|
||||
from django.core.cache import caches, InvalidCacheBackendError
|
||||
import django.dispatch
|
||||
import django.utils
|
||||
@@ -51,10 +50,8 @@ except ImportError:
|
||||
|
||||
try:
|
||||
from xblock_django.api import disabled_xblocks
|
||||
from xblock_django.models import XBlockConfiguration
|
||||
except ImportError:
|
||||
disabled_xblocks = None
|
||||
XBlockConfiguration = None
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
ASSET_IGNORE_REGEX = getattr(settings, "ASSET_IGNORE_REGEX", r"(^\._.*$)|(^\.DS_Store$)|(^.*~$)")
|
||||
@@ -191,13 +188,26 @@ def create_modulestore_instance(
|
||||
if 'read_preference' in doc_store_config:
|
||||
doc_store_config['read_preference'] = getattr(ReadPreference, doc_store_config['read_preference'])
|
||||
|
||||
if disabled_xblocks:
|
||||
disabled_xblock_types = [block.name for block in disabled_xblocks()]
|
||||
else:
|
||||
disabled_xblock_types = ()
|
||||
|
||||
xblock_field_data_wrappers = [load_function(path) for path in settings.XBLOCK_FIELD_DATA_WRAPPERS]
|
||||
|
||||
def fetch_disabled_xblock_types():
|
||||
"""
|
||||
Get the disabled xblock names, using the request_cache if possible to avoid hitting
|
||||
a database every time the list is needed.
|
||||
"""
|
||||
# If the import could not be loaded, return an empty list.
|
||||
if disabled_xblocks is None:
|
||||
return []
|
||||
|
||||
if request_cache:
|
||||
if 'disabled_xblock_types' not in request_cache.data:
|
||||
request_cache.data['disabled_xblock_types'] = [block.name for block in disabled_xblocks()]
|
||||
return request_cache.data['disabled_xblock_types']
|
||||
else:
|
||||
disabled_xblock_types = [block.name for block in disabled_xblocks()]
|
||||
|
||||
return disabled_xblock_types
|
||||
|
||||
return class_(
|
||||
contentstore=content_store,
|
||||
metadata_inheritance_cache_subsystem=metadata_inheritance_cache,
|
||||
@@ -205,7 +215,7 @@ def create_modulestore_instance(
|
||||
xblock_mixins=getattr(settings, 'XBLOCK_MIXINS', ()),
|
||||
xblock_select=getattr(settings, 'XBLOCK_SELECT_FUNCTION', None),
|
||||
xblock_field_data_wrappers=xblock_field_data_wrappers,
|
||||
disabled_xblock_types=disabled_xblock_types,
|
||||
disabled_xblock_types=fetch_disabled_xblock_types,
|
||||
doc_store_config=doc_store_config,
|
||||
i18n_service=i18n_service or ModuleI18nService,
|
||||
fs_service=fs_service or xblock.reference.plugins.FSService(),
|
||||
@@ -255,17 +265,6 @@ def clear_existing_modulestores():
|
||||
_MIXED_MODULESTORE = None
|
||||
|
||||
|
||||
if XBlockConfiguration and disabled_xblocks:
|
||||
@django.dispatch.receiver(post_save, sender=XBlockConfiguration)
|
||||
def reset_disabled_xblocks(sender, instance, **kwargs): # pylint: disable=unused-argument
|
||||
"""
|
||||
If XBlockConfiguation and disabled_xblocks are available, register a signal handler
|
||||
to update disabled_xblocks on model changes.
|
||||
"""
|
||||
disabled_xblock_types = [block.name for block in disabled_xblocks()]
|
||||
modulestore().disabled_xblock_types = disabled_xblock_types
|
||||
|
||||
|
||||
class ModuleI18nService(object):
|
||||
"""
|
||||
Implement the XBlock runtime "i18n" service.
|
||||
|
||||
@@ -185,25 +185,6 @@ class MixedModuleStore(ModuleStoreDraftAndPublished, ModuleStoreWriteBase):
|
||||
self.mappings[course_key] = store
|
||||
self.modulestores.append(store)
|
||||
|
||||
@property
|
||||
def disabled_xblock_types(self):
|
||||
"""
|
||||
Returns the list of disabled xblock types.
|
||||
"""
|
||||
return None if not hasattr(self, "_disabled_xblock_types") else self._disabled_xblock_types
|
||||
|
||||
@disabled_xblock_types.setter
|
||||
def disabled_xblock_types(self, value):
|
||||
"""
|
||||
Sets the list of disabled xblock types, and propagates down
|
||||
to child modulestores if available.
|
||||
"""
|
||||
self._disabled_xblock_types = value # pylint: disable=attribute-defined-outside-init
|
||||
|
||||
if hasattr(self, 'modulestores'):
|
||||
for store in self.modulestores:
|
||||
store.disabled_xblock_types = value
|
||||
|
||||
def _clean_locator_for_mapping(self, locator):
|
||||
"""
|
||||
In order for mapping to work, the locator must be minimal--no version, no branch--
|
||||
|
||||
@@ -1371,7 +1371,7 @@ class DescriptorSystem(MetricsMixin, ConfigurableFragmentWrapper, Runtime):
|
||||
"""
|
||||
# pylint: disable=bad-continuation
|
||||
def __init__(
|
||||
self, load_item, resources_fs, error_tracker, get_policy=None, disabled_xblock_types=(), **kwargs
|
||||
self, load_item, resources_fs, error_tracker, get_policy=None, disabled_xblock_types=lambda: [], **kwargs
|
||||
):
|
||||
"""
|
||||
load_item: Takes a Location and returns an XModuleDescriptor
|
||||
@@ -1439,7 +1439,7 @@ class DescriptorSystem(MetricsMixin, ConfigurableFragmentWrapper, Runtime):
|
||||
"""
|
||||
Returns a subclass of :class:`.XBlock` that corresponds to the specified `block_type`.
|
||||
"""
|
||||
if block_type in self.disabled_xblock_types:
|
||||
if block_type in self.disabled_xblock_types():
|
||||
return self.default_class
|
||||
return super(DescriptorSystem, self).load_block_type(block_type)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user