def update_search_index(course_id, triggered_time_isoformat): """ Updates course search index. """ try: course_key = CourseKey.from_string(course_id) # We skip search indexing for CCX courses because there is currently # some issue around Modulestore caching that makes it prohibitively # expensive (sometimes hours-long for really complex courses). if isinstance(course_key, CCXLocator): LOGGER.warning( u'Search indexing skipped for CCX Course %s (this is currently too slow to run in production)', course_id) return CoursewareSearchIndexer.index( modulestore(), course_key, triggered_at=(_parse_time(triggered_time_isoformat))) except SearchIndexingError as exc: LOGGER.error(u'Search indexing error for complete course %s - %s', course_id, text_type(exc)) else: LOGGER.debug(u'Search indexing successful for complete course %s', course_id)
def handle(self, *args, **options): """ By convention set by Django developers, this method actually executes command's actions. So, there could be no better docstring than emphasize this once again. """ course_ids = options['course_ids'] all_option = options['all'] setup_option = options['setup'] index_all_courses_option = all_option or setup_option if (not len(course_ids) and not index_all_courses_option) or ( len(course_ids) and index_all_courses_option): # lint-amnesty, pylint: disable=len-as-condition raise CommandError( "reindex_course requires one or more <course_id>s OR the --all or --setup flags." ) store = modulestore() if index_all_courses_option: index_names = (CoursewareSearchIndexer.INDEX_NAME, CourseAboutSearchIndexer.INDEX_NAME) if setup_option: for index_name in index_names: try: searcher = SearchEngine.get_search_engine(index_name) except exceptions.ElasticsearchException as exc: logging.exception(u'Search Engine error - %s', exc) return index_exists = searcher._es.indices.exists( index=index_name) # pylint: disable=protected-access index_mapping = searcher._es.indices.get_mapping( # pylint: disable=protected-access index=index_name, ) if index_exists else {} if index_exists and index_mapping: return # if reindexing is done during devstack setup step, don't prompt the user if setup_option or query_yes_no(self.CONFIRMATION_PROMPT, default="no"): # in case of --setup or --all, get the list of course keys from all courses # that are stored in the modulestore course_keys = [ course.id for course in modulestore().get_courses() ] else: return else: # in case course keys are provided as arguments course_keys = list(map(self._parse_course_key, course_ids)) for course_key in course_keys: try: CoursewareSearchIndexer.do_course_reindex(store, course_key) except Exception as exc: # lint-amnesty, pylint: disable=broad-except logging.exception( 'Error indexing course %s due to the error: %s', course_key, exc)
def test_indexing_no_item(self, mock_get_course): """ Test system logs an error if no item found. """ # set mocked exception response err = ItemNotFoundError mock_get_course.return_value = err # Start manual reindex and check error in response with self.assertRaises(SearchIndexingError): CoursewareSearchIndexer.do_course_reindex(modulestore(), self.course.id)
def listen_for_course_publish(sender, course_key, **kwargs): # pylint: disable=unused-argument """ Receives publishing signal and performs publishing related workflows, such as registering proctored exams, building up credit requirements, and performing search indexing """ # first is to registered exams, the credit subsystem will assume that # all proctored exams have already been registered, so we have to do that first try: register_special_exams(course_key) # pylint: disable=broad-except except Exception as exception: log.exception(exception) # then call into the credit subsystem (in /openedx/djangoapps/credit) # to perform any 'on_publish' workflow on_course_publish(course_key) # Finally call into the course search subsystem # to kick off an indexing action if CoursewareSearchIndexer.indexing_is_enabled(): # import here, because signal is registered at startup, but items in tasks are not yet able to be loaded from cms.djangoapps.contentstore.tasks import update_search_index update_search_index.delay(six.text_type(course_key), datetime.now(UTC).isoformat())
def listen_for_course_publish(sender, course_key, **kwargs): # pylint: disable=unused-argument """ Receives publishing signal and performs publishing related workflows, such as registering proctored exams, building up credit requirements, and performing search indexing """ # import here, because signal is registered at startup, but items in tasks are not yet able to be loaded from cms.djangoapps.contentstore.tasks import ( update_outline_from_modulestore_task, update_search_index, update_special_exams_and_publish ) # register special exams asynchronously course_key_str = str(course_key) update_special_exams_and_publish.delay(course_key_str) if key_supports_outlines(course_key): # Push the course outline to learning_sequences asynchronously. update_outline_from_modulestore_task.delay(course_key_str) # Finally call into the course search subsystem # to kick off an indexing action if CoursewareSearchIndexer.indexing_is_enabled() and CourseAboutSearchIndexer.indexing_is_enabled(): update_search_index.delay(course_key_str, datetime.now(UTC).isoformat())
def test_indexing_seq_error_responses(self, mock_index_dictionary): """ Test do_course_reindex response with mocked error data for sequence """ # results are indexed because they are published from ItemFactory response = perform_search("unique", user=self.user, size=10, from_=0, course_id=str(self.course.id)) self.assertEqual(response['total'], 1) # set mocked exception response err = Exception mock_index_dictionary.return_value = err # Start manual reindex and check error in response with self.assertRaises(SearchIndexingError): CoursewareSearchIndexer.do_course_reindex(modulestore(), self.course.id)
def test_indexing_responses(self): """ Test do_course_reindex response with real data """ # results are indexed because they are published from ItemFactory response = perform_search("unique", user=self.user, size=10, from_=0, course_id=str(self.course.id)) self.assertEqual(response['total'], 1) # Start manual reindex CoursewareSearchIndexer.do_course_reindex(modulestore(), self.course.id) # Check results are the same following reindex response = perform_search("unique", user=self.user, size=10, from_=0, course_id=str(self.course.id)) self.assertEqual(response['total'], 1)
def listen_for_course_publish(sender, course_key, **kwargs): # pylint: disable=unused-argument """ Receives publishing signal and performs publishing related workflows, such as registering proctored exams, building up credit requirements, and performing search indexing """ is_enabled = ENABLE_ASYNC_REGISTER_EXAMS.is_enabled(course_key) if is_enabled: from cms.djangoapps.contentstore.tasks import update_special_exams_and_publish course_key_str = str(course_key) update_special_exams_and_publish.delay(course_key_str) else: # first is to registered exams, the credit subsystem will assume that # all proctored exams have already been registered, so we have to do that first try: register_special_exams(course_key) # pylint: disable=broad-except except Exception as exception: log.exception(exception) # then call into the credit subsystem (in /openedx/djangoapps/credit) # to perform any 'on_publish' workflow on_course_publish(course_key) # import here, because signal is registered at startup, but items in tasks are not yet able to be loaded from cms.djangoapps.contentstore.tasks import update_outline_from_modulestore_task, update_search_index if key_supports_outlines(course_key): # Push the course outline to learning_sequences asynchronously. update_outline_from_modulestore_task.delay(str(course_key)) # Finally call into the course search subsystem # to kick off an indexing action if CoursewareSearchIndexer.indexing_is_enabled( ) and CourseAboutSearchIndexer.indexing_is_enabled(): update_search_index.delay(str(course_key), datetime.now(UTC).isoformat())