Commit 63e0f470 by Calen Pennington

Merge pull request #414 from MITx/kimth/xqueue_callback_blocking

Rename acquire_lock to select_for_update, add docstring
parents b8de8ea8 ce11520d
...@@ -67,7 +67,7 @@ class StudentModuleCache(object): ...@@ -67,7 +67,7 @@ class StudentModuleCache(object):
""" """
A cache of StudentModules for a specific student A cache of StudentModules for a specific student
""" """
def __init__(self, user, descriptors, acquire_lock=False): def __init__(self, user, descriptors, select_for_update=False):
''' '''
Find any StudentModule objects that are needed by any descriptor Find any StudentModule objects that are needed by any descriptor
in descriptors. Avoids making multiple queries to the database. in descriptors. Avoids making multiple queries to the database.
...@@ -77,6 +77,7 @@ class StudentModuleCache(object): ...@@ -77,6 +77,7 @@ class StudentModuleCache(object):
Arguments Arguments
user: The user for which to fetch maching StudentModules user: The user for which to fetch maching StudentModules
descriptors: An array of XModuleDescriptors. descriptors: An array of XModuleDescriptors.
select_for_update: Flag indicating whether the row should be locked until end of transaction
''' '''
if user.is_authenticated(): if user.is_authenticated():
module_ids = self._get_module_state_keys(descriptors) module_ids = self._get_module_state_keys(descriptors)
...@@ -86,7 +87,7 @@ class StudentModuleCache(object): ...@@ -86,7 +87,7 @@ class StudentModuleCache(object):
self.cache = [] self.cache = []
chunk_size = 500 chunk_size = 500
for id_chunk in [module_ids[i:i + chunk_size] for i in xrange(0, len(module_ids), chunk_size)]: for id_chunk in [module_ids[i:i + chunk_size] for i in xrange(0, len(module_ids), chunk_size)]:
if acquire_lock: if select_for_update:
self.cache.extend(StudentModule.objects.select_for_update().filter( self.cache.extend(StudentModule.objects.select_for_update().filter(
student=user, student=user,
module_state_key__in=id_chunk) module_state_key__in=id_chunk)
...@@ -102,13 +103,14 @@ class StudentModuleCache(object): ...@@ -102,13 +103,14 @@ class StudentModuleCache(object):
@classmethod @classmethod
def cache_for_descriptor_descendents(cls, user, descriptor, depth=None, descriptor_filter=lambda descriptor: True, acquire_lock=False): def cache_for_descriptor_descendents(cls, user, descriptor, depth=None, descriptor_filter=lambda descriptor: True, select_for_update=False):
""" """
descriptor: An XModuleDescriptor descriptor: An XModuleDescriptor
depth is the number of levels of descendent modules to load StudentModules for, in addition to depth is the number of levels of descendent modules to load StudentModules for, in addition to
the supplied descriptor. If depth is None, load all descendent StudentModules the supplied descriptor. If depth is None, load all descendent StudentModules
descriptor_filter is a function that accepts a descriptor and return wether the StudentModule descriptor_filter is a function that accepts a descriptor and return wether the StudentModule
should be cached should be cached
select_for_update: Flag indicating whether the row should be locked until end of transaction
""" """
def get_child_descriptors(descriptor, depth, descriptor_filter): def get_child_descriptors(descriptor, depth, descriptor_filter):
...@@ -128,7 +130,7 @@ class StudentModuleCache(object): ...@@ -128,7 +130,7 @@ class StudentModuleCache(object):
descriptors = get_child_descriptors(descriptor, depth, descriptor_filter) descriptors = get_child_descriptors(descriptor, depth, descriptor_filter)
return StudentModuleCache(user, descriptors, acquire_lock) return StudentModuleCache(user, descriptors, select_for_update)
def _get_module_state_keys(self, descriptors): def _get_module_state_keys(self, descriptors):
''' '''
......
...@@ -308,7 +308,7 @@ def xqueue_callback(request, course_id, userid, id, dispatch): ...@@ -308,7 +308,7 @@ def xqueue_callback(request, course_id, userid, id, dispatch):
user = User.objects.get(id=userid) user = User.objects.get(id=userid)
student_module_cache = StudentModuleCache.cache_for_descriptor_descendents( student_module_cache = StudentModuleCache.cache_for_descriptor_descendents(
user, modulestore().get_item(id), depth=0, acquire_lock=True) user, modulestore().get_item(id), depth=0, select_for_update=True)
instance = get_module(user, request, id, student_module_cache) instance = get_module(user, request, id, student_module_cache)
if instance is None: if instance is None:
log.debug("No module {} for user {}--access denied?".format(id, user)) log.debug("No module {} for user {}--access denied?".format(id, user))
......
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